[英]Keras - "ValueError: Error when checking target: expected activation_1 to have shape (None, 9) but got array with shape (9,1)
I'm building a model to classify text into one of 9 layers, and am having this error when running it.我正在构建一个 model 将文本分类为 9 层之一,并且在运行它时出现此错误。 Activation 1 seems to refer to the Convolutional layer's input, but I'm unsure about what's wrong with the input.
激活 1 似乎是指卷积层的输入,但我不确定输入有什么问题。
num_classes=9
Y_train = keras.utils.to_categorical(Y_train, num_classes)
#Reshape data to add new dimension
X_train = X_train.reshape((100, 150, 1))
Y_train = Y_train.reshape((100, 9, 1))
model = Sequential()
model.add(Conv1d(1, kernel_size=3, activation='relu', input_shape=(None, 1)))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x=X_train,y=Y_train, epochs=200, batch_size=20)
Running this results in the following error:运行此程序会导致以下错误:
"ValueError: Error when checking target: expected activation_1 to have shape (None, 9) but got array with shape (9,1)
“ValueError:检查目标时出错:预期activation_1具有形状(无,9)但得到的数组形状为(9,1)
There are several typos and bugs in your code.您的代码中有几个拼写错误和错误。
Y_train = Y_train.reshape((100,9))
Since you reshape X_train
to (100,150,1), I guess your input step is 150, and channel is 1. So for the Conv1D
, (there is a typo in your code), input_shape=(150,1)
.由于您将
X_train
重塑为 (100,150,1),我猜您的输入步长为 150,通道为 1。因此对于Conv1D
,(您的代码中有错字) input_shape=(150,1)
。
You need to flatten your output of conv1d before feeding into Dense layer.在输入密集层之前,您需要将 conv1d 的 output 展平。
import keras
from keras import Sequential
from keras.layers import Conv1D, Dense, Flatten
X_train = np.random.normal(size=(100,150))
Y_train = np.random.randint(0,9,size=100)
num_classes=9
Y_train = keras.utils.to_categorical(Y_train, num_classes)
#Reshape data to add new dimension
X_train = X_train.reshape((100, 150, 1))
Y_train = Y_train.reshape((100, 9))
model = Sequential()
model.add(Conv1D(2, kernel_size=3, activation='relu', input_shape=(150,1)))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x=X_train,y=Y_train, epochs=200, batch_size=20)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.