我正在尝试使用 稀疏张量 表示全连接层中的权重变量。
但是,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/