Tensorflow - TypeError: 'int' object is not iterable

我们两清 提交于 2019-12-11 10:58:09

问题


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.

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

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, ...]

I'm getting this error:

   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

I'm pretty new to ML so if someone could figure out where I'm going wrong that would be much appreciated. Thank you.


回答1:


Keras expects input_shape to always be a tuple; for a single value, it'd look like (1000,).

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

Lastly, for 'binary_crossentropy', a better output layer would be Dense(1, activation='sigmoid'). For more info, see this answer.


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.

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)




回答2:


The argument input_shape is expected to be a tuple, even if the input tensor is one-dimensional. Use input_shape=(1000,) instead. Note that the comma is important to ensure that Python interpret it as a tuple, and not a single integer.




回答3:


The error message says: TypeError: 'int' object is not iterable. So, something is wrong with an int in our code, which causes an Error because it's the wrong Type.

The most recent line in the stack trace that's within our own code is:

model.add(LSTM(128, activation='relu', input_shape=1000, return_sequences=True))

The only ints here are 128 and 1000. The 128 is the number of units, so that's fine. The input_shape is not; if we're specifying the "shape" of a numpy (or similar) array, then we need a sequence of values - one for the size of each dimension. Yes, even for a one-dimensional input.

We specify that instead as (1000,).



来源:https://stackoverflow.com/questions/58635521/tensorflow-typeerror-int-object-is-not-iterable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!