【问题标题】:Use coo_matrix in TensorFlow在 TensorFlow 中使用 coo_matrix
【发布时间】:2017-07-22 11:07:37
【问题描述】:

我正在 TensorFlow 中进行矩阵分解,我想使用 Spicy.sparse 中的 coo_matrix,因为它使用的内存更少,并且可以很容易地将我的所有数据放入我的矩阵中以用于训练数据。

tensorflow中是否可以使用coo_matrix初始化变量?

或者我是否必须创建一个会话并使用 sess.run() 和 feed_dict 将我得到的数据输入到 tensorflow 中。

我希望你理解我的问题和我的问题否则发表评论,我会尽力解决它。

【问题讨论】:

    标签: tensorflow sparse-matrix matrix-factorization


    【解决方案1】:

    最接近的东西tensorflow必须在scipy.sparse.coo_matrix 987654322 @,这是tf.Tensor 987654323 @。它可能最容易进入coo_matrix进入您的程序。

    a tf.SparseTensor是COO矩阵的轻微泛化,其中张量表示为三密度tf.Tensor对象:

    • indicesN 987654335 D 987654336 tf.int64 @ @ @ @ @ @ @ @ @ @ @ @的值,其中每行代表非零值的坐标。 N是非零的数量,而且@ 987654338是等效密度张量的等级(在矩阵的情况下2)。
    • values:一个长度 - N version的矢量,其中元素iindices 987654343 indices
    • dense_shapeD 987654345 tf.int64,表示等效的致密张量的形状。

    例如,您可以使用以下代码,该代码使用tf.sparse_placeholder()来定义您可以提供的@ 987654348 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)
    

    一旦您转换了coo_matrix到了tf.SparseTensorValue,您可以直接使用tf.SparseTensorValue 987654354 @ @ / p>

    sess.run(train_op, feed_dict={sparse_input: tf_coo_matrix})
    

    【讨论】:

    • 谢谢,这就是我想要的。但为什么你做稀疏_input = tf.sparse_placeholder(dtype = tf.float32,shape = [100,100]) span>
    • 它只是创建tf.SparseTensor的示例方法。您不必使用特定dtypeshape,您可以使用您的构造tf.SparseTensorValue(只要它具有兼容dtypeshape)。跨度>
    • 感谢您的快速响应。 span>
    • mrry,你似乎有关于sparsetysens的了解。当索引不是顺序时,我为什么收到错误?我不想订购我所拥有的所有索引,所以我希望你能提供帮助。 span>
    猜你喜欢
    • 1970-01-01
    • 2016-05-27
    • 2018-05-30
    • 2023-01-20
    • 2018-11-26
    • 2021-06-11
    • 2021-10-02
    • 2017-12-12
    • 2018-10-16
    相关资源
    最近更新 更多