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

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 图像超分real-esrgan网络自己训练模型遇到问题
  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵