Keras:张量对象没有属性“_keras_history"; [英] Keras: Tensor object has no attribute "_keras_history"

查看:61
本文介绍了Keras:张量对象没有属性“_keras_history";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]

错误输出:

   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}).来自 Lambda 的文档:

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

arguments:要传递给的关键字参数的可选字典功能.

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

例如

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

<小时>

这是一个关于测试这一层的更完整的示例:

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

这篇关于Keras:张量对象没有属性“_keras_history";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆