简体   繁体   中英

Tensorflow/Keras: model's output layer expects different input shape than what it recieved

I have been looking for an answer to this for a few days now, but nothing I could find on the internet has seemed to be specifically applicable for the error I am getting:

ValueError: Error when checking target: expected dense_1 to have shape (1, 300) but got array with shape (1, 60)

Here is the model I am using:

model = keras.Sequential([
keras.layers.Conv1D(5,input_shape=(1,60), kernel_size=12, padding='same'), 
keras.layers.Conv1D(10, padding='same',activation=tf.nn.relu, kernel_size=10),
keras.layers.Conv1D(20, padding='same',activation=tf.nn.relu, kernel_size=6),
keras.layers.Conv1D(30, padding='same',activation=tf.nn.relu, kernel_size=5),
keras.layers.Dense(70, activation=tf.nn.relu),
keras.layers.Dense(300,activation="tanh")
]) 
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='mse',
metrics=['mae'])

The only way I am able to have the net train is to set the number of nodes in my output layer (in this case dense_1 in the error message) to equal 60, the number of values my model takes in for its input.

I have also run this data through a fully connected model where the same outcome occurred, as is would error if the number of nodes in the output was anything but 60.

I tried troubleshooting to see if it was my version of tensorflow or python that may have been causing issues by running the mnist fashion classification example off of the tensorflow website, however this ran with no error, and trained fine.

I then replaced my model with that which was used in the mnist fashion example, except changing the input shape to (60,) as my data is one dimensional, and I got the same exact error:

ValueError: Error when checking target: expected dense_1 to have shape (10,) but got array with shape (60,)

except this time it had shown slight alterations to the input shapes as the first error had occurred in a model with a Conv1D being fed the input.

All of this has lead me to believe that the error is in my data, as I am pretty confident that it is not an error with my model due to it happening with multiple different models.

You need to add the Flatten layer so that data from the Conv1D layer is flattened the for Dense layer.

model = keras.Sequential([
keras.layers.Conv1D(5,input_shape=(1,60), kernel_size=12, padding='same'), 
keras.layers.Conv1D(10, padding='same',activation=tf.nn.relu, kernel_size=10),
keras.layers.Conv1D(20, padding='same',activation=tf.nn.relu, kernel_size=6),
keras.layers.Conv1D(30, padding='same',activation=tf.nn.relu, kernel_size=5),
keras.layers.Flatten(), # This layer should be added
keras.layers.Dense(70, activation=tf.nn.relu),
keras.layers.Dense(300,activation="tanh")
]) 
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='mse',
metrics=['mae'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM