使用稀疏矩阵的自定义估算器

时间:2018-06-26 09:44:41

标签: python tensorflow machine-learning text-classification

我正在尝试做与Tensorflow DNN with tf-idf sparse matrix相同的事情

但是,我正在尝试进行自定义估算。 我已经遵循了他所做的大部分工作,

输入:

(<tensorflow.python.framework.sparse_tensor.SparseTensor object at 0x125fd6b70>, <tf.Tensor 'IteratorGetNext:1' shape=(?, 34549) dtype=int64>)

功能列:

[_NumericColumn(key='\x03', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='\x08', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='\t', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='\x0b', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)

获取稀疏张量:

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)

输入功能:

def inputs_fn(X,y):
    dataset = tf.data.Dataset.from_tensors((X,y))
    dataset = dataset.shuffle(1000).repeat().batch(500)
    return dataset.make_one_shot_iterator().get_next()

获取功能列键:

    feature_cols = []
for key in feature_names:
    feature_cols.append(tf.feature_column.numeric_column(key=key))

输入层(有效地拉离张量流部位):

    def my_model_fn(
   features, # This is batch_features from input_fn
   labels,   # This is batch_labels from input_fn
   mode,
   params
   ):    # Instance of tf.estimator.ModeKeys, see below
    #feature_cols = [tf.feature_column.embedding_column(categorical_column=key, dimension=1) for key in feature_names]
    input_layer = tf.feature_column.input_layer(features, params['feature_columns'])

Lambda输入函数并初始化:

    X = convert_sparse_matrix_to_sparse_tensor(X)
print(inputs_fn(X,y) , 'INPUTS ARE CORRECTLY IN TUPLE FORM')

input = lambda : inputs_fn(X,y)

classifier = tf.estimator.Estimator(model_fn=my_model_fn,params={'feature_columns':feature_cols},model_dir='/checkpoints')
classifier.train(input_fn=input)

我得到的错误:

Traceback (most recent call last):
  File "/Users/william/mn-classification/estimator-01/Model.py", line 103, in <module>
    classifier.train(input_fn=input)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 363, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 843, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 856, in _train_model_default
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 831, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "/Users/william/PycharmProjects/mn-classification/estimator-01/Model.py", line 44, in my_model_fn
    input_layer = tf.feature_column.input_layer(features, params['feature_columns'])
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/feature_column/feature_column.py", line 277, in input_layer
    trainable, cols_to_vars)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/feature_column/feature_column.py", line 191, in _internal_input_layer
    scope, default_name='input_layer', values=features.values()):
TypeError: 'Tensor' object is not callable

我不太确定自己在做什么错,我才刚刚开始使用tensorflow。 Train接受提供的张量值/标签的元组,要素列是_featurecolumns的列表。

0 个答案:

没有答案