python - 类型错误 : Failed to convert object of type Sparsetensor to Tensor

标签 python keras

我正在为 imdb 情感分析数据集构建文本分类模型。我下载了数据集并按照此处给出的教程进行操作 - https://developers.google.com/machine-learning/guides/text-classification/step-4
我得到的错误是

TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("DeserializeSparse:0", shape=(None, 2), dtype=int64), values=Tensor("DeserializeSparse:1", shape=(None,), dtype=float32), dense_shape=Tensor("stack:0", shape=(2,), dtype=int64)). Consider casting elements to a supported type.
x_train 和 x_val 的类型是 scipy.sparse.csr.csr_matrix。这在传递给顺序模型时会出错。怎么解决?
import tensorflow as tf
import numpy as np

from tensorflow.python.keras.preprocessing import sequence
from tensorflow.python.keras.preprocessing import text
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif

# Vectorization parameters

# Range (inclusive) of n-gram sizes for tokenizing text.
NGRAM_RANGE = (1, 2)

# Limit on the number of features. We use the top 20K features.
TOP_K = 20000

# Whether text should be split into word or character n-grams.
# One of 'word', 'char'.
TOKEN_MODE = 'word'

# Minimum document/corpus frequency below which a token will be discarded.
MIN_DOCUMENT_FREQUENCY = 2

# Limit on the length of text sequences. Sequences longer than this
# will be truncated.
MAX_SEQUENCE_LENGTH = 500


def ngram_vectorize(train_texts, train_labels, val_texts):
    """Vectorizes texts as ngram vectors.
    1 text = 1 tf-idf vector the length of vocabulary of uni-grams + bi-grams.
    # Arguments
        train_texts: list, training text strings.
        train_labels: np.ndarray, training labels.
        val_texts: list, validation text strings.
    # Returns
        x_train, x_val: vectorized training and validation texts
    """
    # Create keyword arguments to pass to the 'tf-idf' vectorizer.
    kwargs = {
            'ngram_range': NGRAM_RANGE,  # Use 1-grams + 2-grams.
            'dtype': 'int32',
            'strip_accents': 'unicode',
            'decode_error': 'replace',
            'analyzer': TOKEN_MODE,  # Split text into word tokens.
            'min_df': MIN_DOCUMENT_FREQUENCY,
    }
    vectorizer = TfidfVectorizer(**kwargs)

    # Learn vocabulary from training texts and vectorize training texts.
    x_train = vectorizer.fit_transform(train_texts)

    # Vectorize validation texts.
    x_val = vectorizer.transform(val_texts)

    # Select top 'k' of the vectorized features.
    selector = SelectKBest(f_classif, k=min(TOP_K, x_train.shape[1]))
    selector.fit(x_train, train_labels)
    x_train = selector.transform(x_train)
    x_val = selector.transform(x_val)

    x_train = x_train.astype('float32')
    x_val = x_val.astype('float32')
    return x_train, x_val

最佳答案

我也收到错误信息

TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> [...]
当我基于 Google Machine Learning Guide for text classification 构建模型时.
调用 todense()矢量化训练和验证文本对我有用:
x_train = vectorizer.fit_transform(train_texts).todense()
x_val = vectorizer.transform(val_texts).todense()
(虽然看起来很慢,但我不得不限制训练样本。)
编辑:
当我删除这一行(而不是添加 .todense() )时,它似乎也有效:
model.add(Dropout(rate=dropout_rate, input_shape=x_train.shape[1:]))
有关更多详细信息,请参阅此讨论:https://github.com/tensorflow/tensorflow/issues/47931

关于python - 类型错误 : Failed to convert object of type Sparsetensor to Tensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63950888/

相关文章:

python 元组列表排序

python - 导入错误: No module named 'tensorflow.python' ; 'tensorflow' is not a package

python - 在Python中更改keras floatx(默认float类型)

python - Keras 模型评估显示 TypeError : 'numpy.float64' object is not iterable for mnist dataset

python - 使用 flow_from_directory 进行多类和可变尺寸图像分类

python - matplotlib 中的 "Segmentation Fault"运行示例 Librosa 脚本

python - 仅使用 C 风格的 fprintf() 语句的高效数据交换格式?

python - 如何编写可以与 swift 可执行文件的输入和输出进行通信的 python 脚本?

python - 如何在 TensorFlow 中评估 MNIST 测试数据期间从每个输出节点获取值?

machine-learning - Keras——没有带有广播的 Dot 层?