TensorFlow ValueError: logits and labels must have the same shape ((25, 1) vs (1, 1))

Viewed 649

I have been building a classification model with 25 features and I keep getting this error code below, not sure if the error is coming from the construction of the data or the model? Could also be a problem with how I constructed the dataset tensors

ps super new to tensorflow plus stack overflow

data = np.array(pd.read_csv('DATASET_2018.csv'))




#creating labels 
labels = np.array([]) 

for i in range(len(data)):
    labels = np.append(labels, float(data[i][0]))
    
labels = labels.reshape(623)




#seperating data 
data_set = np.array([])

for i in range(len(data)):
    broken_features =  data[i][1][1:-1].split(',')
    dummy_data = [] 
    for x in broken_features:
        dummy_data.append(float(x))
    data_set = np.append(data_set, dummy_data)
    
data_set = data_set.reshape(623, 25)



#splitting data into testing and training data 

test_data = data_set[int(-len(data_set)*0.20):]
test_labels = labels[int(-len(data_set)*0.20):]

train_data = data_set[:int(-len(data_set)*0.20)]
train_labels = labels[:int(-len(data_set)*0.20)]

train_labels = np.asarray(train_labels).astype('float32').reshape((-1,1))
test_labels = np.asarray(test_labels).astype('float32').reshape((-1,1))




print(train_data.shape, train_labels.shape)


 (499, 25) (499, 1)
    



train_data = tf.random.shuffle(
    train_data, seed=None, name=None
)

data = tf.linalg.normalize(
    train_data, ord='euclidean', axis=None, name=None
)

train_dataset = tf.data.Dataset.from_tensor_slices((data[0], train_labels))

test_data = tf.linalg.normalize(
    test_data, ord='euclidean', axis=None, name=None
)

test_dataset = tf.data.Dataset.from_tensor_slices((test_data[0], test_labels))




def get_compiled_model():
    model = tf.keras.Sequential([
        tf.keras.layers.Dense(25, activation='relu'),
        tf.keras.layers.Dense(10, activation='relu'),
        tf.keras.layers.Dense(1)
    ])
    
    model.compile(optimizer='adam',
                 loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                 metrics=['accuracy'])
    return model 




model.summary()

Model: "sequential_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_35 (Dense)             (25, 25)                  50        
_________________________________________________________________
dense_36 (Dense)             (25, 10)                  260       
_________________________________________________________________
dense_37 (Dense)             (25, 1)                   11        
=================================================================
Total params: 321
Trainable params: 321
Non-trainable params: 0
_________________________________________________________________


model = get_compiled_model()
model.fit(train_dataset, epochs=15)


 ValueError: logits and labels must have the same shape ((25, 1) vs (1, 1))

Thanks for any help

1 Answers

The problem is that your inputs and labels have no batch dimension.

A simple fix is to add before the call to model.fit the lines

train_dataset = train_dataset.batch(1)
test_dataset = test_dataset.batch(1)

The model expects to receive data with shape (None, 25) and labels with shape (None, 1), but it receives data with shape (25,) and labels with shape (1,). When the loss is computed, Keras interprets 25 in the output shape (25, 1) as the batch size and similarly the first 1 in the label shape (1, 1) as a different batch size, so it gives you this error.

All the computation of the model is wrong without the batch size, in fact the output shapes are not the expected ones. They should be (25,), (10,) and (1,) disregarding the batch dimension.

Related