简体   繁体   English

Keras - “ValueError:检查目标时出错:预期activation_1具有形状(无,9)但得到的数组具有形状(9,1)

[英]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.您的代码中有几个拼写错误和错误。

  1. Y_train = Y_train.reshape((100,9))

  2. 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)

  3. 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.

相关问题 keras:ValueError:检查模型目标时出错:预期activation_1具有形状(无,60),但数组的形状为(10,100) - keras: ValueError: Error when checking model target: expected activation_1 to have shape (None, 60) but got array with shape (10, 100) ValueError:检查目标时出错:预期activation_1的形状为(158,),但数组的形状为(121,) - ValueError: Error when checking target: expected activation_1 to have shape (158,) but got array with shape (121,) 检查目标时出错:预期的activation_1的形状为(1,),但数组的形状为(10,) - Error when checking target: expected activation_1 to have shape (1,) but got array with shape (10,) Keras(FIT_GENERATOR)- 检查目标时出错:预期 activation_1 具有 3 个维度,但得到的数组形状为 (32, 416, 608, 3) - Keras(FIT_GENERATOR)- Error, when checking target: expected activation_1 to have 3 dimensions, but got array with shape (32, 416, 608, 3) ValueError:检查模型目标时出错:预期activation_2具有形状(无,761,1)但是具有形状的数组(1,779,1) - ValueError: Error when checking model target: expected activation_2 to have shape (None, 761, 1) but got array with shape (1, 779, 1) ValueError:检查目标时出错:预期 activation_9 具有形状 (74, 6) 但得到形状为 (75, 6) 的数组 - ValueError: Error when checking target: expected activation_9 to have shape (74, 6) but got array with shape (75, 6) ValueError:检查目标时出错:预期 activation_6 具有形状(70,)但得到形状为(71,)的数组 - ValueError: Error when checking target: expected activation_6 to have shape (70,) but got array with shape (71,) ValueError:检查目标时出错:预期激活具有形状 (1,) 但得到形状为 (2,) 的数组 - ValueError: Error when checking target: expected activation to have shape (1,) but got array with shape (2,) Keras-检查目标时出错:预期activation_5具有形状(2,),但数组的形状为(1,) - Keras - Error when checking target: expected activation_5 to have shape (2,) but got array with shape (1,) ValueError:检查目标时出错:预期activation_5 的形状为(1,),但数组的形状为(100,) - ValueError: Error when checking target: expected activation_5 to have shape (1,) but got array with shape (100,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM