当我尝试在 eager 模式下使用 tensorflow 重写dynet 项目时,出现以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute ConcatV2 as input #1 was expected to be a float tensor but is a int32 tensor [Op:ConcatV2] name: concat
我试图定位错误并简化代码,然后发现在 Eager 模式下在一个动态图中计算两个嵌入时,会发生错误。
在静态图模式下添加两个嵌入没有错误。
with tf.Graph().as_default():
emb = tf.keras.layers.Embedding(10000, 50)
emb2 = tf.keras.layers.Embedding(10000, 50)
y_ = emb(tf.constant(100)) + emb2(tf.constant(100))
y = tf.ones((1, 50))
loss = tf.reduce_sum(y - y_)
optimizer = tf.train.MomentumOptimizer(0.2,0.5).minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
sess.run(fetches=[loss, optimizer])
但是当我在急切模式下运行以下代码时,发生了错误。
tfe.enable_eager_execution()
def loss(y):
emb = tf.keras.layers.Embedding(10000,50)
emb2 = tf.keras.layers.Embedding(10000,50)
y_ = emb(tf.constant(100)) + emb2(tf.constant(100))
return tf.reduce_sum(y - y_)
y = tf.ones((1, 50))
grads = tfe.implicit_gradients(loss)(y)
tf.train.MomentumOptimizer(0.2, 0.5).apply_gradients(grads)
急切模式下的代码有什么问题,如何计算急切模式下的两个嵌入?