我正在尝试使用 SparseTensor 来表示完全连接层中的权重变量。
但是,似乎TensorFlow 0.8不允许将SparseTensor用作tf.Variable。
有什么办法可以解决这个问题?

我试过了

import tensorflow as tf

a = tf.constant(1)
b = tf.SparseTensor([[0,0]],[1],[1,1])

print a.__class__  # shows <class 'tensorflow.python.framework.ops.Tensor'>
print b.__class__  # shows <class 'tensorflow.python.framework.ops.SparseTensor'>

tf.Variable(a)     # Variable is declared correctly
tf.Variable(b)     # Fail

顺便说一句,我使用SparseTensor的最终目标是永久屏蔽密集形式的某些连接。因此,在计算和应用渐变时,将忽略这些修剪的连接

在我当前的MLP实现中,SparseTensor及其稀疏形式的matmul ops成功报告了推理输出。但是,使用SparseTensor声明的权重不会随着训练步骤的进行而受到训练。

最佳答案

作为解决问题的方法,可以为稀疏张量的值提供tf.Variable(直到Tensorflow v0.8)。在这种情况下,必须预先定义稀疏结构,但是权重仍然可以训练。

weights = tf.Variable(<initial-value>)
sparse_var = tf.SparseTensor(<indices>, weights, <shape>)  # v0.8
sparse_var = tf.SparseTensor(<indices>, tf.identity(weights), <shape>)  # v0.9

关于neural-network - 使用SparseTensor作为可训练变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37001686/

10-12 17:08