在TensorFlow中使用coo_matrix [英] Use coo_matrix in TensorFlow
问题描述
我正在TensorFlow中进行矩阵分解,我想使用Spicy中的coo_matrix.sparse因为它使用的内存更少,并且可以轻松地将所有数据放入矩阵中以训练数据.
I'm doing a Matrix Factorization in TensorFlow, I want to use coo_matrix from Spicy.sparse cause it uses less memory and it makes it easy to put all my data into my matrix for training data.
是否可以使用coo_matrix在tensorflow中初始化变量?
Is it possible to use coo_matrix to initialize a variable in tensorflow?
还是我必须创建一个会话并使用sess.run()和feed_dict将我输入到tensorflow的数据馈入
Or do I have to create a session and feed the data I got into tensorflow using sess.run() with feed_dict.
我希望您理解我的问题,否则我的问题会发表评论,我会尽力解决.
I hope that you understand my question and my problem otherwise comment and i will try to fix it.
推荐答案
TensorFlow必须最接近 tf.SparseTensor
,它与 tf.Tensor
的稀疏等效.将coo_matrix
输入到程序中可能最简单.
The closest thing TensorFlow has to scipy.sparse.coo_matrix
is tf.SparseTensor
, which is the sparse equivalent of tf.Tensor
. It will probably be easiest to feed a coo_matrix
into your program.
A tf.SparseTensor
是COO矩阵的略微概括,其中张量表示为三个密集的tf.Tensor
对象:
A tf.SparseTensor
is a slight generalization of COO matrices, where the tensor is represented as three dense tf.Tensor
objects:
-
indices
:tf.int64
值的N
xD
矩阵,其中每一行代表一个非零值的坐标.N
是非零的数量,D
是等效密集张量的秩(对于矩阵,则为2). -
values
:值的长度N
向量,其中元素i
是其坐标在indices
的行i
上给出的元素的值. -
dense_shape
:tf.int64
的长度D
向量,表示等效密集张量的形状.
indices
: AnN
xD
matrix oftf.int64
values in which each row represents the coordinates of a non-zero value.N
is the number of non-zeroes, andD
is the rank of the equivalent dense tensor (2 in the case of a matrix).values
: A length-N
vector of values, where elementi
is the value of the element whose coordinates are given on rowi
ofindices
.dense_shape
: A length-D
vector oftf.int64
, representing the shape of the equivalent dense tensor.
例如,您可以使用以下代码,其中使用 tf.sparse_placeholder()
定义您可以提供的tf.SparseTensor
和 tf.SparseTensorValue
代表实际输入的值:
For example, you could use the following code, which uses tf.sparse_placeholder()
to define a tf.SparseTensor
that you can feed, and a tf.SparseTensorValue
that represents the actual value being fed :
sparse_input = tf.sparse_placeholder(dtype=tf.float32, shape=[100, 100])
# ...
train_op = ...
coo_matrix = scipy.sparse.coo_matrix(...)
# Wrap `coo_matrix` in the `tf.SparseTensorValue` form that TensorFlow expects.
# SciPy stores the row and column coordinates as separate vectors, so we must
# stack and transpose them to make an indices matrix of the appropriate shape.
tf_coo_matrix = tf.SparseTensorValue(
indices=np.array([coo_matrix.rows, coo_matrix.cols]).T,
values=coo_matrix.data,
dense_shape=coo_matrix.shape)
将coo_matrix
转换为tf.SparseTensorValue
后,可以直接将tf.SparseTensorValue
与tf.SparseTensorValue
一起馈入:
Once you have converted your coo_matrix
to a tf.SparseTensorValue
, you can feed sparse_input
with the tf.SparseTensorValue
directly:
sess.run(train_op, feed_dict={sparse_input: tf_coo_matrix})
这篇关于在TensorFlow中使用coo_matrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!