Keras input dim error

591 views Asked by At

while Iam experimenting with keras and Gym of Openai and I keep getting this error

ValueError: Error when checking input: expected reshape_1_input to have shape (None, 979, 1) but got array with shape (979, 1, 1)

I gather my Data as follow:

def getData():
    rewardc = 0
    rewardo = 0
    labels = np.array([])
    data = np.array([])
    for i in range(11):
        print("run",i)
        for _ in range (10000):
            print("---------------------------------------------------------------------------")
            print("action", _)
            #env.render()
            action = env.action_space.sample()
            observation, reward, done, info = env.step(action)
            if done:
                env.reset()
                break
            rewardc = rewardo - reward
            rewardo = reward
            observationo = observation
            rewardco = rewardc
            ohobservation = np.array(observationo)
            ohobservation = np.append(ohobservation, rewardo)
            ohobservation = np.append(ohobservation, rewardco)
            #print ("whole observation",ohobservation)
            #print("data", data)
            labelsb = np.array([action])
            if labels.size == 0:
                labels = labelsb
            else:
                labels = np.vstack((labels,action))
            if data.size == 0:
                data = ohobservation
            else:
                data = np.vstack((data, ohobservation))

    return labels, data

My x array will look like that:

[ [2]  [0]  [2]  [3]  [0]  [0]  ..  [2]  [3]]

My Y:

  Y [[  1.15792274e-02   9.40991027e-01   5.85608387e-01 ...,   0.00000000e+00
   -5.27112172e-01   5.27112172e-01]
 [  1.74466133e-02   9.40591342e-01   5.95346880e-01 ...,   0.00000000e+00
   -1.88372436e+00   1.35661219e+00]
 [  2.32508659e-02   9.39789397e-01   5.87415648e-01 ...,   0.00000000e+00
   -4.41631844e-02  -1.83956118e+00]

Network Code:

model = Sequential()
    model.add(Dense(units= 64,  input_dim= 100))
    model.add(Activation('relu'))
    model.add(Dense(units=10))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])
    model.fit(X,Y, epochs=5)

But I cannot feed it in Keras at any Chance. It would be awesome if somebody could help me solve it thank you!

1

There are 1 answers

19
Daniel Möller On

Inputs

If your data is 979 examples, each example containing one element, make sure that its first dimension is 979

print(X.shape) #confirm that the shape is (979,1) or (979,)

If the shape is different from that, you will have to reshape the array, because the Dense layer expects shapes in those forms.

X = X.reshape((979,))

Now, make sure that your Dense layer is compatible with that shape:

 #using input_dim:
 Dense(units=64, input_dim=1) #each example has only one element

 #or, using input_shape:
 Dense(units=64, input_shape=(1,)) #input_shape must always be a tuple. Again, the number of examples shouldn't be a part of this shape

This will solve the problems you have with the inputs. All the error messages you get like this one are about the compatibility between your input data and the input shape you gave to the first layer:

Error when checking input: expected reshape_1_input to have shape (None, 979, 1) 
but got array with shape (979, 1, 1)

The first shape in the message is the input_shape you passed to the layer. The second is the shape of your actual data.

Outputs

The same compatibility is necessary for Y, but now with the last layer.

If you put units=10 in the last layer, it means your labels must be of shape (979,10).

If your labels don't have that shape, adjust the number of cells to match it.