我在使用任何张量流RNNCell的子类时遇到问题。根据tensorflow的来源,任何RNNCell的状态都应该是一个元组,但是当我给它一个元组时,它抛出一个错误,表明它正在尝试询问状态元组的ndims属性,该属性不存在。 / p>
我正在尝试创建一个LSTM,以便可以控制每个单独的输入。
这是我可以做的最简单的代码,即使有同样的问题,所以我希望我做的事情很容易解决。这是简单的代码:
lstm_layer = tf.contrib.rnn.LSTMCell(num_units = 64)
initial_state = lstm_layer.zero_state(batch_size=1,dtype=tf.float32)
initial_input = np.expand_dims(np.array([1,2,3,4,5,6,7,8]),0)
output_single, state_single = lstm_layer(inputs=initial_input,state=initial_state)
这是我得到的错误:
AttributeError Traceback (most recent call last)
<ipython-input-22-1dcce10906e5> in <module>
2 initial_state = lstm_layer.zero_state(batch_size=1,dtype=tf.float32)
3 initial_input = np.expand_dims(np.array([1,2,3,4,5,6,7,8]),0)
----> 4 output_single, state_single = lstm_layer(inputs=initial_input,state=initial_state)
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py in __call__(self, inputs, state, scope, *args, **kwargs)
369 # method. See the class docstring for more details.
370 return base_layer.Layer.__call__(self, inputs, state, scope=scope,
--> 371 *args, **kwargs)
372
373
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/layers/base.py in __call__(self, inputs, *args, **kwargs)
528
529 # Actually call layer
--> 530 outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
531
532 if not context.executing_eagerly():
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
536 if not self.built:
537 # Build layer if applicable (if the `build` method has been overridden).
--> 538 self._maybe_build(inputs)
539 # We must set self.built since user defined build functions are not
540 # constrained to set self.built.
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
1589 # Check input assumptions set before layer building, e.g. input rank.
1590 input_spec.assert_input_compatibility(
-> 1591 self.input_spec, inputs, self.name)
1592 input_list = nest.flatten(inputs)
1593 if input_list and self._dtype is None:
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
107 spec.min_ndim is not None or
108 spec.max_ndim is not None):
--> 109 if x.shape.ndims is None:
110 raise ValueError('Input ' + str(input_index) + ' of layer ' +
111 layer_name + ' is incompatible with the layer: '
AttributeError: 'tuple' object has no attribute 'ndims'
看来keras API中的所有层都有一些基本方法被调用,但是不适用于元组。但是,我感到奇怪的是,这将是一个前所未有的问题。所以我希望只是我犯错了
答案 0 :(得分:0)
我发现了问题。在这种情况下,Tensorflow与numpy的配合不好。代替
initial_input = np.expand_dims(np.array([1,2,3,4,5,6,7,8]),0)
我需要给它
initial_input = tf.expand_dims(np.array([1,2,3,4,5,6,7,8],dtype=np.float32),0)