基于稀疏矩阵的定制估计器

2024-04-25 14:58:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做和Tensorflow DNN with tf-idf sparse matrix差不多的事情

不过,我正在尝试定制一个估计器。 我跟踪了他大部分的工作

输入:

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

特色栏目:

^{pr2}$

获取稀疏张量:

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))

输入层(有效地脱离tensorflow站点):

    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接受提供的张量值/标签的元组,而feature列是一个_featurecolumns的列表。在


Tags: inpylayerinputmodeltftensorflowline