使用SparseTensor运行LinearClassifier.fit

时间:2016-08-03 18:32:20

标签: tensorflow

我尝试使用SparseTensor创建具有稀疏二进制numpy coo矩阵(报告)的LinearClassifer。这是TensorFlow 0.9.0

我这样做如下:

reports_indices = list()
rows,cols = reports.nonzero()
for row,col in zip(rows,cols):
   reports_indices.append([row,col])

x_sparsetensor = tf.SparseTensor(
  indices=reports_indices,
  values=[1] * len(reports_indices),
  shape=[reports.shape[0],reports.shape[1]])

报告的维度为10K×1.5K。

然后按如下方式设置LinearClassifier:

m = tf.contrib.learn.LinearClassifier()

m.fit(x=x_sparsetensor,y=response_vector.todense(),input_fn=None)

响应向量是二进制的,长度为10K。这会导致以下错误:

Traceback (most recent call last):
  File "ddi_prr.py", line 38, in <module>
    m.fit(x=x_sparsetensor,y=response_vector.todense(),input_fn=None)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 173, in fit
    input_fn, feed_fn = _get_input_fn(x, y, batch_size)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 67, in _get_input_fn
    x, y, n_classes=None, batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/io/data_feeder.py", line 117, in setup_train_data_feeder
    X, y, n_classes, batch_size, shuffle=shuffle, epochs=epochs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/io/data_feeder.py", line 240, in __init__
    batch_size)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/io/data_feeder.py", line 44, in _get_in_out_shape
    x_shape = list(x_shape[1:]) if len(x_shape) > 1 else [1]
TypeError: object of type 'Tensor' has no len()

由于某些原因,我的构造是否不正确?似乎LinearClassifier.fit不能用SparseTensor for x实例化,是真的吗?提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

据我所知,将SparseTensors xy个参数传递给.fitnot supported

  

x:形状的矩阵或张量[n_samples,n_features ...]。可   返回返回功能数组的迭代器。培训输入样本   用于拟合模型。如果设置,input_fn必须为None。

此外,SparseTensorTensor的稀疏等价物 - 一个表示要执行的符号计算的对象。我相信您希望x使用的是SparseTensorValue。 您可以尝试使用其他方式将数据传递给Estimator:input_fn函数:

def get_input_fn(sparse_x, y):
  def input_fn():
    return sparse_x, y
m.fit(input_fn=get_input_fn(x, y))

如果不起作用,您可以尝试在SparseTensor函数中生成input_fn