1.tf.Variable()相关
my_state = tf.Variable(0, name = "counter") #创建一个Op变量my_state,并初始化为0
one = tf.constant(1) #创建一个Op常量赋值为1
new_value = tf.add(my_state, one)
update = tf.assign(my_state, new_value) #通过assign()函数,将new_value的值赋给my_state.
init_Op = tf.global_variables_initializer() #tf.global_variables_initializer()
#会返回一个操作,初始化计算图中所有Variable对象
2.输出所有可训练的变量
variable_names = [v.name for v in tf.trainable_variables()]
print(variable_names)
3.strip()
.split('.')[-1]
#[-1]代表把最后一块切出来
4.读取,显示,resize图片
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
image_raw_data = tf.gfile.GFile('C:/Users/DELL/Desktop/CWTPic/train1.jpg','r').read() #加载原始图像
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data) #解码
plt.imshow(img_data.eval())
plt.show()
resized = tf.image.resize_images(img_data, [64,64],method=0) #第一个参数为原始图像,第二个参数为图像大小,第三个参数给出了指定的算法
resized = np.asarray(resized.eval(),dtype='uint8') #变为uint8才能显示
plt.imshow(resized)
plt.show()
5.tf.name_scope与tf.Variable_scope
对于使用tf.Variable来说,tf.name_scope和tf.variable_scope功能一样,都是给变量加前缀,相当于分类管理,模块化。
对于tf.get_variable来说,tf.name_scope对其无效,也就是说tf认为当你使用tf.get_variable时,你只归属于tf.variable_scope来管理共享与否。
来看一个例子:
with tf.name_scope('name_sp1') as scp1:
with tf.variable_scope('var_scp2') as scp2:
with tf.name_scope('name_scp3') as scp3:
a = tf.Variable('a')
b = tf.get_variable('b')
等同于
with tf.name_scope('name_sp1') as scp1:
with tf.name_scope('name_sp2') as scp2:
with tf.name_scope('name_scp3') as scp3:
a = tf.Variable('a')
with tf.variable_scope('var_scp2') as scp2:
b = tf.get_variable('b')
6.sess.run( )机制
在一个sess.run()环境中,一个大图里面的节点只会计算一次,哪怕有c->a->b这种依赖关系,运行sess.run([a,b])这种时候,a也只会被执行一次。其内部的机制是:先查看图中的依赖关系,得到本次sess.run()需要计算图中那些节点,然后进行一次计算,返回计算的a,b的值。