简体   繁体   中英

TensorFlow/Keras: shape error on output layer

I have been searching the internet for a few days now trying to find a solution to this error, but nothing that I could find was specifically applicable:

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

Here is the code which generates the error(other than data pre-processing):

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(),
keras.layers.Dense(70, activation="relu"),
keras.layers.Dense(300,activation="tanh")
])
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='mse',
metrics=['mae'])
model.fit(traindata,trainlabels, epochs=10, batch_size=int(len(traindata)/60))

I am not exactly sure as to what is causing this error, but I did some troubleshooting to try and narrow down possibilities.

The first thing I attempted was to try and run one of the examples off the TensorFlow website, I chose the MNIST fashion example located here .

This ran with no errors, and trained fine, so I do not think that it is an error in my TensorFlow or Python downloads.

I then tried copying the model that was used in the MNIST fashion example to test to see if it was an error in my model. This is the code:

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(60,)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer=tf.train.AdamOptimizer(),
loss = 'sparse_categorical_cross_entropy',
metrics = ['accuracy']
)

The only change I made to the model was the input shape to the first layer, just so it would be able to take in my data. The error still persisted however:

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

The last thing I tried was making my own dense model that would take in dummy data created through np.random.random .

This is the entire file:

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((50,60))
datalabels = np.random.random((50,60))
model = keras.Sequential([
keras.layers.Dense(128,input_shape=(60,), activation="relu"),
keras.layers.Dense(50, activation="relu"),
keras.layers.Dense(10, activation="tanh")

])
model.compile(optimizer=tf.train.AdamOptimizer(),
loss="mae",
metrics=['mse'])

model.fit(data, datalabels, epochs=5, batch_size=10)

This also received the error, and I am confused as to why, but I am thinking that it might have to do with the generation of my dummy data

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

If it helps, I printed all of the layers for the last model and their input and output shapes:

Layer: dense
    input_shape: (None, 60)
    output_shape: (None, 128)
Layer: dense_1
    input_shape: (None, 128)
    output_shape: (None, 50)
Layer: dense_2
    input_shape: (None, 50)
    output_shape: (None, 10)

What perplexes me the most about this error is that it only errors on the output layer. If I add another layer to the end of the model, that layer will error, unless the number of units in the output layer is equal to the shape of the input of the model (ex: with input_shape=(60,) the number of the last layers units was equal to 60). Does anyone know why this might be happening?

Use model.summary() and you would see the output shape of each layer. In the first example you provided, the output shape of last layer (which is also the output shape of the model) is (None, 300) . This means it would expect the labels of shape (300,) (ie the shape of each label ). However, it seems the labels array you provide to the model when calling fit , ie trainlabels , has a shape of (num_samples, 60) . Whereas, it must be consistent with the output shape of the model and therefore has a shape of (num_samples, 300) . The same thing applies to all the other failing examples you mentioned.

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