python - 在构建 NodeDef 时,输入“ref”传递了 int32 预期的 ref 类型
问题描述
我想将 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)
也是因为 Ecol,所以我用 y(2-d) 和 tensors1 替换了 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 层调用了该函数两次。
有两个图被构建。一个用于训练。另一个用于验证。你不应该使用一些全局变量来保存一些内部状态。创建一个输出两个张量的自定义层。
推荐阅读
- javascript - 在 Javascript 中查找对象列表
- javascript - 如何打印属性名称而不是整个 JS 对象?
- c++ - 如何将 A 到 Z 作为变量
- pip - 人工延迟
- bash - 这两个 bash 片段是否等效,如果不是,哪个更好?
- .net - 将引用的组件(类型、方法等)从它们的程序集中提取到目标程序集中
- android - Gradle 同步失败:java.lang.StackOverflowError
- r - R:残差数百个变量的最快方法
- python - 在一个数组中创建 100 个字符串(python)
- excel - 在 ms Excel 中制作可玩迷宫:在特定单元格中禁用按钮