【问题标题】:How to read SciPy sparse matrix into Tensorflow's placeholder如何将 SciPy 稀疏矩阵读入 Tensorflow 的占位符
【发布时间】:2023-03-28 02:30:02
【问题描述】:

通过这种方式可以读取密集数据:

# tf - tensorflow, np - numpy, sess - session
m = np.ones((2, 3))
placeholder = tf.placeholder(tf.int32, shape=m.shape)
sess.run(placeholder, feed_dict={placeholder: m})

如何将 scipy 稀疏矩阵(例如 scipy.sparse.csr_matrix)读入 tf.placeholder 或 tf.sparse_placeholder?

【问题讨论】:

    标签: tensorflow scipy


    【解决方案1】:

    我认为目前 TF 没有从稀疏数据中读取的好方法。如果您不想将稀疏矩阵转换为密集矩阵,可以尝试构造一个sparse tensor.

    这是official tutorial 告诉你的:

    SparseTensors 不能很好地处理队列。如果你使用稀疏张量 您必须在之后使用 tf.parse_example 解码字符串记录 批处理(而不是在批处理之前使用 tf.parse_single_example)。

    【讨论】:

      【解决方案2】:

      将 SciPy 稀疏矩阵提供给 TF 占位符

      • 选项1:您需要使用tf.sparse_placeholder。在Use coo_matrix in TensorFlow 中显示了将数据提供给sparse_placeholder 的方式
      • 方案2:需要将稀疏矩阵转换为NumPy稠密矩阵并馈送到tf.place_holder(当然,当转换的稠密矩阵内存不足时,这种方式是不可能的)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 2020-12-07
        • 2014-12-21
        • 1970-01-01
        • 2017-03-26
        • 2017-03-31
        相关资源
        最近更新 更多