Vishwad
Vishwad

Reputation: 231

Tensorflow Input Dimension mismatch problem

I have created an LSTM-NN. I am passing an input but I get the error:
ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=1. Full shape received: (7,)

To resolve this, I have referred to a stack overflow post that mentions use of the argument: input_shape. I still have not been able to resolve my problem due to a lack of understanding. Kindly help. Here is my code

# This is the definition of the model
class LSTMmodel(tf.Module):
    def __init__(self, arg_name=None):
        super().__init__(name=arg_name)
        self.__input = tf.Variable(initial_value=[0 for x in range(7)])
        self.__network = tf.keras.layers.LSTM(units=7, input_shape=(7,))
        self.__output = tf.Variable(initial_value=[0 for x in range(7)])
    @tf.function
    def train(self, arg_data_train, labels, learning_rate):
        with tf.GradientTape() as t:
            self.__input = tf.Variable(initial_value=[0 for x in range(7)])
            self.__output = self.__network(self.__input)
            loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=None, logits=None)
            dw, db = t.gradient(loss, [self.w, self.b])
        self.w.assign_sub(learning_rate * dw)
        self.b.assign_sub(learning_rate * db)

    @tf.function
    def __call__(self, arg_input=[0 for x in range(7)]):
        self.__input = tf.Variable(arg_input)
        self.__output = self.__network(self.__input)
        return self.__output
# This is the input I provide for training where the problem occurs.
# The two vars ````cgm```` and ````labels```` are length 9222 lists.
# Each element of the list is a list with length 7 filled with only integers.
modela = LSTMmodel(arg_name='namea')
modela.train(cgm ,labels, 0.4)
```

Upvotes: 2

Views: 277

Answers (1)

yudhiesh
yudhiesh

Reputation: 6829

An LSTM takes as input 3D tensors and you are passing in 1D tensors so you need to reshape it to the appropriate shape.

self.__input = tf.Variable(initial_value=[0 for x in range(7)])
self.__input_reshaped = tf.reshape(self.__input, [1, 7, 1]) # shape of (1, 7, 1)

You will also need to change the input_shape of the LSTM.

self.__network = tf.keras.layers.LSTM(units=7, input_shape=(7,1))

Then pass the reshaped input to the network to get the output.

self.__output = self.__network(self.__input_reshaped)

Upvotes: 1

Related Questions