Ignacio Valenzuela
Ignacio Valenzuela

Reputation: 137

What shape should I use in Keras Input class?

I'm trying to run a simple neural network and I've gotten to the point where my features are flat using the following code:

training_dataset = (
tf.data.Dataset.from_tensor_slices(
    (
        tf.cast(ballast_train[features].values, tf.float64),
        tf.cast(ballast_train[target].values, tf.int32)
    )
)

)

for features_tensor, target_tensor in training_dataset:
    print(f'features:{features_tensor} target:{target_tensor}')

features:[0.46029711 0.33290338 0.78302964 0.10295655 0.5890411 ] target:5
features:[0.63530873 0.90712946 0.27781778 0.10295655 0.45988258] target:5
features:[0.68413444 0.81390713 0.8448272  0.65073914 0.46771037] target:2

Now, I'm trying to run the following code but I'm not able to get the tf.keras.Input() part of the code right.

`inputs = tf.keras.Input(shape=(5,))
x = tf.keras.layers.Dense(100, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(15, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)

model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['accuracy'])

model.fit(training_dataset, epochs=5)`

When trying to fit the model, this error occurs:

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

What should go in the "shape" parameter? Is there something I'm missing here?

Upvotes: 1

Views: 52

Answers (1)

Leland Hepworth
Leland Hepworth

Reputation: 996

This question has a similar issue, and it may work in your case too. Try the following to see if it works:

model.fit(training_dataset.batch(batch_size), epoch=5)

where you will need to provide a value for batch_size

If that doesn't work, I always tend to provide my features and samples to the separate arguments x and y in tf.keras.Model.fit. You could try modifying your code to do that instead of combining them in the dataset.

Upvotes: 1

Related Questions