You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keras Fit方法报‘Unrecognized Data Type’:NLP文本分类问题求助

解决LSTM文本分类模型的数据类型识别问题

问题根源

报错核心是模型输入的字符串数据未被正确转换为TensorFlow可识别的张量格式,同时存在二分类任务激活函数误用问题(单神经元输出用softmax会导致输出恒为1,干扰训练逻辑)。

具体修复步骤

  1. 规范输入数据格式
    直接传入Python列表或普通numpy字符串数组时,TensorFlow无法正确解析类型,需将数据转换为tf.string类型张量,或用tf.data.Dataset构建数据集(后者更适配TensorFlow训练流程)。

  2. 修正输出层激活函数
    二分类任务的单输出神经元应使用sigmoid激活函数,softmax仅适用于多分类的多神经元输出场景。

修改后的完整代码

库导入(无需改动)

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import nltk

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import NMF
import numpy as np
import itertools
from sklearn.metrics import confusion_matrix
from tabulate import tabulate
from sklearn import svm
from sklearn.model_selection import GridSearchCV

import tensorflow as tf
from keras.models import Sequential
from keras.layers import LSTM, Dropout, Dense, Embedding, Input
from keras.layers import TextVectorization
from keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing import sequence

输入数据与格式转换

text_data = [
    "The movie was fantastic! I really enjoyed it and would watch it again.",
    "I didn't like the movie. The plot had too many holes.",
    "One of the best movies I've seen this year. Highly recommended!",
    "The acting was poor and the storyline was boring. I wouldn't recommend it."
]

labels = [1, 0, 1, 0]

# 转换为TensorFlow张量
X = tf.convert_to_tensor(text_data, dtype=tf.string)
y = tf.convert_to_tensor(labels, dtype=tf.float32)

# 大规模数据推荐用tf.data.Dataset
# dataset = tf.data.Dataset.from_tensor_slices((text_data, labels)).batch(2)

文本向量化(无需改动)

vectorize_layer = TextVectorization(
    max_tokens=9000,
    output_mode='int',
    output_sequence_length=50,
    pad_to_max_tokens=True,
    standardize="lower_and_strip_punctuation",
    split='whitespace'
)

vectorize_layer.adapt(text_data)

模型定义(修正激活函数)

lstm_model = Sequential()
lstm_model.add(Input(shape=(1,), dtype="string"))
lstm_model.add(vectorize_layer)
lstm_model.add(Embedding(input_dim=9000, output_dim=128, embeddings_initializer='uniform'))
lstm_model.add(LSTM(64, return_sequences=True, recurrent_dropout=0.25, dropout=0.25))
lstm_model.add(LSTM(64))
lstm_model.add(Dense(32, activation='relu'))
# 二分类单神经元改用sigmoid激活
lstm_model.add(Dense(1, activation='sigmoid'))
lstm_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
lstm_model.summary()

训练模型

# 使用张量输入训练
lstm_model.fit(X, y, epochs=10)

# 若用tf.data.Dataset,执行以下代码:
# lstm_model.fit(dataset, epochs=10)

额外说明

  • 当Input层指定接收字符串类型时,TensorFlow要求输入必须是tf.string张量,Python列表或普通numpy字符串数组无法被直接解析,因此需要显式转换。
  • 二分类任务中,binary_crossentropy损失函数配合sigmoid激活是标准搭配,用softmax会导致输出概率恒为1,无法学习有效分类边界。

内容的提问来源于stack exchange,提问作者Geoffrey Stocksdale Richards

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:45:08