【问题标题】:Sparse matrices in python tensorflow text classificationpython tensorflow文本分类中的稀疏矩阵
【发布时间】:2016-07-02 15:41:19
【问题描述】:

我一直在尝试使用 python 中的 tensorflow 包实现文本分类例程。我已经有一个在 scikit-learn 环境中工作的成功感知器版本,但 scikit-learn 没有多层神经网络(除了一些我似乎无法在任何地方找到/安装的神话版本 0.18)。

我认为最好先在 tensorflow 中尝试一些更简单的东西,以了解该包的工作原理以及它可以做什么和不能做什么,所以我选择了最近的邻居。到目前为止一切顺利,除了我找不到一种方法将词汇矩阵的稀疏版本(文本的词袋矢量化)提供给 tensorflow 中的占位符(在 scikit-learn 中,这根本没问题)。将词汇矩阵转换为密集矩阵可以解决问题,但会严重降低算法速度并阻塞 RAM。

有没有办法解决这个问题?从我在网上发现的情况来看,tensorflow 对稀疏对象的支持似乎非常有限(只有某些操作会接受它们作为输入),但我希望我错了。

附:是的,我阅读了this 线程,但它并没有解决我的问题。是的,我知道我可以坚持使用 scikit-learn 的感知器或选择其他软件包,但这是我将根据我在这里得到的答案做出的决定。

【问题讨论】:

  • 为什么不使用 tensorflow 中提供的词嵌入功能?
  • 这是否使用散列技巧(这是我的项目所必需的)并且它的行为是否像稀疏对象内存/处理方式?

标签: python-3.x tensorflow text-classification


【解决方案1】:

使用 TensorFlow 1.0.1,我可以做到这一点:

a = sparse.csr_matrix([[0, 1, 2], [5, 0, 0], [0, 0, 5],
                       [10, 1, 0], [0, 0, 4]])
# w = np.arange(6, dtype=np.float32).reshape([3, 2])
a = a.tocoo()

a_ = tf.sparse_placeholder('float32')
w_ = tf.Variable(tf.random_normal([3, 2], stddev=1.0))

a_sum = tf.sparse_reduce_sum(a_, 1)
a_mul = tf.sparse_tensor_dense_matmul(a_, w_)

# Initializing the variables
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    indices = np.array(zip(a.row, a.col), dtype=np.int32)
    values = np.array(a.data, dtype=np.float32)
    shape = np.array(a.shape, dtype=np.int32)

    print sess.run(a_mul, feed_dict={a_: tf.SparseTensorValue(indices, values, shape)})
    w = sess.run(w_)
    print np.dot(a.todense(), w)

您可以从 API 页面找到代码:sparse placeholder。在第一层之后,其他层的神经网络将是密集矩阵。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 2023-04-10
    • 2011-11-20
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2017-10-20
    相关资源
    最近更新 更多