簡體   English   中英

具有 2 個輸入的 Keras 模型抱怨輸入形狀

[英]Keras model with 2 inputs complains about input shape

我一直在研究一個有 2 個輸入的網絡來評估我的國際象棋引擎的國際象棋位置。 為此,我將網絡從我的 C++ 代碼轉換為 Keras,以便能夠在 GPU 上對其進行訓練。

我的模型看起來像這樣:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 20480)        0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            (None, 20480)        0                                            
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 256)          5243136     input_1[0][0]                    
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 256)          5243136     input_2[0][0]                    
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 512)          0           dense_1[0][0]                    
                                                                 dense_2[0][0]                    
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 32)           16416       concatenate_1[0][0]              
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 32)           1056        dense_3[0][0]                    
__________________________________________________________________________________________________
dense_5 (Dense)                 (None, 1)            33          dense_4[0][0]                    
==================================================================================================
Total params: 10,503,777
Trainable params: 10,503,777
Non-trainable params: 0

由於大量的輸入和大量的訓練數據(大約 3 億個位置),我在訓練期間使用了 Sparse 矩陣,效果很好。

我想將權重傳輸回我的手寫 C++ 代碼,並且出於調試目的,我想將單個輸入輸入 Keras 模型以將其與我的 C++ 模型進行比較。

indices =[21768,21769,21770,21771,21773,21774,21775,21788,21825,21830,21890,21893,21952,21959,22019,1288,1289,1290,1291,1292,1293,1294,1295,1345,1350,1410,1413,1472,1479,1539]
eval = -0.24
x_1 = np.zeros(half_input_size)
x_2 = np.zeros(half_input_size)

for i in indices:
    if(i < half_input_size):
        x_1[i] = 1
    else:
        x_2[i-half_input_size] = 1


print(x_1.shape)
print(x_2.shape)

print(model.predict([x_1, x_2]))


兩個輸入的形狀似乎是:

(20480,)
(20480,)

然而,Keras 給了我以下錯誤:

Traceback (most recent call last):
  File "A:/OneDrive/ProgrammSpeicher/CLionProjects/Koivisto/resources/networkTrainingKeras/Train.py", line 317, in <module>
    print(model.predict([x_1, x_2]))
  File "C:\Users\finne\.conda\envs\DeepLearning\lib\site-packages\keras\engine\training.py", line 1441, in predict
    x, _, _ = self._standardize_user_data(x)
  File "C:\Users\finne\.conda\envs\DeepLearning\lib\site-packages\keras\engine\training.py", line 579, in _standardize_user_data
    exception_prefix='input')
  File "C:\Users\finne\.conda\envs\DeepLearning\lib\site-packages\keras\engine\training_utils.py", line 145, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected input_1 to have a shape (20480,) but got array with shape (1,)

如果有人能簡單地告訴我我搞砸了什么,我很高興!

問候芬蘭人

進行預測時需要添加batch_dim。

如果您的模型接受 2D 輸入,則必須在預測中傳遞 2D 樣本

你可以簡單地擴展維度

model.predict([np.expand_dims(x_1,0), np.expand_dims(x_2,0)])

您應該為您的輸入添加batch維度。

x_1 = np.expand_dims(x_1, 0)
x_2 = np.expand_dims(x_1, 0)

現在,您有(1, 20480)形狀,這意味着一個具有 20480 個特征的示例

暫無
暫無

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

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