Mar*_*edt 1 sparse-matrix matrix-factorization tensorflow
我在TensorFlow中进行矩阵分解,我想使用来自Spicy.sparse的coo_matrix,因为它使用更少的内存,并且可以很容易地将我的所有数据放入我的矩阵中来训练数据.
是否可以使用coo_matrix来初始化张量流中的变量?
或者我是否必须使用sess.run()和feed_dict创建会话并将我获得的数据提供给tensorflow.
我希望你理解我的问题和我的问题否则评论,我会尝试解决它.
mrr*_*rry 11
TensorFlow最接近的scipy.sparse.coo_matrix
是tf.SparseTensor
,它是稀疏的等价物tf.Tensor
.最简单的方法是将一个coo_matrix
程序输入程序.
A tf.SparseTensor
是COO矩阵的略微概括,其中张量表示为三个密集tf.Tensor
对象:
indices
:值的N
x D
矩阵,tf.int64
其中每行表示非零值的坐标.N
是非零的数量,并且D
是等效密集张量的等级(在矩阵的情况下为2).values
:N
值的长度向量,其中element i
是坐标在行i
上给出的元素的值indices
.dense_shape
:长度D
矢量tf.int64
,表示等效密集张量的形状.例如,您可以使用以下代码,该代码tf.sparse_placeholder()
用于定义tf.SparseTensor
您可以提供的代码,以及tf.SparseTensorValue
代表正在馈送的实际值的代码:
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)
Run Code Online (Sandbox Code Playgroud)
一旦你已经将您coo_matrix
的tf.SparseTensorValue
,您可以养活sparse_input
与tf.SparseTensorValue
直接:
sess.run(train_op, feed_dict={sparse_input: tf_coo_matrix})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3412 次 |
最近记录: |