0
votes

I know this question is asked before but i was unable to get my ans out of them.
So

   state is [[  0.2]
     [ 10. ]
     [  1. ]
     [-10.5]
     [ 41.1]]

and

    (5, 1) # np.shape(state)

and when model.predict(state) it throw

ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (5, 1)

But....

model = Sequential()
model.add(Dense(5,activation='relu',input_shape=(5,1)))

My first layer of model have input_shape=(5,1) which is equal to the shape of state that I am passing.
I also have 2 dense more layers after this.


And
print(model.summary()) 
// output 
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 5, 5)              10        
_________________________________________________________________
dropout_1 (Dropout)          (None, 5, 5)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 5, 5)              30        
_________________________________________________________________
dropout_2 (Dropout)          (None, 5, 5)              0         
_________________________________________________________________
dense_3 (Dense)              (None, 5, 3)              18        
=================================================================


And model definition is (!!noob alert )
model = Sequential()
model.add(Dense(5,activation='relu',input_shape=(5,1)))
model.add(Dropout(0.2))
# model.add(Flatten())

model.add(Dense(5,activation='relu'))
model.add(Dropout(0.2))
# model.add(Flatten())

model.add(Dense(3,activation='softmax'))

model.compile(loss="mse", optimizer=Adam(lr=0.001), metrics=['accuracy'])
1

1 Answers

3
votes

A couple things. First, the predict function assumes the first dimension of the input tensor is the batch size (even if you're only predicting for one sample), but the input_shape attribute on your first layer in the Sequential model excludes batch size, as indicated here. Second, the dense layers are being applied over the last dimension, which is not going to give you what you want since I assume your input vector has 5 features, but you are adding this last 1 dimension which makes your model outputs the wrong size. Try the following code:

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam

model = Sequential()
model.add(Dense(5, activation='relu', input_shape=(5,)))
model.add(Dropout(0.2))
model.add(Dense(5,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(3,activation='softmax'))
model.compile(loss="mse", optimizer=Adam(lr=0.001), metrics=['accuracy'])
print(model.summary())

state = np.array([0.2, 10., 1., -10.5, 41.1])  # shape (5,)
print("Prediction:", model.predict(np.expand_dims(state, 0)))  # expand_dims adds batch dimension

You should see this output for model summary and also be able to see a predicted vector:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 5)                 30        
_________________________________________________________________
dropout (Dropout)            (None, 5)                 0         
_________________________________________________________________
dense_1 (Dense)              (None, 5)                 30        
_________________________________________________________________
dropout_1 (Dropout)          (None, 5)                 0         
_________________________________________________________________
dense_2 (Dense)              (None, 3)                 18        
=================================================================
Total params: 78
Trainable params: 78
Non-trainable params: 0