ValueError: Unknown loss function: categorical crossentropy.



我正在尝试建立一个大学项目的聊天机器人,通过遵循youtube教程,基本上没有经验。直到现在,一切都很好,我得到一个ValueError。

这是我运行代码时收到的结果:

C:UsersKimbe.condaenvstf.2python.exe C:UsersKimbePycharmProjectschatbottraining.py 
C:UsersKimbePycharmProjectschatbottraining.py:53: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
training = np.array(training)
2022-11-23 21:38:00.366897: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2022-11-23 21:38:00.367881: W tensorflow/stream_executor/cuda/cuda_driver.cc:263] failed call to cuInit: UNKNOWN ERROR (303)
2022-11-23 21:38:00.371587: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: Kims-Surface
2022-11-23 21:38:00.371782: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: Kims-Surface
2022-11-23 21:38:00.372191: 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.
C:UsersKimbe.condaenvstf.2libsite-packageskerasoptimizersoptimizer_v2gradient_descent.py:111: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.
super().__init__(name, **kwargs)
Epoch 1/200
Traceback (most recent call last):
File "C:UsersKimbePycharmProjectschatbottraining.py", line 69, in <module>
model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasutilstraceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UsersKimbeAppDataLocalTemp__autograph_generated_filecynafcyn.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:UsersKimbe.condaenvstf.2libsite-packageskerasenginetraining.py", line 1160, in train_function  *
return step_function(self, iterator)
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasenginetraining.py", line 1146, in step_function  **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasenginetraining.py", line 1135, in run_step  **
outputs = model.train_step(data)
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasenginetraining.py", line 994, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasenginetraining.py", line 1052, in compute_loss
return self.compiled_loss(
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasenginecompile_utils.py", line 240, in __call__
self.build(y_pred)
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasenginecompile_utils.py", line 182, in build
self._losses = tf.nest.map_structure(
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasenginecompile_utils.py", line 353, in _get_loss_object
loss = losses_mod.get(loss)
File "C:UsersKimbe.condaenvstf.2libsite-packageskeraslosses.py", line 2649, in get
return deserialize(identifier)
File "C:UsersKimbe.condaenvstf.2libsite-packageskeraslosses.py", line 2603, in deserialize
return deserialize_keras_object(
File "C:UsersKimbe.condaenvstf.2libsite-packageskerasutilsgeneric_utils.py", line 769, in deserialize_keras_object
raise ValueError(
ValueError: Unknown loss function: categorical crossentropy. 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

这是我的代码:

import random
import json
import pickle
import numpy as np
import nltk
from nltk.stem import WordNetLemmatizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Dropout
from tensorflow.keras.optimizers import SGD
lemmatizer = WordNetLemmatizer()
intents = json.loads(open('intents.json').read())
words = []
classes = []
documents = []
ignore_letters = ['?', '!', '.', ',']
for intent in intents['intents']:
for pattern in intent['patterns']:
word_list = nltk.word_tokenize(pattern)
words.extend(word_list)
documents.append((word_list, intent['tag']))
if intent['tag'] not in classes:
classes.append(intent['tag'])
words = [lemmatizer.lemmatize(word) for word in words if word not in ignore_letters]
words = sorted(set(words))
classes = sorted(set(classes))
pickle.dump(words, open('words.pkl', 'wb'))
pickle.dump(words, open('classes.pkl', 'wb'))
training = []
output_empty = [0] * len(classes)
for document in documents:
bag = []
word_patterns = document[0]
word_patterns = [lemmatizer.lemmatize(word.lower()) for word in word_patterns]
for word in words:
bag.append(1) if word in word_patterns else bag.append(0)
output_row = list(output_empty)
output_row[classes.index(document[1])] = 1
training.append([bag, output_row])
random.shuffle(training)
training = np.array(training)

train_x = list(training[:, 0])
train_y = list(training[:, 1])
model = Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(train_y[0]), activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical crossentropy', optimizer=sgd, metrics=['accuracy'])
model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
model.save('Chatbot_model.model')
print("Done")

我在谷歌上搜索了一下,尝试了不同的修复方法,但似乎没有一个有效。因为它说了一些关于重建tensorflow我假设我需要重新下载它并再次执行代码?之前,tensorflow和代码似乎运行良好,但在添加随机。Shuffle此错误出现。

如果有人能帮我就太好了。谢谢你!:)

您可能需要考虑输入格式为float或int。

示例:当序列是格式化的,可以传递时,计算是有益的,但当它不能有函数负载时,计算就没有意义了。

import nltk
from nltk.stem import WordNetLemmatizer
import tensorflow as tf
import json
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
vocab = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_", 
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
",", "ù", "é", "ç", "ô", "À", "à" ]
lemmatizer = WordNetLemmatizer()
intents = json.loads(open("F:\temp\Python\chatbots\intents.json").read())
words = []
classes = []
documents = []
ignore_letters = ['?', '!', '.', ',']
list_classes = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
list_words = [ ]
list_label = [ ]
for intent in intents['intents']:
for pattern in intent['patterns']:
word_list = nltk.word_tokenize(pattern)
words.extend(word_list)
documents.append((word_list, intent['tag']))
if intent['tag'] not in classes:
classes.append(intent['tag'])

words = [lemmatizer.lemmatize(word) for word in words if word not in ignore_letters]
words = sorted(set(words))
classes = sorted(set(classes))
print( "======================================================================================" )
layer = tf.keras.layers.StringLookup(vocabulary=vocab)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Class / Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def auto_paddings( data, max_sequences=40 ):
data = tf.constant( data, shape=(data.shape[0], 1) )
paddings = tf.constant([[1, 40 - data.shape[0] - 1], [0, 0]])
padd_data = tf.pad( data, paddings, "CONSTANT" )
padd_data = tf.constant( padd_data, shape=(40, 1) ).numpy()
return padd_data
print( "======================================================================================" )
for words_string in words:
padd_data = auto_paddings( layer( tf.strings.bytes_split(words_string) ), 40 )
list_words.append( padd_data )
list_label.append( list_classes[0] )    # requires mapping or supervise learning
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
dataset = tf.data.Dataset.from_tensor_slices((tf.constant(list_words, shape=(168, 1, 40, 1),dtype=tf.float32), tf.constant(list_label, shape=(168, 1, 1), dtype=tf.int64)))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 40, 1 )),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True, return_state=False)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(192, activation='relu'),
tf.keras.layers.Dense(11),
])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam(
learning_rate=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
name='Nadam'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=False,
reduction=tf.keras.losses.Reduction.AUTO,
name='sparse_categorical_crossentropy'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit( dataset, batch_size=100, epochs=50 )

输出:函数调用样本输入,反馈值有意义损失优化器fn和矩阵值似乎反映了真实情况。

Epoch 1/50
2022-11-24 08:13:14.910457: I tensorflow/stream_executor/cuda/cuda_dnn.cc:384] Loaded cuDNN version 8100
168/168 [==============================] - 11s 26ms/step - loss: 0.9852 - accuracy: 0.5893
Epoch 2/50
168/168 [==============================] - 5s 27ms/step - loss: 0.2256 - accuracy: 1.0000
Epoch 3/50
104/168 [=================>............] - ETA: 1s - loss: 0.0082 - accuracy: 1.0000

相关内容

  • 没有找到相关文章

最新更新