【发布时间】:2018-01-30 20:31:17
【问题描述】:
尝试实现用于文本分类的 tesorflow 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'>
请指点一下,我是 ML 和 tensorflow 的新手。
【问题讨论】:
标签: tensorflow machine-learning tensorflow-datasets