[英]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.