使用Tensorflow DNN和tf-idf稀疏矩阵进行文本分类

尝试使用Tensorflow DNN进行文本分类。

tf-idf稀疏矩阵IV:

X_train_sam:<31819x3122 sparse matrix of type '<class 'numpy.float64'>'with 610128 stored elements in Compressed Sparse Row format>

标签作为DV:

y_train_sam.values:array(['mexican', 'mexican', 'italian', ..., 'chinese', 'italian','italian'], dtype=object)

使用以下代码将稀疏矩阵转换为张量:

def convert_sparse_matrix_to_sparse_tensor(X):    coo = X.tocoo()    indices = np.mat([coo.row, coo.col]).transpose()    return tf.SparseTensorValue(indices, coo.data, coo.shape) X_train_sam = convert_sparse_matrix_to_sparse_tensor(X_train_sam)

准备建模数据

def train_input_fn(features, labels, batch_size):    dataset = tf.data.Dataset.from_tensors((features, labels))    dataset = dataset.shuffle(1000).repeat().batch(batch_size)    return dataset.make_one_shot_iterator().get_next()inp = train_input_fn(X_train_sam,y_train_sam.values,batch_size=1000)

应用DNN分类器

classifier = tf.estimator.DNNClassifier(    feature_columns=[float]*X_train_sam.dense_shape[1],    hidden_units=[10, 10],    n_classes=len(y_train_sam.unique()))classifier.train(input_fn=lambda:inp)

遇到以下错误:

ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.framework.sparse_tensor.SparseTensorValue'>

请提供一些指导,我是机器学习和Tensorflow的新手。


回答:

如果在你的代码中这一行

classifier.train(input_fn=lambda:inp)

lambda:inp应该是字典还是你指的是匿名函数?根据文档

https://www.tensorflow.org/api_docs/python/tf/estimator/DNNClassifier

input_fn: 返回一个元组的输入函数:features – Tensor或字符串特征名称到Tensor的字典。labels – Tensor或包含标签的Tensor字典。

所以你需要一个返回元组的函数,而不是单个值…

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注