尽管编译,但在 Tensorflow 模型中没有发现损失

问题描述 投票:0回答:0

我目前正在使用 TensorFlow 2.9.2,我似乎无法让损失函数起作用。我尝试使用 tf.keras.Sequential() 创建模型并使用 model.add 作为图层,我还尝试创建一个创建模型的函数。无论我做什么,我总是会收到以下错误:

 ValueError: No loss found. You may have forgotten to provide a `loss` argument in the `compile()` method.

第一次尝试:

from keras.layers import Conv2D, MaxPooling2D, Dense
import keras
from keras import losses 
from keras import optimizers 
from keras import metrics 
def create_model(): 
    
    model = tf.keras.Sequential([
    tf.keras.layers.Dense(65000/train.shape[0], input_shape=(65000,)),
    layers.Dense(128, activation='relu'),
    layers.Dense(256, activation='relu'),
    layers.Dense(3,activation='sigmoid'),
    ])
    
    #model.add_loss(tf.keras.losses.MeanSquaredError())


    model.compile(loss = 'mean_squared_error',  optimizer = 'sgd', metrics = [metrics.categorical_accuracy])
    
    return model

model = create_model()
model.fit(train_x, labels, epochs=10)

尝试次数 2:

from keras.layers import Conv2D, MaxPooling2D, Dense
import keras
from keras import losses 
from keras import optimizers 
from keras import metrics 
model =tf.keras.Sequential()
model.add(Dense(100, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='softmax'))
model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
model.fit(train_x, labels, epochs=2, steps_per_epoch=10)
print('My custom loss: ', model.loss_tracker.result().numpy())

两次尝试都将我引向没有发现损失的 ValueError。

这是我的版本信息:

tensorflow version 2.9.2
numpy version 1.23.4
pandas version 1.5.0
keras version 2.9.0
python Version:- 3.9.16 (main, Dec  7 2022, 01:11:51) 
[GCC 9.4.0]

我在 paperspace.com Jupyter Notebook 上运行。我试过在不同的显卡上创建另一个笔记本,但仍然没有运气。

!jupyter--version 的输出:

Selected Jupyter core packages...
IPython          : 8.5.0
ipykernel        : 6.16.0
ipywidgets       : 8.0.2
jupyter_client   : 7.3.4
jupyter_core     : 5.1.5
jupyter_server   : 1.23.5
jupyterlab       : 3.4.6
nbclient         : 0.7.2
nbconvert        : 7.2.9
nbformat         : 5.7.3
notebook         : 6.5.2
qtconsole        : not installed
traitlets        : 5.8.1
python tensorflow machine-learning keras deep-learning
© www.soinside.com 2019 - 2024. All rights reserved.