Shape Misalignment in TensorFlow input

Viewed 51

I have some data I'm trying to make a simple TensorFlow neural network for. I've preprocessed the data so its shape is (18631, 147), so around 18000 rows of 147 values each. each row is its own training data, and I have another dataframe with shape (18631, 3) that has the ground truths for three different variables I might try predicting, labeled 1, 2 and 3. When I try to run model.fit, I get a weird error I don't understand:

ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 147 but received input with shape (147, 1)

The way I try to prepare my data for input into TensorFlow is with the from_tensor_slices() function, specifically with the code:

#data is the (18631, 147) dataframe, target is the (18631, 3). 
lat_target = target.pop(1)
dataset = tensorflow.data.Dataset.from_tensor_slices((data.values,lat_target))

I then make my model with:

input_layer = tensorflow.keras.Input((147,))
first_layer = tensorflow.keras.layers.Dense(32)(input_layer)
second_layer = tensorflow.keras.layers.Dense(32)(first_layer)
last_layer = tensorflow.keras.layers.Dropout(.3)(second_layer)
pred_layer = tensorflow.keras.layers.Dense(1)(last_layer)
model = tensorflow.keras.Model(inputs=input_layer,outputs=pred_layer)
model.compile(optimizer="adam",loss=tensorflow.keras.losses.CategoricalCrossentropy(from_logits=True),metrics=['accuracy'])

and try to train with:

model.fit(dataset,epochs=20)

which is when I get the error message. I think the main problem is I don't understand what the difference between a shape with value 147 and an input with shape (147,1) is, but I'm also not sure I didn't make an obvious mistake somewhere else in the code.

1 Answers

Your input shape is (147,1) but you are feeding input= 147.

This should work

import tensorflow as tf
input_layer = tf.keras.Input((147,1))
first_layer = tf.keras.layers.Dense(32)(input_layer)
second_layer = tf.keras.layers.Dense(32)(first_layer)
last_layer = tf.keras.layers.Dropout(.3)(second_layer)
pred_layer = tf.keras.layers.Dense(1)(last_layer)
model = tf.keras.Model(inputs=input_layer,outputs=pred_layer)
model.summary()

Output

Model: "model_3"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_4 (InputLayer)        [(None, 147, 1)]          0         
                                                                 
 dense_9 (Dense)             (None, 147, 32)           64        
                                                                 
 dense_10 (Dense)            (None, 147, 32)           1056      
                                                                 
 dropout_3 (Dropout)         (None, 147, 32)           0         
                                                                 
 dense_11 (Dense)            (None, 147, 1)            33        
                                                                 
=================================================================
Total params: 1,153
Trainable params: 1,153
Non-trainable params: 0
Related