簡體   English   中英

在Keras中擬合模型時的尺寸誤差

[英]Dimensionality error when fitting model in Keras

我正在嘗試使用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:檢查目標時出錯:預期density_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文本1 [0] [0]文本2 [0] [0]


concatenate_1(串聯)(16,384)0 lstm_1 [0] [0] lstm_1 [1] [0]


density_1(Dense)(16,8)3080 concatenate_1 [0] [0]


density_2(Dense)(16,1)9 density_1 [0] [0]


參數總計:1,184,273

可訓練的參數:1,184,273

不可訓練的參數: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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM