Skip to content Skip to sidebar Skip to footer

Keras Sequential Model Input Shape

I want to train a neural net based on a numpy array with 4 entries as the X-data and another array with one entry as the y-data. X_train = [x1, x2, x3, x4] y_train = [y1] A rather

Solution 1:

Input shape always expect the batch size as first dimention.

For example in your case, the following layer does not expect an array of shape (4,)

Dense(64, input_dim=4, activation='relu')

The input shape of this dense layer is a tensor of shape (n, 4) where n is the batch size.

To pass your observation to the model you first need to expand its dims as follows:

observation = np.asarray(observation)
observation = np.expand_dims(observation, axis=0) # From shape (4,) to (1, 4)
estimator.fit(observation, action)

Your code should look like this.

# creating a deep learning model with keras
def build_model():
    model = Sequential()

    model.add(Dense(64, input_dim=4, activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(16, activation='relu'))

    model.add(Dense(1, activation='sigmoid'))

    model.compile(Adam(lr=lr, decay=decay), loss='mse')
    model.summary()
    return model

model = build_model()

# running the game
for i_episodes in range(200):
    env.reset()
    for i in range(100):
        env.render()
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)

        # observation = ndarray float64
        # reward = float
        # done = bool
        # action = int
        # info = empty

        observation = np.asarray(observation)
        reward = np.asarray(reward)
        action = np.asarray(action)

        model.fit(np.expand_dims(observation, axis=0), np.expand_dims(action, axis=0))

Also if you are learning DQN check out this article

Solution 2:

Try this piece of code. You have to specify Input dimension when you are going to solve any regression problem with the neural net. So In input dimension you have to pass the number of columns you want to feed to your network.

  def baseline_model():  
    model = Sequential()

    model.add(Dense(64, input_dim=4, activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(16, activation='relu'))

    model.add(Dense(1, activation='sigmoid'))

    model.compile(Adam(lr=lr, decay=decay), loss='mse')
    model.summary()
    return model

Now You have to wrap it in the class keras regressor so that keras will know it is a regression problem you are going to solve.

estimator = KerasRegressor(build_fn=baseline_model, epochs=30, batch_size=3, verbose=1)

if you need more about how to solve Regression problem with keras than see the my notebook below you will get help from it.

Also use this line before calling Fit fucntion

(observation=observation.reshape(1,4))

Link : Solve Regression problem with Keras Neural Network

Solution 3:

Bro! For the second error use this code. Now its running fine for me.

X=[]
y=[]

# creating the environment
env = gym.make('CartPole-v1')

#defining global variables
lr=0.0001
decay=0.001
batch_size=None

# creating a deep learning model with kerasdef model():
    model = Sequential()

    model.add(Dense(64, input_dim=4, activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(16, activation='relu'))

    model.add(Dense(1, activation='sigmoid'))

    model.compile(Adam(lr=lr, decay=decay), loss='mse')
    model.summary()
    return model

# running the gamefor i_episodes inrange(200):
    env.reset()
    for i inrange(100):
        #env.render()
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)


        observation = np.asarray(observation)
        reward = np.asarray(reward)
        action = np.asarray(action)

        X.append( observation)
        y.append(action)


        if done:
            break
env.close()


X=np.asarray(X)
y=np.asarray(y)
estimator = KerasRegressor(build_fn=model, epochs=30, batch_size=3, verbose=1)
estimator.fit(X, y)

Post a Comment for "Keras Sequential Model Input Shape"