在 Tensorflow 函数中使用稀疏矩阵参数
Using Sparse Matrix Arguments in a Tensorflow Function
我是 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})
link https://github.com/tensorflow/tensorflow/issues/342 的某个人建议,作为解决方法,传入构建稀疏矩阵所需的元素,然后在函数内动态创建稀疏矩阵。这似乎有点老套,当我尝试以这种方式构建它时会出错。
任何帮助,尤其是代码答案,将不胜感激!
我想我明白了。我链接到的建议确实有效,我只需要更正所有输入以具有一致的类型。这是我在问题中列出的虚拟示例,编码正确:
import tensorflow as tf
import sklearn.feature_extraction
import numpy as np
def convert_csr_to_sparse_tensor_inputs(X):
coo = X.tocoo()
indices = np.mat([coo.row, coo.col]).transpose()
return indices, coo.data, coo.shape
X = ____ #Some sparse 2 x 8 csr matrix
y_input = np.asarray([1, 1, 1, 1, 1, 1, 1, 1])
y_input.shape = (8,1)
x_indices, x_values, x_shape = convert_csr_to_sparse_tensor_inputs(X)
# tf Graph input
y = tf.placeholder(tf.float64)
values = tf.placeholder(tf.float64)
indices = tf.placeholder(tf.int64)
shape = tf.placeholder(tf.int64)
# Create model
def multiply(values, indices, shape, y):
x_tensor = tf.SparseTensor(indices, values, shape)
out_layer = tf.sparse_tensor_dense_matmul(x_tensor, y)
return out_layer
pred = multiply(values, indices, shape, y)
# Launch the graph
with tf.Session() as sess:
result = sess.run(pred, feed_dict={values: x_values, indices: x_indices, shape: x_shape, y: y_input})
我是 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})
link https://github.com/tensorflow/tensorflow/issues/342 的某个人建议,作为解决方法,传入构建稀疏矩阵所需的元素,然后在函数内动态创建稀疏矩阵。这似乎有点老套,当我尝试以这种方式构建它时会出错。
任何帮助,尤其是代码答案,将不胜感激!
我想我明白了。我链接到的建议确实有效,我只需要更正所有输入以具有一致的类型。这是我在问题中列出的虚拟示例,编码正确:
import tensorflow as tf
import sklearn.feature_extraction
import numpy as np
def convert_csr_to_sparse_tensor_inputs(X):
coo = X.tocoo()
indices = np.mat([coo.row, coo.col]).transpose()
return indices, coo.data, coo.shape
X = ____ #Some sparse 2 x 8 csr matrix
y_input = np.asarray([1, 1, 1, 1, 1, 1, 1, 1])
y_input.shape = (8,1)
x_indices, x_values, x_shape = convert_csr_to_sparse_tensor_inputs(X)
# tf Graph input
y = tf.placeholder(tf.float64)
values = tf.placeholder(tf.float64)
indices = tf.placeholder(tf.int64)
shape = tf.placeholder(tf.int64)
# Create model
def multiply(values, indices, shape, y):
x_tensor = tf.SparseTensor(indices, values, shape)
out_layer = tf.sparse_tensor_dense_matmul(x_tensor, y)
return out_layer
pred = multiply(values, indices, shape, y)
# Launch the graph
with tf.Session() as sess:
result = sess.run(pred, feed_dict={values: x_values, indices: x_indices, shape: x_shape, y: y_input})