【发布时间】:2019-11-21 17:02:39
【问题描述】:
我正在尝试复制(一个更小的版本)AlphaGo Zero 系统。但是,在网络模型中,我遇到了问题。我应该实现的损失函数如下:
地点:
- z 是网络两个头之一的标签(介于 -1 和 1 之间的实际值),v 是网络预测的这个值。
- pi 是所有动作的分布概率的标签,p 是网络预测的所有动作的分布概率。
- c 是 L2 正则化参数。
我向网络传递一个通道列表(代表游戏状态)和一个数组(与 pi 和 p 大小相同)代表哪些动作确实有效(如果有效则输入1,否则输入0)。
如您所见,损失函数同时使用目标和网络预测进行计算。但是经过广泛的搜索,在实现我的自定义损失函数时,我只能作为参数传递y_true 和y_pred,即使我有两个“y_true”和两个“y_pred”。我曾尝试使用索引来获取这些值,但我很确定它不起作用。
网络的建模和自定义损失函数在下面的代码中:
def custom_loss(y_true, y_pred):
# I am pretty sure this does not work
output_prob_dist = y_pred[0]
output_value = y_pred[1]
label_prob_dist = y_true[0]
label_value = y_pred[1]
mse_loss = K.mean(K.square(label_value - output_value), axis=-1)
cross_entropy_loss = K.dot(K.transpose(label_prob_dist), output_prob_dist)
return mse_loss - cross_entropy_loss
def define_model():
"""Neural Network model implementation using Keras + Tensorflow."""
state_channels = Input(shape = (5,5,6), name='States_Channels_Input')
valid_actions_dist = Input(shape = (32,), name='Valid_Actions_Input')
conv = Conv2D(filters=10, kernel_size=2, kernel_regularizer=regularizers.l2(0.0001), activation='relu', name='Conv_Layer')(state_channels)
pool = MaxPooling2D(pool_size=(2, 2), name='Pooling_Layer')(conv)
flat = Flatten(name='Flatten_Layer')(pool)
# Merge of the flattened channels (after pooling) and the valid action
# distribution. Used only as input in the probability distribution head.
merge = concatenate([flat, valid_actions_dist])
#Probability distribution over actions
hidden_fc_prob_dist_1 = Dense(100, kernel_regularizer=regularizers.l2(0.0001), activation='relu', name='FC_Prob_1')(merge)
hidden_fc_prob_dist_2 = Dense(100, kernel_regularizer=regularizers.l2(0.0001), activation='relu', name='FC_Prob_2')(hidden_fc_prob_dist_1)
output_prob_dist = Dense(32, kernel_regularizer=regularizers.l2(0.0001), activation='softmax', name='Output_Dist')(hidden_fc_prob_dist_2)
#Value of a state
hidden_fc_value_1 = Dense(100, kernel_regularizer=regularizers.l2(0.0001), activation='relu', name='FC_Value_1')(flat)
hidden_fc_value_2 = Dense(100, kernel_regularizer=regularizers.l2(0.0001), activation='relu', name='FC_Value_2')(hidden_fc_value_1)
output_value = Dense(1, kernel_regularizer=regularizers.l2(0.0001), activation='tanh', name='Output_Value')(hidden_fc_value_2)
model = Model(inputs=[state_channels, valid_actions_dist], outputs=[output_prob_dist, output_value])
model.compile(loss=custom_loss, optimizer='adam', metrics=['accuracy'])
return model
# In the main method
model = define_model()
# ...
# MCTS routine to collect the data for the network input
# ...
x_train = [channels_input, valid_actions_dist_input]
y_train = [dist_probs_label, who_won_label]
model.fit(x_train, y_train, epochs=10)
简而言之,我的问题是:如何正确实现这个同时使用网络输出和网络标签值的自定义损失函数?
【问题讨论】:
-
你有没有试过为不同的目标实现两种不同的损失函数,然后
model.compile(loss={custom_loss1,custom_loss2}, optimizer='adam', metrics=['accuracy'])。顺便说一下,图片没有显示。 -
@Abdirahman 此处的图像显示良好(在三种不同的设备中)。关于你的建议,我已经考虑过了。但是,论文中显示的损失函数是 MSE 和交叉熵的减法。如果我分别实现它们,权重更新肯定会有所不同。还是我错了?
-
可以得到论文的链接吗?
标签: python tensorflow keras loss-function