值错误:未知损失函数:分类交叉熵

-1 投票
1 回答
39 浏览
提问于 2025-04-14 17:07

我正在做一个关于机器学习的项目:根据木瓜的成熟度来分类。但是因为我对这个领域还很陌生,所以我在YouTube上找了一些关于机器学习的视频,跟着一个博主学习。那个博主在写代码时没有遇到问题,但我却遇到了。我照着他做,结果还是出现了问题。我在网上查了这个问题,但还是没有找到答案。

代码如下:

import tensorflow as tf
import os
from tensorflow import keras
from keras.utils import img_to_array, load_img
import numpy as np
import cv2
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt


train_path = "D:/DATASETS/Red Lady/Train/"
test_path = "D:/DATASETS/Red Lady/Test/"

BatchSize = 64  # reduce the value if you have less Gpu memory

#img = load_img(train_path + "Class A/2.jpg")
#plt.imshow(img)
#plt.show()

#imgA = img_to_array(img)
#print(imgA.shape)

# Build the model
# ===============

model = Sequential()
model.add(Conv2D(filters=128, kernel_size=3, activation="relu", input_shape=(100, 100, 3)))
model.add(MaxPooling2D())
model.add(Conv2D(filters=64, kernel_size=3, activation="relu"))
model.add(Conv2D(filters=32, kernel_size=3, activation="relu"))
model.add(MaxPooling2D())
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(5000, activation="relu"))
model.add(Dense(1000, activation="relu"))
model.add(Dense(131, activation="softmax"))

print(model.summary())

# compile the model
model.compile(loss="categorical_cross entropy", optimizer="SGD", metrics=["accuracy"])

# load the data

train_datagen = ImageDataGenerator(rescale=1./255,
                                   shear_range=0.3,
                                   horizontal_flip=True,
                                   vertical_flip=True,
                                   zoom_range=0.3)

test_datagen = ImageDataGenerator(rescale=1./255)


train_generator = train_datagen.flow_from_directory(train_path,
                                                    target_size=(100, 100),
                                                    batch_size=BatchSize,
                                                    color_mode="rgb",
                                                    class_mode="categorical",
                                                    shuffle=True)

test_generator = test_datagen.flow_from_directory(test_path,
                                                  target_size=(100, 100),
                                                  batch_size=BatchSize,
                                                  color_mode="rgb",
                                                  class_mode="categorical")

stepsPerEpoch = np.ceil(train_generator.samples / BatchSize)

ValidationSteps = np.ceil(test_generator.samples / BatchSize)

# Early Stopping
# ==============

stop_early = EarlyStopping(monitor="val_accuracy", patience=5)


# train the model
history = model.fit(train_generator,
                    steps_per_epoch=stepsPerEpoch, epochs=50,
                    validation_data=test_generator,
                    validation_steps=ValidationSteps,
                    callbacks=[stop_early])

model.save("C:/Users/MY PC/PycharmProjects/MODEL 1 (Build Model)")

**这是结果和错误信息:**

C:\ProgramData\anaconda3\envs\pythonProject\python.exe "C:\Users\MY PC\PycharmProjects\MODEL 1 (Build Model)\main.py" 
2024-03-10 18:59:41.733172: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-03-10 18:59:41.835561: W tensorflow/tsl/framework/cpu_allocator_impl.cc:82] Allocation of 309760000 exceeds 10% of free system memory.
2024-03-10 18:59:42.116138: W tensorflow/tsl/framework/cpu_allocator_impl.cc:82] Allocation of 309760000 exceeds 10% of free system memory.
2024-03-10 18:59:42.207755: W tensorflow/tsl/framework/cpu_allocator_impl.cc:82] Allocation of 309760000 exceeds 10% of free system memory.
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             (None, 98, 98, 128)       3584      
                                                                 
 max_pooling2d (MaxPooling2D  (None, 49, 49, 128)      0         
 )                                                               
                                                                 
 conv2d_1 (Conv2D)           (None, 47, 47, 64)        73792     
                                                                 
 conv2d_2 (Conv2D)           (None, 45, 45, 32)        18464     
                                                                 
 max_pooling2d_1 (MaxPooling  (None, 22, 22, 32)       0         
 2D)                                                             
                                                                 
 dropout (Dropout)           (None, 22, 22, 32)        0         
                                                                 
 flatten (Flatten)           (None, 15488)             0         
                                                                 
 dense (Dense)               (None, 5000)              77445000  
                                                                 
 dense_1 (Dense)             (None, 1000)              5001000   
                                                                 
 dense_2 (Dense)             (None, 131)               131131    
                                                                 
=================================================================
Total params: 82,672,971
Trainable params: 82,672,971
Non-trainable params: 0
_________________________________________________________________
None
Found 2500 images belonging to 3 classes.
Found 652 images belonging to 3 classes.
Epoch 1/50
Traceback (most recent call last):
  File "C:\Users\MY PC\PycharmProjects\MODEL 1 (Build Model)\main.py", line 85, in <module>
    callbacks=[stop_early])
  File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\MYPC~1\AppData\Local\Temp\__autograph_generated_fileuv05hknk.py", line 15, in tf__train_function
    retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:

    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\engine\training.py", line 1160, in train_function  *
        return step_function(self, iterator)
    File "C:\ProgramData\anaconda3\envs\pythonProject\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\pythonProject\lib\site-packages\keras\engine\training.py", line 1135, in run_step  **
        outputs = model.train_step(data)
    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\engine\training.py", line 994, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\engine\training.py", line 1053, in compute_loss
        y, y_pred, sample_weight, regularization_losses=self.losses
    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\engine\compile_utils.py", line 240, in __call__
        self.build(y_pred)
    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\engine\compile_utils.py", line 183, in build
        self._get_loss_object, self._losses
    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\engine\compile_utils.py", line 353, in _get_loss_object
        loss = losses_mod.get(loss)
    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\losses.py", line 2649, in get
        return deserialize(identifier)
    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\losses.py", line 2607, in deserialize
        printable_module_name="loss function",
    File "C:\ProgramData\anaconda3\envs\pythonProject\lib\site-packages\keras\utils\generic_utils.py", line 770, in deserialize_keras_object
        f"Unknown {printable_module_name}: {object_name}. Please "

    ValueError: Unknown loss function: categorical_cross entropy. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.


Process finished with exit code 1

--版本信息-- Tensorflow = 2.11.0 Keras = 2.10.0 Python = 3.7.16

1 个回答

1

虽然在 model.compile() 方法中可以把损失函数作为字符串传进去,但我觉得你可能拼错了。试试这样传:

model.compile(loss=tf.keras.losses.CategoricalCrossentropy(),...

撰写回答