qq_31793379 2022-04-12 01:32 采纳率: 100%
浏览 220
已结题

tensorflow中model.fit()函数输入参数报错,如何解决?

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
filepath_dict = 'venv\Data.csv'
df = pd.read_csv(filepath_dict )
sentences = df['headlines'].values
y = df['target'].values
Y = []
for target in y:
if target == 'Sarcastic':
Y.append(1)
else:
Y.append(0)
from sklearn.model_selection import train_test_split
sentences_train,sentences_test,Y_train,Y_test = train_test_split(sentences,Y,test_size=0.5,random_state=500)
from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=10000)
tokenizer.fit_on_texts(sentences)
maxlen = 300
vocab_size = len(tokenizer.word_index)+1
#embedding模型

X_train = tokenizer.texts_to_sequences(sentences_train)
X_test = tokenizer.texts_to_sequences(sentences_test)
from keras.preprocessing.sequence import pad_sequences
X_train = pad_sequences(X_train,padding='post',maxlen=maxlen)
X_test = pad_sequences(X_test,padding='post',maxlen=maxlen)
Y_test = np.array(Y_test)
Y_train = np.array(Y_train)
#结束

embedding_dim = 300
from keras.models import Sequential
from keras import layers
from keras.layers import Dense,Activation,Dropout,LSTM
from keras.optimizer_v2 import adam
model = Sequential()
model.add(layers.Embedding(vocab_size,
embedding_dim,
input_length=maxlen,
trainable=True))
model.add(LSTM(128,return_sequences=True))
model.add(LSTM(64,return_sequences=False))
model.add(layers.Dense(15,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
model.compile(optimizer='adam',
loss = 'bomart_crossentropy',
metrics= ['accuracy'])
model.summary()
history = model.fit(X_train,
Y_train,
epochs=20,
verbose=False,
validation_data=(X_test,Y_test),
batch_size=10)

运行结果及报错内容

Traceback (most recent call last):
File "C:\Users\yly20\PycharmProjects\pythonProject1\main.py", line 63, in
history = model.fit(X_train,
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1147, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\engine\training.py", line 1021, in train_function  *
    return step_function(self, iterator)
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\engine\training.py", line 1010, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\engine\training.py", line 1000, in run_step  **
    outputs = model.train_step(data)
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\engine\training.py", line 860, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\engine\training.py", line 918, in compute_loss
    return self.compiled_loss(
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\engine\compile_utils.py", line 184, in __call__
    self.build(y_pred)
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\engine\compile_utils.py", line 133, in build
    self._losses = tf.nest.map_structure(self._get_loss_object, self._losses)
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\engine\compile_utils.py", line 272, in _get_loss_object
    loss = losses_mod.get(loss)
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\losses.py", line 2369, in get
    return deserialize(identifier)
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\losses.py", line 2324, in deserialize
    return deserialize_keras_object(
File "C:\Users\yly20\PycharmProjects\pythonProject1\venv\lib\site-packages\keras\utils\generic_utils.py", line 709, in deserialize_keras_object
    raise ValueError(

ValueError: Unknown loss function: bomart_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.
我的解答思路和尝试过的方法

似乎我完全没有找到方法解决,求指教

我想要达到的结果
  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 5月12日
      • 已采纳回答 5月4日
      • 创建了问题 4月12日

      悬赏问题

      • ¥15 关于#游戏程序#的问题:端 玩wy服务器10几分钟闪退一次没有任何提示,连启动器一起闪退 本人电脑5800h 3060 16运内 win11 求解答
      • ¥15 做一个手游私服需要什么条件?
      • ¥15 关于企业微信,调用官方 微盘api 时候access_token过期的问题?
      • ¥15 请问这个C语言代码的界面怎样修改
      • ¥15 QT for Android 开发的App后台切换后卡死,没有响应
      • ¥15 【Coppeliasim】利用remoteApi实现Coppeliasim与C++程序的数据通讯
      • ¥15 Spring Boot 服务迁移到 k8s , 配置文件 eureka.instance.prefer-ip-address = true,注册在 eureka 依然是服务名称
      • ¥15 No module named 'pyLDAvis'
      • ¥15 Microsoft.ReportViewer.WinForms 预览打印显示
      • ¥15 C#点到对岸的距离,各距离线段即垂直于下方斜线