'error while training the ml network in tensorflow; 'int' object is not subscriptable

I'm trying to train a CNN-LSTM coupled model named pred-rnn.

PredPP function is defined according to this.

First I try to defined the model.

model = tf.keras.models.Sequential()

inputs_shape = [n_samples, n_days, n_lat, n_lon, n_channels]

predpp_layer = PredPP(inputs_shape, n_channels,
                 num_layers, num_hidden,
                 ifn,
                 True,
                 )

model.add(predpp_layer)

When I want to train the model with model.fit:

# run the training
optimizer = tf.keras.optimizers.Adam(learning_rate=LR, name='Adam')
model.compile(optimizer=optimizer, loss='mse', metrics=['mse'])
results = model.fit(X_TRAIN, Y_TRAIN, validation_split=0.25, batch_size=1, epochs=64, verbose=1, )

I get this error:

~/ParFlow-NN-master/src/parflow_nn/layers/CausalLSTMCell.py in __init__(self, layer_name, filter_size, num_hidden_in, num_hidden_out, seq_shape, forget_bias, tln, initializer)
     23         self.num_hidden_in = num_hidden_in
     24         self.num_hidden = num_hidden_out
---> 25         self.batch = seq_shape[0]
     26         self.height = seq_shape[2]
     27         self.width = seq_shape[3]

TypeError: 'int' object is not subscriptable

Maybe this explanation is not enough to tell what is the source of such an error, but I'd appreciate if any of you could give me some insight about where to look for solving this error.



Solution 1:[1]

  • The error you got indicates that seq_shape as an int (and not a tuple/list), and that's why it can't be indexed with [0]
  • From the constructor stack trace, we can see that seq_shape is the 5th argument (.../CausalLSTMCell.py in __init__(self, ..., ..., ..., ..., seq_shape, ...)
  • Your code in line 56 passes self.shape to seq_shape
  • A few lines above, at line 45 you initialize self.shape = input_shape[0]
  • If the input shape is a tensorflow shape, it should be a tuple. Indexing it with [0] would indeed produce an int (the size of the first dimension)
  • I don't know what should've been there, but it's probably not [0] :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barak Itkin