user1984382
user1984382

Reputation: 33

Shape mismatch with Tensorflow Dataset and Network

I am getting an error relating to shapes whilst defining a very simple network using Tensorflow 2.

My code is:

import tensorflow as tf
import pandas as pd

data = pd.read_csv('data.csv')

target = data.pop('result')
target = tf.keras.utils.to_categorical(target.values, num_classes=3)
data_set = tf.data.Dataset.from_tensor_slices((data.values, target))

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=data.shape[1:]),
    tf.keras.layers.Dense(12, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy')
model.fit(data_set, epochs=5)

The call to fit() throws the following error:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 12 but received input with shape [12, 1]

Walking through the code:

  1. The input CSV file has thirteen columns - with the last being the label
  2. This is converted to a 3 bit one-hot encoding
  3. The Dataset is constructed of two Tensors - one of shape (12,) and the other of shape (3,)
  4. The network Input layer defines it's expected shape as be the value data shape ignoring the first axis which is the batch size

I am stumped about why there is mismatch between the shape of the data and the expected data shape for the network - especially as the latter is defined by reference to the former.

Upvotes: 1

Views: 208

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36714

Add .batch() at the end of the dataset:

data_set = tf.data.Dataset.from_tensor_slices((data.values, target)).batch(8)

Upvotes: 1

Related Questions