[英]Transforming text for a Keras CNN
我正在嘗試將論文“從頭開始理解文本”的方法應用於其他數據集。 我在運行模型方面遇到困難。
我的句子已經變成張量了
(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)
我意識到我沒有正確地預處理目標,但是我無法弄清楚到底發生了什么!
模型輸出的形狀(即(None, 5, 3)
)必須等於目標數據的形狀(即(None, 1, 3)
)。
您輸入的輸入是(None, maxlen, vocab_size)
。
卷積層和池化層將maxlen減小為最終值5(請參閱model.summary()
以了解形狀的變化)。
您必須找到一種將5轉換為1的方法。有幾種可能。
GlobalMaxPooling1D
或GlobalAveragePooling1D
。 Flatten()
放在Dense層之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.