keras ValueError:未找到丢失,您可能忘记在“compile()”方法中提供“loss”参数

eqqqjvef  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(127)

“我已经使用TensorFlow 1.2编写了Dispnet的整个网络框架,现在我想尝试TensorFlow 2.10的网络框架。所以我基于2.12的API编写了以下模型

导入tensorflow作为tf
从tensorflow导入keras
layer = tf.keras.layers
conv1 = layer.Conv2D(filters=64,kernel_size=(7,7),strides=(1,1),padding=“padding”,activation=“relu,name=“conv1”)(input)
max_pool1 = layer.MaxPool2D(pool_size=(2,2),strides=(2,2),padding=“padding”,name=“max_pool1”)(conv1)
......
层层
......
cat1 = tf.keras.layers.Concatenate(axis=3)([upconv1,max_pool1])iconv1 = layer.Conv2D(filters=32,kernel_size=(3,3),strides=(1,1),padding=“padding”,activation=“relu”,name=“iconv1”)(cat1)
model = keras.Model(inputs=input,outputs=iconv1,name=“Dispnet_Simple”)model.compile(optimizer='adam',loss=tf.keras.losses.MeanSquaredError,metrics=“accuracy”])output = []
model.fit(x=合并_image,y=输出,batch_size=BATCH_SIZE,epochs=2)
errors:Traceback(most recent call last):“C:\Users\SUPERJ~1\AppData\Local\Temp_autograph_generated_filedo1_ym8c.py”,line 15,in tf__train_function retval = ag__.converted_call(ag__.ld(step_function),(ag__.ld(self),ag__.ld(iterator)),None,fscope)ValueError:用户代码:

File "C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\keras\engine\training.py", line 1160, in train_function  *
    return step_function(self, iterator)
File "C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\keras\engine\training.py", line 1146, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\keras\engine\training.py", line 1135, in run_step  **
    outputs = model.train_step(data)
File "C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\keras\engine\training.py", line 995, in train_step
    self._validate_target_and_loss(y, loss)
File "C:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\keras\engine\training.py", line 959, in _validate_target_and_loss
    raise ValueError(

ValueError:未发现丢失。您可能忘记在compile()方法中提供loss参数。

祈求伟大的神保存孩子!!!!!
我改变了模型的损失参数。compile()无数次。我真的不写了!!!

ttisahbt

ttisahbt1#

正如@Superjiang所提到的,这个问题的出现是因为输入和输出的形状不一致。
解决方案:
1.您可以尝试更改输入和输出数组的形状,使它们相似。
1.您可以放置一个全局平均池层,然后更改维度。

相关问题