Keras:张量对象没有“_keras_history”属性

3

大多数类似之前问题的答案建议将有问题的张量包装在Lambda层中。我已经这样做了,然而(并尝试了许多修复方案),它仍然抛出相同的错误。 我的当前模型定义的伪代码如下:

# [previous layers of model definition not shown here for simplicity]
out_duration = Reshape((30, 3))(out_duration)
out_position = Reshape((30, 3))(out_position)
low = tf.constant([(30x3) numpy array of lower bounds)]) # can't use K.clip here because the lower bound is different for every element
high = tf.constant([(30x3) numpy array of upper bounds)])
clipped_out_position = Lambda(lambda x: tf.clip_by_value(*x), output_shape=out_position.get_shape().as_list())([out_position, low, high])
model = Model(inputs=x, outputs=[out_duration, clipped_out_position]

错误输出:
   File "multitask_inverter.py", line 107, in <module>
    main()
  File "multitask_inverter.py", line 75, in main
    model = Model(inputs=x, outputs=[out_duration, clipped_out_position])
  File "/om/user/lnj/openmind_env/tensorflow-gpu/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/om/user/lnj/openmind_env/tensorflow-gpu/lib/python3.5/site-packages/keras/engine/topology.py", line 1705, in __init__
    build_map_of_graph(x, finished_nodes, nodes_in_progress)
  File "/om/user/lnj/openmind_env/tensorflow-gpu/lib/python3.5/site-packages/keras/engine/topology.py", line 1695, in build_map_of_graph
    layer, node_index, tensor_index)
  File "/om/user/lnj/openmind_env/tensorflow-gpu/lib/python3.5/site-packages/keras/engine/topology.py", line 1665, in build_map_of_graph
    layer, node_index, tensor_index = tensor._keras_history
AttributeError: 'Tensor' object has no attribute '_keras_history'
1个回答

2

您可以使用Lambda(..., arguments={'low': low, 'high': high})向层提供lowhigh。来自Lambda文档的说明:

arguments:可选关键字参数字典,用于传递给函数。

例如:

clipped_out_position = Lambda(lambda x, low, high: tf.clip_by_value(x, low, high),
                              arguments={'low': low, 'high': high})(out_position)

编辑:

这里是有关测试该层的更完整示例:

x = Input(shape=(100,))
out_duration = Dense(90)(x)
out_position = Dense(90)(x)
out_duration = Reshape((30, 3))(out_duration)
out_position = Reshape((30, 3))(out_position)

low = tf.constant(np.random.rand(30, 3).astype('float32'))
high = tf.constant(1 + np.random.rand(30, 3).astype('float32'))
clipped_out_position = Lambda(lambda x, low, high: tf.clip_by_value(x, low, high),
                              arguments={'low': low, 'high': high})(out_position)

model = Model(inputs=x, outputs=[out_duration, clipped_out_position])
model.compile(loss='mse', optimizer='adam')
model.fit(np.random.rand(5000, 100), [np.random.rand(5000, 30, 3), np.random.rand(5000, 30, 3)])

这会导致一个 ValueError: No data provided for "lambda_1". Need data for each key in: ['out_duration', 'lambda_1'] -- 我不太确定发生了什么。 - Jess
我认为你需要发布更多的代码。新问题不是来自你已发布的代码。你的模型长什么样子,你如何调用fit()函数? - Yu-Yang
我已经添加了一个使用此行的示例网络。你能将它与你的网络进行比较,并检查是否有任何不同吗?在我看来,你的错误是一个不同的问题,而不是原始问题,因为它是由 fit() 引发的错误,而不是模型构建。 - Yu-Yang
太棒了,它起作用了!您能解释一下为什么添加arguments参数是这种情况的正确修复方法吗? - Jess
我认为这是因为lowhigh不是Keras张量。 Keras张量在_keras_history中记录一些拓扑信息。当模型构建期间找不到该信息时,您将看到像那样的错误。要将不是Keras层输出的内容传递给Lambda层,可以使用arguments参数。 - Yu-Yang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接