Tensorflow - TypeError: 'int' object is not iterable
Asked Answered
F

3

1

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.

Frigorific answered 31/10, 2019 at 0:45 Comment(0)
R
3

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)

Rubino answered 31/10, 2019 at 0:56 Comment(4)
Thank you so much. This fixed the original problem. However, I'm now getting this error: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).Frigorific
@Cutter You're welcome. Sounds like an issue with your data formatting or install versions - regardless, it lies outside the scope of this question. If you open a new question on this error, you can reply here to notify me - I can take a look.Rubino
Thanks @OverLordGoldDragon. I tried investigating the issue myself with no success. I've made a follow-up question, if you could take a look that would be great :) #58636587Frigorific
It was an error for Tensorflow 1. For Tensorflow 2 (version: 2.4.1), the input shape of an int number is OK.Categorical
P
1

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.

Pam answered 31/10, 2019 at 0:53 Comment(0)
D
1

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

Desdamona answered 31/10, 2019 at 0:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.