我想乘以2个张量,所以我在Keras中使用lambda层,并使用目标2个张量作为lambda层的输入,如下所示
def get_col_att(tensors):
for i in range(num_samples):
global t
t=tf.assign(t,i)
x = tf.nn.embedding_lookup(tensors[0], t)
print("tensors[1]:",tensors[1])
y = tf.nn.embedding_lookup(tensors[1], t)
print("x shape",x.shape,"y shape",y.shape)
ab=tf.transpose(y)
Ecol=tf.reshape(tf.tensordot(x,ab,axes=1),[1,M,C])
if i==0:
all_col_attention=tf.Variable(initial_value=Ecol)
else:
all_col_attention=tf.concat([all_col_attention,Ecol],0)
print("all_col_attention",all_col_attention)
return all_col_attention
total_alpha_sel_np=Lambda(get_col_att)([Hq,cols_last_hidden])
但它给出了以下错误
Input 'ref' passed int32 expected ref type while building NodeDef
我找出了错误在哪里,在下面一行
all_col_attention=tf.Variable(initial_value=Ecol)
你知道吗 也因为经济合作 所以我用y(2-d)和张量1代替了Ecol(3-d)。它适用于张量[1],但不适用于y
x shape (13, 80) y shape (12, 80)
tf.tensordot(x,ab,axes=1) Tensor("lambda_42/Reshape:0", shape=(1, 13, 12), dtype=float32)
x shape (13, 80) y shape (12, 80)
tf.tensordot(x,ab,axes=1) Tensor("lambda_42/Reshape_2:0", shape=(1, 13, 12), dtype=float32)
x shape (13, 80) y shape (12, 80)
tf.tensordot(x,ab,axes=1) Tensor("lambda_42/Reshape_4:0", shape=(1, 13, 12), dtype=float32)
all_col_attention Tensor("lambda_42/concat_1:0", shape=(3, 13, 12), dtype=float32)
x shape (13, 80) y shape (12, 80)
tf.tensordot(x,ab,axes=1) Tensor("lambda_42/Reshape_6:0", shape=(1, 13, 12), dtype=float32)
请在这方面帮助我:-(
如我所见,问题不在于张量的形状。该错误与lambda层有关。Keras中的lambda层调用函数两次,因为有训练和验证图。你知道吗
有两个图被构造。一个用来训练。另一个用于验证。 你不应该使用一些全局变量来保存一些内部状态。 创建一个输出两个张量的自定义层。你知道吗
Reference
相关问题 更多 >
编程相关推荐