培训网络后如何打印过滤器?训练网络后只想查看滤波器的权重值

时间:2018-10-04 07:43:03

标签: tensorflow

我有一个卷积,最大池化和一个全连接层的CNN网络。训练网络后,我想查看过滤器采用的权重的最终值。为此我必须做什么?我是否需要打印完全连接层的W矩阵?我是否需要更换一些砝码?我正在使用30的批次规模训练网络。

`
def weight_variable(shape):
    initial = tf.truncated_normal(shape, mean=0, stddev=0.1)
return tf.Variable(initial)
#network
x = tf.placeholder(tf.float32, [None, 
FLAGS.image_height*FLAGS.image_width])
y_ = tf.placeholder(tf.float32, [None, 2])



input=tf.reshape(x, 
   [-1,FLAGS.image_height,FLAGS.image_width,FLAGS.input_channel])
filter = weight_variable([FLAGS.filter_size, FLAGS.filter_size, 
   FLAGS.input_channel, FLAGS.filter_channel])
conv_out = tf.nn.sigmoid(conv2d(input, filter))

pool_out = max_pool(conv_out)


pool_list = pool_out.get_shape().as_list()
input_dim = pool_list[1]* pool_list[2]* pool_list[3]
pool_2D = tf.reshape(pool_out, [-1, input_dim])
W_fc = weight_variable([input_dim, 2])

logits = tf.matmul(pool_2D, W_fc)   #(batch_size,2)
y_conv=tf.nn.softmax(logits)
s=tf.argmax(y_conv,axis=1)
test = tf.reduce_sum(s)

cross_entropy = 
tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=y_)
loss = tf.reduce_mean(cross_entropy)
optimizer = tf.train.GradientDescentOptimizer(FLAGS.rLearn).minimize(loss)


correct_prediction = tf.equal(tf.argmax(y_conv,axis=1), tf.argmax(y_, 
axis=1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
#training loop
for i in range(200):`

1 个答案:

答案 0 :(得分:1)

我不明白您初始化权重矩阵的方式。

假设您以这种方式初始化体重矩阵:

def new_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05)) 

训练后,您可以通过以下方式获取权重变量的值:

# Retrieve the values of the weight-variables from TensorFlow.
# A feed-dict is not necessary because nothing is calculated.
w = session.run(weights)