Keras + Tensorflow:'ConvLSTM2D'对象没有属性'outbound_nodes' [英] Keras + Tensorflow: 'ConvLSTM2D' object has no attribute 'outbound_nodes'

查看:948
本文介绍了Keras + Tensorflow:'ConvLSTM2D'对象没有属性'outbound_nodes'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ConvLSTM作为运行中的tensorflow网络的一部分,因为我在使用tensorflow ConvLSTM实现时遇到了一些问题,因此我选择使用ConvLSTM2D Keras层.

I’m trying to have a ConvLSTM as part of my functioning tensorflow network, because I had some issues with using the tensorflow ConvLSTM implementation, I settled for using the ConvLSTM2D Keras Layer instead.

为了在我的Tensorflow会话中提供Keras,我使用了博客文章建议(我正在使用Tensorflow后端): https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html

To make Keras available in my Tensorflow session I used the blogposts suggestion (I’m using the Tensorflow backend): https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html

import tensorflow as tf
sess = tf.Session()

from keras import backend as K
K.set_session(sess)

我的代码段(导致问题的原因):

A snippet of my code (that what causes the issues):

# state has a shape of [1, 75, 32, 32] with batchsize=1
state = tf.concat([screen, screen2, non_spatial], axis=1)

# Reshaping state to get time=1 to have the right shape for the ConvLSTM
state_reshaped = tf.reshape(state, [1, 1, 75, 32, 32])

# Keras ConvLSTM2D Layer
# I tried leaving out the batch_size for the input_shape but it didn't make a difference for the error and it seems to be fine
lstm_layer = ConvLSTM2D(filters=5, kernel_size=(3, 3), input_shape=(1, 1, 75, 32, 32), data_format='channels_first', stateful=True)(state_reshaped)

fc1 = layers.fully_connected(inputs=layers.flatten(lstm_layer), num_outputs=256, activation_fn=tf.nn.relu)

这给了我以下错误: AttributeError: 'ConvLSTM2D' object has no attribute 'outbound_nodes’

This gives me the following error: AttributeError: 'ConvLSTM2D' object has no attribute 'outbound_nodes’"

我不知道这意味着什么.我认为这可能与混合Keras ConvLSTM和张量流扁平化有关.所以我尝试像这样使用Keras Flatten():

I have no idea what this means. I thought it might has to do with mixing Keras ConvLSTM and tensorflows flatten. So I tried using Keras Flatten() instead like this:

# lstm_layer shape is (5, 5, 30, 30)
lstm_layer = Flatten(data_format='channels_first')(lstm_layer)

fc1 = layers.fully_connected(inputs=lstm_layer, num_outputs=256, activation_fn=tf.nn.relu)

,并出现以下错误:ValueError: The last dimension of the inputs to 'Dense' should be defined. Found 'None'. 该错误是由于Flatten()导致的,无论出于何种原因,其输出形状为(?, ?),并且完全连接的层需要为最后一个尺寸定义形状,但是我不明白为什么它不确定.它是在之前定义的. 相反,使用Reshape((4500,))(lstm_layer)会给我同样的no attribute 'outbound_nodes'错误.

and got the following error: ValueError: The last dimension of the inputs to 'Dense' should be defined. Found 'None'. This error is caused by Flatten(), for whatever reason, having an output shape of (?, ?) and the fullyconnected layer needing to have a defined shape for the last dimension but I don't understand why it would be undefined. It was defined before. Using Reshape((4500,))(lstm_layer) instead gives me the same no attribute 'outbound_nodes' error.

我搜索了这个问题,虽然不是唯一的问题,但找不到解决方法.

I googled the issue and I seem to not be the only one but I couldn't find a solution.

如何解决此问题? Flatten()的未知输出形状是bug还是所需的行为,如果是这样,为什么?

How can I solve this issue? Is the unknown output shape of Flatten() a bug or wanted behavior, if so why?

推荐答案

我遇到了同样的问题,并且对tensorflow代码有所了解.问题在于Keras 2.2.0进行了一些重构,而tf.keras尚未更新到该新API.

I encountered the same problem and had a bit of a dig into the tensorflow code. The problem is that there was some refactoring done for Keras 2.2.0 and tf.keras hasn't yet been updated to this new API.

在Keras 2.2.0中,"outbound_nodes"属性已重命名为"_outbound_nodes".它很容易修复,base.py中有两个引用需要更新:

The 'outbound_nodes' attribute was renamed to '_outbound_nodes' in Keras 2.2.0. It's pretty easy to fix, there's two references in base.py you need to update:

/site-packages/tensorflow/python/layers/base.py

/site-packages/tensorflow/python/layers/base.py

更新后对我来说很好.

这篇关于Keras + Tensorflow:'ConvLSTM2D'对象没有属性'outbound_nodes'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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