在tensorflow 中一般数据都是用tensor来表示,而在python 中一般是用numpy包,然而有时候需要打印变量的数据,所以下面可以代码:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.1)
return initial
if __name__=='__main__':
'''
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784],'x')
y_ = tf.placeholder(tf.float32, [None, 10],'y')
x_image = tf.reshape(x, [-1, 28, 28, 1])
'''
W_conv1 = weight_variable([5, 5, 1, 32])
现在要打印W_conv1变量的值,首先看一下加入print(W_conv1)打印的效果
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.1)
return initial
if __name__=='__main__':
'''mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784],'x')
y_ = tf.placeholder(tf.float32, [None, 10],'y')
x_image = tf.reshape(x, [-1, 28, 28, 1])'''
W_conv1 = weight_variable([5, 5, 1, 32])
print (W_conv1)
# b_conv1 = bias_variable([32])
结果为:Tensor("truncated_normal:0", shape=(5, 5, 1, 32), dtype=float32),这是张量tensor,要打印这样的变量,需要在session 中,所以加入 with tf.Session() as sess 。程序如下:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.1)
return initial
if __name__=='__main__':
'''mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784],'x')
y_ = tf.placeholder(tf.float32, [None, 10],'y')
x_image = tf.reshape(x, [-1, 28, 28, 1])'''
W_conv1 = weight_variable([5, 5, 1, 32])
with tf.Session() as sess:
print (sess.run(W_conv1))
# b_conv1 = bias_variable([32])
打印结果为5*5*1*32,所有复制了部分数据如下:
[[-0.01138691 -0.06353019 0.12420362 0.14429896 0.01513781
-0.1557924 -0.11008668 -0.05706662 0.04268187 0.02811845
0.03623001 -0.15556422 -0.09083539 0.02959536 -0.00855448
0.09642988 -0.09893788 -0.04005608 -0.02273898 -0.03150641
-0.00355575 0.120373 0.03304343 -0.04112866 0.02824191
0.04924054 0.07960307 -0.0673811 0.0637029 -0.10225315
0.15535429 -0.01115945]]
[[ 0.1377502 -0.13991357 0.05424013 0.15169595 -0.09851914
0.0651127 0.05134506 0.0107875 0.01194856 0.00058991
0.02181729 -0.09146625 0.04761747 -0.06609621 0.03090276
-0.10576289 -0.00022165 0.06573081 -0.08087479 -0.06913969
0.05346059 -0.06754622 -0.08353492 0.025491 0.06887309
0.03608112 0.11575726 0.04321242 0.0215429 -0.07847684
-0.06896582 0.00389415]]]
通过.eval函数可以把tensor转化为numpy类数据,程序如下:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.1)
return initial
if __name__=='__main__':
'''mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784],'x')
y_ = tf.placeholder(tf.float32, [None, 10],'y')
x_image = tf.reshape(x, [-1, 28, 28, 1])'''
sess=tf.Session()
W_conv1 = weight_variable([5, 5, 1, 32])
a=W_conv1.eval(session=sess)
print (a)
# b_conv1 = bias_variable([32])
通过tf.convert_to_tensor函数可以把numpy转化为tensor 类数据:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.1)
return initial
if __name__=='__main__':
'''mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784],'x')
y_ = tf.placeholder(tf.float32, [None, 10],'y')
x_image = tf.reshape(x, [-1, 28, 28, 1])'''
sess=tf.Session()
W_conv1 = weight_variable([5, 5, 1, 32])
a=W_conv1.eval(session=sess)
b=tf.convert_to_tensor(a)
print (b)
# b_conv1 = bias_variable([32])