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

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条回答 默认 最新

  • ash062 2022-04-12 08:48
    关注

    loss不是binary_crossentropy?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 在qt的QGraphicsView和QGraphicsScene中遇到的问题
  • ¥50 如何完美解决谷歌礼品卡支付不被检测
  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电