2016-08-03 22 views
2

我正在嘗試使用SparseTensor創建帶有稀疏二進制numpy合併矩陣(報告)的LinearClassifer。這是TensorFlow 0.9.0使用SparseTensor運行LinearClassifier.fit

我這樣做如下:

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不能用x的SparseTensor實例化,這是真的嗎?預先感謝您的幫助。

回答

0

據我所知,傳遞SparseTensors作爲xy參數.fitnot supported

x: matrix or tensor of shape [n_samples, n_features...]. Can be iterator that returns arrays of features. The training input samples for fitting the model. If set, input_fn must be None.

另外,SparseTensorTensor稀疏等效 - 要執行表示符號計算的對象。我相信你想用作x的是SparseTensorValue。 您可以嘗試通過它使用將數據傳遞給估計的另一種方式: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)) 

,如果它不能正常工作,您可以嘗試產生input_fn函數內SparseTensor秒。