Search code examples
tensorflowkerasartificial-intelligencereinforcement-learning

Dense layer give me 2D array


I am making reinforcement learning for CartPole and i meet this problem

model = keras.models.Sequential()
model.add(Dense(8,activation = 'relu'))
model.add(Dense(2,activation = 'linear')

this is my model

state = env.reset()
print(state)


output:
[-0.00315391 -0.0150189   0.01804181  0.02032083]

And this is what i got for prediction of my model

model.predict(state)

output:
[[-0.00028523  0.00031606]
 [-0.00135828  0.00150507]
 [ 0.00500827 -0.01125371]
 [ 0.00564091 -0.01267526]]

Why Dense(2,activation='linear') receiving 2D array?

I was expecting output with (2,1) shape but why model is receiving (2,4) shape?

I found that output has relationship between output node of model and input shape


Solution

  • The shape of state is (4,), so the model considers it as 4 samples (First dimension is the number of samples). If you want to pass 1 sample with 4 features, the shape should be (1,4) (1 sample, 4 features), to give you (1,2) (1 sample, 2 outputs).

    Pass it like this:

    model.predict(np.reshape(state,(1,-1)))
    
    # output e.g.  
    # array([[ 0.0078704 , -0.00879759]], dtype=float32) #(1,2)
    

    Note that your model should be built with the inputs with 4 features (e.g. input_shape=(4,)), otherwise you will get error. If you are just making prediction, run your model definition again.