这里有一个例外:
2024-03-22 22:20:40.503162: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-03-22 22:20:41.005622: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-03-22 22:20:41.887025: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Epoch 1/10
Traceback (most recent call last):
File "D:\PythonProjects\QuarkMate\main.py", line 63, in <module>
model, tokenizer = train_model(messages)
^^^^^^^^^^^^^^^^^^^^^
File "D:\PythonProjects\QuarkMate\main.py", line 58, in train_model
_model.fit(padded_sequences, to_categorical(padded_sequences), epochs=10, verbose=1)
File "D:\PythonProjects\QuarkMate\.venv\Lib\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler
raise e.with_traceback(filtered_tb) from None
File "D:\PythonProjects\QuarkMate\.venv\Lib\site-packages\keras\src\backend\tensorflow\nn.py", line 546, in categorical_crossentropy
raise ValueError(
ValueError: Arguments `target` and `output` must have the same rank (ndim). Received: target.shape=(None, 20, 48), output.shape=(None, 48)
那个代码,最近刚接触keras,了解的还不够
import csv
import json
import os
import threading
import numpy as np
import telebot
from keras import Sequential, Input
from keras.layers import Embedding, LSTM, Dense
from keras.preprocessing.sequence import pad_sequences
from keras.src.legacy.preprocessing.text import Tokenizer
from keras.utils import to_categorical
bot = telebot.TeleBot("censored hehehehaw")
# Parameters
max_sequence_length = 20
embedding_dim = 50
learning_interval = 3 * 60 # Learning interval in seconds (3 minutes)
dataset_file = 'dataset.json'
def load_dataset(file):
_messages = []
if os.path.exists(file):
with open(file, 'r', encoding='utf-8') as json_file:
_messages = json.load(json_file)
return _messages
def save_dataset(_messages, file):
with open(file, 'w', encoding='utf-8') as json_file:
json.dump(_messages, json_file)
if not os.path.exists(dataset_file):
open(dataset_file, 'a').close()
messages = load_dataset(dataset_file)
def train_model(_messages):
_tokenizer = Tokenizer()
_tokenizer.fit_on_texts(_messages)
sequences = _tokenizer.texts_to_sequences(_messages)
padded_sequences = pad_sequences(sequences, maxlen=max_sequence_length, padding='post')
_model = Sequential([
Input(shape=(max_sequence_length,)),
Embedding(len(_tokenizer.word_index) + 1, embedding_dim),
LSTM(128),
Dense(len(_tokenizer.word_index) + 1, activation='sigmoid')
])
_model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
_model.fit(padded_sequences, to_categorical(padded_sequences), epochs=10, verbose=1)
return _model, _tokenizer
model, tokenizer = train_model(messages)
def generate_response(message, _model, _tokenizer):
sequence = _tokenizer.texts_to_sequences([message])
padded_sequence = pad_sequences(sequence, maxlen=max_sequence_length, padding='post')
predicted_sequence = _model.predict(padded_sequence)[0]
predicted_word_index = np.argmax(predicted_sequence)
predicted_word = list(_tokenizer.word_index.keys())[
list(_tokenizer.word_index.values()).index(predicted_word_index)]
return predicted_word
@bot.message_handler(func=lambda message: True)
def echo_all(message):
global messages
if message.text.startswith('/') or message.from_user.is_bot:
return
if hasattr(message, 'reply_to_message') and message.reply_to_message is not None:
previous_message = message.reply_to_message.text
messages.append(previous_message)
messages.append(message.text)
save_dataset(messages, dataset_file)
response = generate_response(message.text, model, tokenizer)
if response:
bot.reply_to(message, response)
def periodic_ing.Timer(learning_interval, periodic_model_training).start()
model, tokenizer = train_model(messages)
periodic_model_training()
model_training():
global model, tokenizer, messages
thread
bot.polling()
我问过chatgpt和bingai。这是一个在电报聊天中自动学习的机器人,可以用定义的主题回答,请帮助我
我看到两个问题。通常,softmax 与 categorical_crossentropy 损失齐头并进(涉及多个标签)。但最直接的问题(从错误消息来看)是您的标签和最后一层(密集层)形状不匹配。参见例如https://discuss.tensorflow.org/t/target-output-mismatch-using-sentencepiecetokenizer-layer-with-huggingface-dataset/23240