它在第一个时期开始时停止训练。
错误是:
InvalidArgumentError: Incompatible shapes: [1,8,32] vs. [1,10,32]
[[Node: training_8/RMSprop/gradients/loss_8/time_distributed_9_loss/mul_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _class=["loc:@train.../Reshape_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](training_8/RMSprop/gradients/loss_8/time_distributed_9_loss/mul_grad/Shape, training_8/RMSprop/gradients/loss_8/time_distributed_9_loss/mul_grad/Shape_1)]]
我不确定错误是否可能是训练数据的格式。 这是train_x的示例:
[array([ 0, 1, 2, 3, 4, 5, 6, 3, 7, 8, 9, 10, 11, 12, 13, 14], dtype=int32), array([15, 16, 17, 18, 19], dtype=int32), array([20, 16, 17, 18, 21, 22, 23, 24, 25, 26], dtype=int32), array([27, 1, 17, 28, 18, 29, 30, 31, 24, 32], dtype=int32), array([33, 1, 17, 3, 34, 35, 36, 37, 18, 38], dtype=int32), array([39, 16, 40, 28, 41, 42], dtype=int32), array([39, 1, 40, 28, 41, 43], dtype=int32), array([44, 6, 3, 45], dtype=int32), array([15, 16, 40, 46, 47, 48, 3, 49, 50, 51, 52, 53], dtype=int32), array([ 0, 54, 28, 55, 56, 57, 58, 59], dtype=int32)]
这是train_label的示例
[array([0, 1, 2, 1, 2, 3, 0, 1, 4, 2, 2, 4, 5, 6, 0, 7], dtype=int32), array([0, 5, 8, 9, 9], dtype=int32), array([10, 5, 8, 9, 7, 9, 7, 7], dtype=int32), array([10, 1, 8, 1, 9, 9, 3, 0, 7, 2, 7], dtype=int32), array([10, 1, 8, 1, 9, 9, 9, 7, 9, 7], dtype=int32), array([0, 5, 8, 1, 2, 2], dtype=int32), array([0, 1, 8, 1, 2, 2], dtype=int32), array([11, 0, 1, 9], dtype=int32), array([ 0, 5, 8, 7, 12, 13, 1, 2, 9, 9, 5, 6], dtype=int32), array([ 0, 14, 1, 2, 2, 12, 4, 15], dtype=int32)]
这是代码:
n_vocab = len(unique_words) #1517
model = Sequential()
model.add(Embedding(n_vocab,100))
model.add(Convolution1D(128, 5, border_mode='same', activation='relu'))
model.add(Dropout(0.25))
model.add(GRU(100,return_sequences=True))
model.add(TimeDistributed(Dense(n_classes, activation='softmax')))
model.compile('rmsprop', 'categorical_crossentropy')
n_epochs = 30
train_x = [np.asarray(x, dtype=np.int32) for x in encoded_sentences]
train_label = [np.asarray(x, dtype=np.int32) for x in encoded_POS]
for i in range(n_epochs):
print("Training epoch {}".format(i))
bar = progressbar.ProgressBar(maxval=len(train_x))
for n_batch, sent in bar(enumerate(train_x)):
label = train_label[n_batch]
# Make labels one hot
label = np.eye(n_classes)[label][np.newaxis,:]
# View each sentence as a batch
sent = sent[np.newaxis,:]
if sent.shape[1] > 1: #ignore 1 word sentences
model.train_on_batch(sent, label)
答案 0 :(得分:1)
已发送和标签之间的长度不对称。在train_x中发送的第三个长度是10,但在train_label中发送的第三个标签长度是8。