similar之前问题的大部分答案都建议将有问题的张量包装在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'
答案 0 :(得分:2)
您可以使用high
向图层提供Lambda(..., arguments={'low': low, 'high': high})
和Lambda
。来自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)])
修改强>
以下是测试此图层的更完整示例:
{{1}}