【问题标题】:Keras model with 2 inputs complains about input shape具有 2 个输入的 Keras 模型抱怨输入形状
【发布时间】:2020-09-10 09:43:38
【问题描述】:

我一直在使用具有 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 亿个位置),我在训练过程中使用了稀疏矩阵,效果很好。

我想将权重转移回我手写的 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,)

如果有人能简单地告诉我我搞砸了什么,我很高兴!

问候 芬兰人

【问题讨论】:

  • 是的,在训练期间,我在模型的输入节点处设置了 sparse=True,为此我将其更改为 sparse=True。

标签: python tensorflow machine-learning keras


【解决方案1】:

进行预测时需要添加batch_dim。

如果您的模型接受 2D 输入,您必须在预测中传递 2D 样本

你可以简单地扩展维度

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

【讨论】:

    【解决方案2】:

    您应该在输入中添加batch 维度。

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

    现在,您有 (1, 20480) 的形状,这意味着一个具有 20480 个特征的示例

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 2019-12-12
      • 2020-09-26
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多