我刚刚开始学习 TensorFlow,但遇到了一些问题。几天前,我阅读了这篇论文——深度压缩:使用剪枝、训练量化和霍夫曼编码压缩深度神经网络。在剪枝部分,作者首先通过正常的网络训练来学习连通性。接下来,他们修剪小权重的连接:所有权重低于阈值的连接都从网络中删除。最后,他们重新训练网络以学习剩余稀疏连接的最终权重。
我想获取每一层的所有权重,并与阈值一一比较,并将小权重设置为零。这是我的代码,有一个异常 TypeError("Using a tf.Tensor
as a Python bool
is not allowed.")。设计网络时如何获取权重张量的值?有没有人实现过这个代码或任何其他建议的方法?提前致谢!
with tf.variable_scope('layer1-conv1'):
conv1_weights = tf.get_variable(
"weight", [CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_DEEP],
initializer = tf.truncated_normal_initializer(stddev=0.1)
)
shapeDim=CONV1_SIZE*CONV1_SIZE*NUM_CHANNELS*CONV1_DEEP
reshape_w=tf.reshape(conv1_weights,[-1])
i=0
if step != 0 and step != 1:
while i < shapeDim:
if reshape_w[i] < RATIO:
reshape_w[i] = 0
conv1_weights=tf.reshape(reshape_w, [CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_DEEP])
conv1_biases = tf.get_variable("bias", [CONV1_DEEP], initializer=tf.constant_initializer(0.0))
conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))