2017-09-30 12 views
0

1次元スパーステンソル

import tensorflow as tf 
import numpy as np 

x = tf.sparse_placeholder(tf.float32) 
y = tf.sparse_reduce_sum(x) 

with tf.Session() as sess: 
    indices = np.array([0, 1], dtype=np.int64) 
    values = np.array([1.5, 3.0], dtype=np.float32) 
    shape = np.array([2], dtype=np.int64) 
    print(sess.run(y, feed_dict={ 
     x: tf.SparseTensorValue(indices, values, shape)})) 

このコードは、次のエラーがスローされます。

ValueError: Cannot feed value of shape (2,) for Tensor u'Placeholder_2:0', which has shape '(?, ?)' 

私が間違っている形状を渡すだろうか?

答えて

1

インデックスのサイズは(2,1)である必要があります。インデックスをindices = np.array([[0], [1]], dtype=np.int64)に変更してください。以下のコードが動作します:

x = tf.sparse_placeholder(tf.float32) 
y = tf.sparse_reduce_sum(x) 

with tf.Session() as sess: 
    indices = np.array([[0], [1]], dtype=np.int64) 
    values = np.array([1.5, 3.0], dtype=np.float32) 
    shape = np.array([2], dtype=np.int64) 
    print(sess.run(y, feed_dict={ 
     x: tf.SparseTensorValue(indices, values, shape)})) 

#Output 
#4.5 
関連する問題