我正在尝试使用Keras(Tensorflow后端)构建模型:
def build_model(args):
# Define the input nodes
text1 = build_input_node('text1', args.batch_size, args.time_steps)
text2 = build_input_node('text2', args.batch_size, args.time_steps)
# Create the shared LSTM node
shared_lstm = LSTM(INPUT_SIZE, stateful=args.stateful)
# Run inputs through shared layer
encoded1 = shared_lstm(text1)
encoded2 = shared_lstm(text2)
# Concatenate outputs to form a tensor of shape (2*batch_size, INPUT_SIZE)
concatenated = concatenate([encoded1, encoded2], axis=0)
# Input shape: (2*batch_size, INPUT_SIZE)
# Output shape: (2*batch_size, batch_size)
dense1 = Dense(args.batch_size,
input_shape=(2 * args.batch_size, INPUT_SIZE),
activation='sigmoid')(concatenated)
# Input shape: (2*batch_size, batch_size)
# Output shape: (2*batch_size, 1)
output_shape = (2 * args.batch_size, 1)
output = Dense(1,
input_shape=(2 * args.batch_size, args.batch_size),
activation='sigmoid')(dense1)
model = Model(inputs=[text1, text2], outputs=output)
optimizer = build_optimizer(name=args.optimizer, lr=args.learning_rate)
model.compile(loss=args.loss,
optimizer=optimizer,
metrics=['accuracy'])
return model, output_shape
应该将输入到模型中的数据重新整形以适合output_shape
变量:
def build_datasets(input, time_steps, output_shape):
T1 = []
T2 = []
Y = []
for sentence1, sentence2, score in input:
T1.append([t.vector for t in nlp(sentence1)])
T2.append([t.vector for t in nlp(sentence2)])
Y.append(np.full(output_shape, score))
T1 = pad_and_reshape(T1, time_steps)
T2 = pad_and_reshape(T2, time_steps)
X = [T1, T2]
Y = np.asarray(Y)
# fit the scores between 0 and 1
Y = expit(Y)
return X, Y
但是当我调用model.fit(X, Y, epochs=100, batch_size=8)
时,它会抛出以下错误:
ValueError:检查目标时出错:期望dense_34有2个维度,但得到的数组有形状(1468,16,1)
其中1468是样本数,16是2 * batch_size。
我做错了什么?如何为输出节点获得正确的形状?
修改 模型摘要如下:
text1(InputLayer)(8,15,384)0
text2(InputLayer)(8,15,384)0
lstm_1(LSTM)(8,384)1181184 text1 [0] [0] 文本2 [0] [0]
concatenate_1(Concatenate)(16,384)0 lstm_1 [0] [0] lstm_1 [1] [0]
dense_1(密集)(16,8)3080 concatenate_1 [0] [0]
dense_2(密集)(16,1)9 dense_1 [0] [0]
总参数:1,184,273
可训练的参数:1,184,273
不可训练的参数:0
答案 0 :(得分:0)
经过keras
代码调试后,我发现keras
使用此行调整Y
的维度:
y = _standardize_input_data(y, self._feed_output_names,
output_shapes,
check_batch_axis=False,
exception_prefix='target')
反过来调用
data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
由于我的y
形状为(1468, 16, 1)
,因此稍后在验证时会出错。
修复方法是将Y.append(np.full(output_shape, score))
替换为Y.append(score)
。