我是Tensorflow的新手 . 我正在尝试使用Tensorflow在python中编写一个函数,该函数在稀疏矩阵输入上运行 . 通常我会定义一个张量流占位符,但显然没有稀疏矩阵的占位符 .
定义一个在张量流中对稀疏数据进行操作并将值传递给它的函数的正确方法是什么?
具体来说,我试图重写多层感知器的基本示例,在这里找到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,当我尝试以这种方式构建它时,我会遇到错误 .
任何帮助,特别是代码的答案,将不胜感激!
1 回答
我想我明白了 . 我链接的建议确实有效,我只需要纠正所有输入以获得一致的类型 . 这是我在问题中列出的虚拟示例,编码正确: