Keras 报告 TypeError: 不支持的操作数类型 +: 'NoneType' 和 'int' [英] Keras reports TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

查看:19
本文介绍了Keras 报告 TypeError: 不支持的操作数类型 +: 'NoneType' 和 'int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Keras 的初学者,只是编写了一个玩具示例.它报告 TypeError.代码和错误如下:

I'm a beginner in Keras and just write a toy example. It reports a TypeError. The code and error are as follows:

代码:

inputs = keras.Input(shape=(3, ))

cell = keras.layers.SimpleRNNCell(units=5, activation='softmax')
label = keras.layers.RNN(cell)(inputs)

model = keras.models.Model(inputs=inputs, outputs=label)
model.compile(optimizer='rmsprop',
              loss='mae',
              metrics=['acc'])

data = np.array([[1, 2, 3], [3, 4, 5]])
labels = np.array([1, 2])
model.fit(x=data, y=labels)

错误:

Traceback (most recent call last):
    File "/Users/david/Documents/code/python/Tensorflow/test.py", line 27, in <module>
        run()
    File "/Users/david/Documents/code/python/Tensorflow/test.py", line 21, in run
        label = keras.layers.RNN(cell)(inputs)
    File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/layers/recurrent.py", line 619, in __call__
...
    File "/Users/david/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py", line 473, in __call__
        scale /= max(1., (fan_in + fan_out) / 2.)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

那我该怎么处理呢?

推荐答案

RNN 层的输入将具有 (num_timesteps, num_features) 的形状,即每个样本由 num_timesteps 组成 时间步长,其中每个时间步长是一个长度为 num_features 的向量.此外,时间步数(即num_timesteps)可以是可变的或未知的(即None),但特征的数量(即num_features)应该从一开始就固定和指定.因此,您需要更改 Input 层的形状以与 RNN 层保持一致.例如:

The input to a RNN layer would have a shape of (num_timesteps, num_features), i.e. each sample consists of num_timesteps timesteps where each timestep is a vector of length num_features. Further, the number of timesteps (i.e. num_timesteps) could be variable or unknown (i.e. None) but the number of features (i.e. num_features) should be fixed and specified from the beginning. Therefore, you need to change the shape of Input layer to be consistent with the RNN layer. For example:

inputs = keras.Input(shape=(None, 3))  # variable number of timesteps each with length 3
inputs = keras.Input(shape=(4, 3))     # 4 timesteps each with length 3
inputs = keras.Input(shape=(4, None))  # this is WRONG! you can't do this. Number of features must be fixed

然后,您还需要更改输入数据的形状(即 data),以与您指定的输入形状一致(即它必须具有 (num_samples, num_timesteps, num_features)).

Then, you also need to change the shape of input data (i.e. data) as well to be consistent with the input shape you have specified (i.e. it must have a shape of (num_samples, num_timesteps, num_features)).

作为旁注,您可以通过直接使用 SimpleRNN 层来更简单地定义 RNN 层:

As a side note, you could define the RNN layer more simply by using the SimpleRNN layer directly:

label = keras.layers.SimpleRNN(units=5, activation='softmax')(inputs)

这篇关于Keras 报告 TypeError: 不支持的操作数类型 +: 'NoneType' 和 'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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