获取全部的变量和 trainable 变量
tf.global_variables()
获取全部的变量
tf.trainable_variables()
获取全部的trainable变量
二者返回的形式都是list类型
这个在后期的时候会用到,比如:我们定义两个不同的优化器,它们各自优化部分参数,这时候我们需要给它们传入各自需要优化的变量的list,这时候它们就只优化list中的变量,其他变量它就不算梯度了。
使用 tf.name_scope() 和 tf.variable_scope() 管理命名空间
with tf.name_scope('ns1') as ns:
ns_v1 = tf.Variable(initial_value=[1.0], name='v')
ns_gv1 = tf.get_variable(name='v', shape=[2,3])
ns_v2 = tf.Variable(initial_value=[1.0], name='v')
print('ns_v1', ns_v1)
print('ns_gv1', ns_gv1)
print('ns_v2', ns_v2)
ns_v1 <tf.Variable 'ns1/v:0' shape=(1,) dtype=float32_ref>
ns_gv1 <tf.Variable 'v:0' shape=(2, 3) dtype=float32_ref>
ns_v2 <tf.Variable 'ns1/v_1:0' shape=(1,) dtype=float32_ref>
结果就很清晰了,在 tf.name_scope() 里边:
使用 tf.Variable() 创建的变量,会自动地在 name 前面加上 scope name
使用 tf.get_variable() 创建的变量,并不受 name_scope 的影响
tf.nn.conv2d
tf.nn.conv2d(tf.nn.conv2d(
input,
filter=None,
strides=None,
padding=None,
use_cudnn_on_gpu=True,
data_format='NHWC',
dilations=[1, 1, 1, 1],
name=None,
filters=None
)
input shape : [batch, in_height, in_width, in_channels]
filter shape : [filter_height, filter_width, in_channels, out_channels]
strides shape : [batch, height, width, channels]
- 卷积后的形状:
输出宽或高 = ceil((输入宽或高-filter的宽或高+1)/步幅)
tf.nn.max_pool
tf.nn.max_pool(
value,
ksize,
strides,
padding,
data_format='NHWC',
name=None,
input=None
)
ksize shape : [batch, height, width, channels]
,但是一般为[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
strides shape : [batch, height, width, channels]
,和ksize一样 ,一般 也是[1, stride,stride, 1]
- 池化后的形状