在TensorFlow中使用coo_matrix [英] Use coo_matrix in TensorFlow

查看:502
本文介绍了在TensorFlow中使用coo_matrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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 x D矩阵,其中每一行代表一个非零值的坐标. N是非零的数量,D是等效密集张量的秩(对于矩阵,则为2).
  • values:值的长度N向量,其中元素i是其坐标在indices的行i上给出的元素的值.
  • dense_shape:tf.int64的长度D向量,表示等效密集张量的形状.
  • indices: An N x D matrix of tf.int64 values in which each row represents the coordinates of a non-zero value. N is the number of non-zeroes, and D is the rank of the equivalent dense tensor (2 in the case of a matrix).
  • values: A length-N vector of values, where element i is the value of the element whose coordinates are given on row i of indices.
  • dense_shape: A length-D vector of tf.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.SparseTensorValuetf.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆