Tensorflow-TypeError:'int'对象不可迭代 [英] Tensorflow - TypeError: 'int' object is not iterable
问题描述
我遇到了一个错误,但是它被埋在TensorFlow库中,所以我正在努力弄清楚我的模型出了什么问题.
I'm getting an error but it's buried down in the TensorFlow library so I'm struggling to figure out what's wrong with my model.
我正在尝试将RNN与LSTM结合使用.我的模型如下:
I'm trying to use an RNN with LSTM. My model looks like this:
model = Sequential()
model.add(LSTM(128, activation='relu',
input_shape=1000, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(2, activation='softmax'))
opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=3, validation_data=(x_test, y_test))
我的训练数据是一个列表列表,每个列表包含1000个浮点数.例如,x_train[0] =
My training data is a list of lists each comprised of 1000 floats. For example, x_train[0] =
[0.0, 0.0, 0.1, 0.25, 0.5, ...]
我收到此错误:
File "C:\Users\bencu\Desktop\ProjectFiles\Code\Program.py", line 74, in FitModel
input_shape=1000, return_sequences=True))
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\layers\recurrent_v2.py", line 881, in __init__
**kwargs)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\layers\recurrent.py", line 1007, in __init__
super(DropoutRNNCellMixin, self).__init__(*args, **kwargs)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\layers\recurrent.py", line 2541, in __init__
**kwargs)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\layers\recurrent.py", line 395, in __init__
super(RNN, self).__init__(**kwargs)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 356, in __init__
batch_input_shape = (batch_size,) + tuple(kwargs['input_shape'])
TypeError: 'int' object is not iterable
我对ML很陌生,所以如果有人可以弄清楚我要去哪里错了,那将不胜感激.谢谢.
I'm pretty new to ML so if someone could figure out where I'm going wrong that would be much appreciated. Thank you.
推荐答案
Keras期望input_shape
始终是 tuple ;对于单个值,它看起来像(1000,)
.
Keras expects input_shape
to always be a tuple; for a single value, it'd look like (1000,)
.
但是,对于LSTM,预期的完整形状(batch_shape
)为:(num_samples, timesteps, num_channels)
-或等效地为(batch_size, timesteps, features)
. input_shape
就是batch_shape
,没有维度0-即(timesteps, num_channels)
.如果您输入的数据是单变量(例如1D序列),则num_channels=1
-因此:
For LSTM, however, the expected full shape (batch_shape
) is: (num_samples, timesteps, num_channels)
- or equivalently, (batch_size, timesteps, features)
. input_shape
is simply batch_shape
without dimension 0 - i.e., (timesteps, num_channels)
. If your input data is univariate (e.g. 1D sequence), then num_channels=1
- thus:
model.add(LSTM(128, activation='relu', input_shape=(1000, 1), return_sequences=True))
最后,对于'binary_crossentropy'
,更好的输出层将是Dense(1, activation='sigmoid')
.有关更多信息,请参见此答案.
Lastly, for 'binary_crossentropy'
, a better output layer would be Dense(1, activation='sigmoid')
. For more info, see this answer.
提示:为确保正常,请运行print(x_train.shape)
,并确保除first(第0维)以外的所有值都与您的input_shape
相匹配.但是,我建议始终使用batch_shape
而不是input_shape
,除非应用程序涉及可变的批处理大小-这样会使调试更加容易.
Tip: to be sure, run print(x_train.shape)
, and make sure all the values except first (dim 0) match your input_shape
. I'd recommend, however, to always use batch_shape
over input_shape
, unless the application involves variable batch sizes - it makes debugging much easier.
对于您的一维示例,如果它返回类似(32, 1000)
的内容,则需要添加尺寸以使其成为3D:x_train = np.expand_dims(x_train, -1)
(-1 =最后一个轴)
For your 1D example, if it returns something like (32, 1000)
, you'll need to add a dimension to make it 3D: x_train = np.expand_dims(x_train, -1)
(-1 = last axis)
这篇关于Tensorflow-TypeError:'int'对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!