繁体   English   中英

Keras:Tensor对象没有属性“_keras_history”

[英]Keras: Tensor object has no attribute “_keras_history”

类似先前问题的大多数答案都建议将有问题的张量包装在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'

您可以使用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)])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM