【发布时间】:2017-03-16 12:04:57
【问题描述】:
我正在尝试使用 Keras 重写一段 tflearn 代码。
目标是组合两个输入,其中一个输入跳过第一层。以下代码在 tflearn 中有效:
# Two different inputs.
inputs = tflearn.input_data(shape=[None, 10])
action = tflearn.input_data(shape=[None, 10])
#First layer used only by the inputs
net = tflearn.fully_connected(inputs, 400, activation='relu')
# Add the action tensor in the 2nd hidden layer
# Use two temp layers to get the corresponding weights and biases
t1 = tflearn.fully_connected(net, 300)
t2 = tflearn.fully_connected(action, 300)
# Combine the two layers using the weights from t1 and t2 and the bias from t2
net = tflearn.activation(tf.matmul(net,t1.W) + tf.matmul(action, t2.W) + t2.b, activation='relu')
我正在尝试使用以下代码在 Keras 中复制此代码:
# Two different inputs.
inputs = tf.placeholder(tf.float32, [None, 10])
action = tf.placeholder(tf.float32, [None, 10])
#First layer used only by the inputs
t1 = Sequential()
t1.add(Dense(400, activation='relu', input_shape=(1,10)))
# Add the action tensor in the 2nd hidden layer
# Use two temp layers to get the corresponding weights and biases
t1.add(Dense(300))
t2 = Sequential()
t2.add(Dense(300, input_shape=(1,10)))
# Combine the two layers
critnet = Sequential()
critnet.add(Merge([t1, t2], mode='sum'))
critnet.add(Activation('relu'))
# Create the net using the inputs and action placeholder
net = critnet([inputs, action])
keras 中的代码表现不同。如何在 keras 中合并两层以获得与 tflearn 中相同的结果?
【问题讨论】:
-
不确定你所说的行为不同是什么意思,但两个代码非常不同。首先,您在 tflearn 代码中添加一个偏差项(在 keras 代码中不存在)。然后,tflearn 代码以 ReLU 激活结束(可以简单地使用 .add(activation('relu') ) 将其添加到 keras)。而“使用两个临时层来获得相应的权重和偏差”的评论对我来说毫无意义。
-
谢谢。确实缺少relu激活层。我已经添加了它。加上注释''Use two temp layers to get对应的权重和偏差''表示我们可以使用t1和t2的权重和偏差将它们组合起来形成一个新的层。在 Keras 中,我们可以使用 Merge 合并图层,如代码所示,但是有一些方法可以像在 tflearn 代码中那样合并它们。
标签: tensorflow keras tflearn