下面是我的代码,用于使用keras运行LSTM模型:
epochs = 10
batch_size = 16
MAX_NB_WORDS = 50000
MAX_SEQUENCE_LENGTH = 250
EMBEDDING_DIM = 100
inputs = Input(shape=(X_train.shape[1],))
x = Embedding(MAX_NB_WORDS, EMBEDDING_DIM)(inputs)
x = SpatialDropout1D(0.2)(x)
x = LSTM(100, dropout=0.2, recurrent_dropout=0.2)(x)
logits = Dense(5)(x)
variance_pre = Dense(1)(x)
variance = Activation('softplus', name='variance')(variance_pre)
logits_variance = concatenate([logits, variance], name='logits_variance')
softmax_output = Activation('softmax', name='softmax_output')(logits)
model = Model(inputs=inputs, outputs=[logits_variance,softmax_output])
model.summary()
model.compile(
optimizer=Adam(lr=1e-3, decay=0.001),
loss={
'logits_variance': bayesian_categorical_crossentropy(100, 10),
'softmax_output': 'categorical_crossentropy'
},
metrics={'softmax_output': metrics.categorical_accuracy},
loss_weights={'logits_variance': .2, 'softmax_output': 1.})
history = model.fit(X_train, Y_train, epochs=epochs,
batch_size=batch_size,validation_split=0.1,callbacks=[EarlyStopping(monitor='val_loss',
patience=3, min_delta=0.0001)])
X_train形状为(2002,250),Y_train形状为(2002,5)
在模型上方运行时,出现以下错误:
ValueError:维度1的切片索引10超出范围。对于具有输入形状:[?, 6],[2],[2],[2]和计算出的输入张量的“ loss_34 / logits_variance_loss / strided_slice_1”(运算符:“ StridedSlice”),并具有计算出的输入张量:input [1] = <0 10 >,输入[2] = <0 11>,输入[3] = <1 1>。
请帮助解决该模型可能存在的问题。