tf.variable_scope & tf.varaible_scope
2018-02-28 17:48
246 查看
# -*- coding:utf-8 -*- import tensorflow as tf # 设置GPU按需增长 #config = tf.ConfigProto() #config.gpu_options.allow_growth = True #sess = tf.Session(config=config) sess = tf.Session() # 1.placeholder v1 = tf.placeholder(tf.float32, shape=[2,3,4]) print v1.name v1 = tf.placeholder(tf.float32, shape=[2,3,4], name='ph') print v1.name v1 = tf.placeholder(tf.float32, shape=[2,3,4], name='ph') print v1.name print type(v1) print v1 # 2. tf.Variable() v2 = tf.Variable([1,2], dtype=tf.float32) print v2.name v2 = tf.Variable([1,2], dtype=tf.float32, name='V') print v2.name v2 = tf.Variable([1,2], dtype=tf.float32, name='V') print v2.name print type(v2) print v2 with tf.name_scope('nsc1'): v1 = tf.Variable([1], name='v1') with tf.variable_scope('vsc1'): v2 = tf.Variable([1], name='v2') v3 = tf.get_variable(name='v3', shape=[]) print 'v1.name: ', v1.name print 'v2.name: ', v2.name print 'v3.name: ', v3.name ''' 第一组,用 tf.Variable() 的方式来定义。 ''' # 拿官方的例子改动一下 def my_image_filter(): conv1_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]), name="conv1_weights") conv1_biases = tf.Variable(tf.zeros([32]), name="conv1_biases") conv2_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]), name="conv2_weights") conv2_biases = tf.Variable(tf.zeros([32]), name="conv2_biases") return None # First call creates one set of 4 variables. result1 = my_image_filter() # Another set of 4 variables is created in the second call. result2 = my_image_filter() # 获取所有的可训练变量 vs = tf.trainable_variables() print 'There are %d train_able_variables in the Graph: ' % len(vs) for v in vs: print v ''' 2.第二种方式,用 tf.get_variable() 的方式 ''' # 下面是定义一个卷积层的通用方式 def conv_relu(kernel_shape, bias_shape): # Create variable named "weights". weights = tf.get_variable("weights", kernel_shape, initializer=tf.random_normal_initializer()) # Create variable named "biases". biases = tf.get_variable("biases", bias_shape, initializer=tf.constant_initializer(0.0)) return None def my_image_filter(): # 按照下面的方式定义卷积层,非常直观,而且富有层次感 with tf.variable_scope("conv1"): # Variables created here will be named "conv1/weights", "conv1/biases". relu1 = conv_relu([5, 5, 32, 32], [32]) with tf.variable_scope("conv2"): # Variables created here will be named "conv2/weights", "conv2/biases". return conv_relu( [5, 5, 32, 32], [32]) with tf.variable_scope("image_filters") as scope: # 下面我们两次调用 my_image_filter 函数,但是由于引入了 变量共享机制 # 可以看到我们只是创建了一遍网络结构。 result1 = my_image_filter() scope.reuse_variables() result2 = my_image_filter() # 看看下面,完美地实现了变量共享!!! vs = tf.trainable_variables() print 'There are %d train_able_variables in the Graph: ' % len(vs) for v in vs: print v
相关文章推荐
- tf.name_scope&tf.variable scope
- tensorflow 变量生成 变量管理 tf.Variable & tf.get_variable & tf.variable_scope
- tf.name_scope() 和 tf.variable_scope() 的用法和玄机
- tf.variable_scope官方文档
- tf.get_variable 和tf.variable_scope
- Understand on the tf.variable_scope and tf.name_scope
- tf.variable_scope官方文档
- tf.variable_scope()和tf.name_scope(),tf.Variable()和tf.get_variable()
- tf.variable_scope官方文档
- tf.get_variable tf.variable_scope tf.name_scope
- tf.variable_scope和tf.name_scope的用法
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同
- name&variable scope
- tensorflow中使用tf.variable_scope和tf.get_variable的ValueError
- tensorflow学习笔记(十七):name&variable scope
- tf.variable_scope()和tf.name_scope()函数的区别
- tf.get_variable 和tf.variable_scope
- tensorflow学习之路(2-1):tf.variable_scope(), tf.name_scope(), tf.get_variable()的认识
- tf.name_scope() 与 tf.variable_scope() 的区别