我正在尝试将文章“从零开始的文字理解”的方法应用到不同的数据集。我很难让我的模型运行。
我的句子变成了Tensor of shape
(19579, 140, 69)
我已经使用以下方法预处理我的目标:
lb = sklearn.preprocessing.LabelBinarizer()
lb.fit(authors)
targets = lb.transform(authors)
targets = targets.reshape((targets.shape[0], 1, targets.shape[1]))
以
形式出现(19579, 1, 3)
张量
我的模特是:
nb_filter = 256
dense_outputs = 1024
cat_output = 3
batch_size = 80
nb_epoch = 10
inputs = Input(shape=(maxlen, vocab_size), name='input', dtype='float32')
conv0 = Convolution1D(nb_filter=nb_filter, filter_length=18, border_mode='valid', activation='relu', input_shape=(maxlen, vocab_size))(inputs)
conv0 = MaxPooling1D(pool_length=2)(conv0)
conv1 = Convolution1D(nb_filter=nb_filter, filter_length=14, border_mode='valid', activation='relu', input_shape=(maxlen, vocab_size))(conv0)
conv1 = MaxPooling1D(pool_length=2)(conv1)
conv2 = Convolution1D(nb_filter=nb_filter, filter_length=4, border_mode='valid', activation='relu', input_shape=(maxlen, vocab_size))(conv1)
conv3 = Convolution1D(nb_filter=nb_filter, filter_length=4, border_mode='valid', activation='relu', input_shape=(maxlen, vocab_size))(conv2)
conv4 = Convolution1D(nb_filter=nb_filter, filter_length=4, border_mode='valid', activation='relu', input_shape=(maxlen, vocab_size))(conv3)
conv5 = Convolution1D(nb_filter=nb_filter, filter_length=4, border_mode='valid', activation='relu', input_shape=(maxlen, vocab_size))(conv4)
conv6 = Convolution1D(nb_filter=nb_filter, filter_length=4, border_mode='valid', activation='relu', input_shape=(maxlen, vocab_size))(conv5)
conv6 = MaxPooling1D(pool_length=2)(conv6)
dense0 = Dropout(0.5)(Dense(dense_outputs, activation='relu')(conv6))
dense1 = Dropout(0.5)(Dense(dense_outputs, activation='relu')(dense0))
pred = Dense(cat_output, activation='softmax', name='output')(dense1)
model = Model(input=inputs, output=pred)
sgd = SGD(lr=0.01, momentum=0.9)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
但是当我打电话时
model.fit(x=X_train, y=y_train)
我收到以下错误:
ValueError: Error when checking target: expected output to have shape (None, 5, 3) but got array with shape (15663, 1, 3)
我意识到我没有正确预处理目标,但我无法弄清楚到底发生了什么!
答案 0 :(得分:2)
模型输出的形状((None, 5, 3)
)必须等于目标数据的形状((None, 1, 3)
)。
您输入了(None, maxlen, vocab_size)
之类的输入
您的卷积图层和合并图层将maxlen减少到最终值5.(请参阅model.summary()
以了解您的形状发生了什么)。
你必须找到一种方法将这5变为1.有几种可能性。
GlobalMaxPooling1D
或GlobalAveragePooling1D
。 Flatten()
Dense图层之前的数据。