tensorflow api笔记

获取全部的变量和 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]

  • 池化后的形状
为什么relu函数只需要用一个很小的正偏置就行?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值