Tensorflow DNN with tf-idf sparse matrix

2019-08-17 17:46发布

Trying to implement tesorflow DNN for text classification.

tf-idf sparse IV:

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

labels as DV:

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

Converting sparse to tensor using following piece:

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)

Preparing data for modeling

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)

Applying DNN Classifier

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)

Getting following error:

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

Please give some pointers, i am new to ML and tensorflow.

1条回答
做自己的国王
2楼-- · 2019-08-17 18:20

If in your code on this line

classifier.train(input_fn=lambda:inp)

lambda:inp is supposed to be a dictionary or you mean an anonymous function? From the documentation at

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

input_fn: Input function returning a tuple of: features - Tensor or dictionary of string feature name to Tensor. labels - Tensor or dictionary of Tensor with labels.

So you need a function that returns a tuple, not a single value...

查看更多
登录 后发表回答