首页 文章

在Keras中拟合模型时的尺寸误差

提问于
浏览
0

我正在尝试使用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 .

我究竟做错了什么?如何为输出节点获得正确的形状?

Edit 型号摘要如下:


图层(类型)输出形状参数#连接到

text1(InputLayer)(8,15,384)0


text2(InputLayer)(8,15,384)0


lstm_1(LSTM)(8,384)1181184 text1 [0] [0] text2 [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

1 回答

  • 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(score) 替换 Y.append(np.full(output_shape, score)) .

相关问题