【发布时间】:2017-06-23 00:53:57
【问题描述】:
我是 TensorFlow 的新手。我正在尝试使用对稀疏矩阵输入进行操作的 Tensorflow 在 python 中编写一个函数。通常我会定义一个 tensorflow 占位符,但显然稀疏矩阵没有占位符。
在 tensorflow 中定义对稀疏数据进行操作并将值传递给其中的函数的正确方法是什么?
具体来说,我正在尝试重写多层感知器的基本示例,在此处找到 https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py,以接受稀疏输入而不是密集输入。
作为一个虚拟示例,您将如何编写一个看起来像这样的函数?
import tensorflow as tf
x = tf.placeholder("sparse")
y = tf.placeholder("float", [None, n_classes])
# Create model
def sparse_multiply(x, y):
outlayer = tf.sparse_tensor_dense_matmul(x, y)
return out_layer
pred = multiply(x, y)
# Launch the graph
with tf.Session() as sess:
result = sess.run(pred, feed_dict={x: x_input, y: y_input})
链接https://github.com/tensorflow/tensorflow/issues/342 上的某人建议作为一种解决方法,传入构造稀疏矩阵所需的元素,然后在函数中动态创建稀疏矩阵。这似乎有点 hacky,当我尝试以这种方式构建它时会出错。
任何帮助,尤其是代码答案,将不胜感激!
【问题讨论】:
标签: python tensorflow sparse-matrix