简体   繁体   中英

Keras: Tensor object has no attribute “_keras_history”

Most of the answers to similar previous questions have suggested wrapping the problematic tensor in a Lambda layer. I've already done this, however (and tried many variations on the fix), and it's still throwing the same error. Pseudocode for my current model definition looks like this:

# [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]

Error output:

   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'

You can provide low and high to the layer with Lambda(..., arguments={'low': low, 'high': high}) . From the documentation of Lambda :

arguments: optional dictionary of keyword arguments to be passed to the function.

For example,

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

EDIT:

Here's a more complete sample on testing this layer:

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)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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