我正在处理模型中的自定义损失,但出现此错误
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [32,32] vs. [32,32,912]
[[{{node LogicalAnd}}]]
这很奇怪,但是我知道input_b的形状为(331,912)是有问题的。因此,我查看了模型的形状,但input_b不存在。 编辑 因此,如果解决了第二个输入不显示的问题。但是我仍然遇到同样的错误。所以我的想法是因为第二个输入不正确。
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_a (InputLayer) (None,224,3) 0
__________________________________________________________________________________________________
input_b (InputLayer) (None,912) 0
__________________________________________________________________________________________________
model_1 (Model) (None,2048) 20861480 input_a[0][0]
__________________________________________________________________________________________________
concatenate_1 (concatenate) (None,2960) 0 input_b[0][0]
model_1[1][0]
==================================================================================================
Total params: 20,861,480
Trainable params: 20,806,952
Non-trainable params: 54,528
__________________________________________________________________________________________________
None
所以我不知道此错误发生了什么。然后我会认为这是由于输入层不在那儿,是吗?这是我的模型代码
# Define new model
input_a = Input(shape=(224,3),name='input_a')
input_b = Input(shape=(912,),name='input_b')
embeddings = base_network(input_a) # loaded from a fine tuned model
final_data = concatenate(axis=1)([input_b,embeddings])
batch_loss = batch_hard_triplet_loss(embeddings,input_b,0.4,False)
model = Model(inputs=[input_a,input_b],outputs=final_data)
# Compile model
model.compile(loss=batch_loss,optimizer='adam')
完整错误日志
Preocessing data...
Shape of X train
(331,3)
Shape of Y train
(331,912)
(164,3)
(164,912)
Finished processing data.
Initializing Model...
WARNING:tensorflow:From C:/Users/matthew.millar/Documents/reid/ReIDFullProject/AdvancedTensorflowTripleLoss.py:47: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Model Created.
Started Training model...
WARNING:tensorflow:From C:\Users\matthew.millar\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\ops\math_grad.py:102: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
Train on 332 samples,validate on 164 samples
Epoch 1/10
Traceback (most recent call last):
File "C:/Users/matthew.millar/Documents/reid/ReIDFullProject/AdvancedTensorflowTripleLoss.py",line 288,in <module>
callbacks=callbacks_list)
File "C:\Users\matthew.millar\Anaconda3\envs\tf\lib\site-packages\keras\engine\training.py",line 1039,in fit
validation_steps=validation_steps)
File "C:\Users\matthew.millar\Anaconda3\envs\tf\lib\site-packages\keras\engine\training_arrays.py",line 199,in fit_loop
outs = f(ins_batch)
File "C:\Users\matthew.millar\Anaconda3\envs\tf\lib\site-packages\keras\backend\tensorflow_backend.py",line 2715,in __call__
return self._call(inputs)
File "C:\Users\matthew.millar\Anaconda3\envs\tf\lib\site-packages\keras\backend\tensorflow_backend.py",line 2675,in _call
fetched = self._callable_fn(*array_vals)
File "C:\Users\matthew.millar\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py",line 1439,in __call__
run_metadata_ptr)
File "C:\Users\matthew.millar\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\errors_impl.py",line 528,in __exit__
c_api.TF_getcode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [32,912]
[[{{node LogicalAnd}}]]
这是拟合函数,所以您知道我如何发送数据
history = model.fit(x=[x_train,y_train],y=dummy_y_train,epochs=EPOCH,batch_size=BATCHSIZE,shuffle=True,validation_data=([x_test,y_test],dummy_y_test),verbose=1,callbacks=callbacks_list)