118、TensorFlow变量共享(二)

import tensorflow as tf
# 在不同的变量域中调用conv_relu,并且声明我们想创建新的变量
def my_image_filter(input_images):
    with tf.variable_scope("conv1"):
        # Variables created here will be named "conv1/weights" ,"conv1/biases"
        relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
    with tf.variable_scope("conv2"):
        # Variables created here will be named "conv2/weights" , "conv2/biases"
        return conv_relu(relu1, [5, 5, 32, 32], [32])


# 如果你想分享变量,你有两个选择,第一你可以创建一个有相同名字的变量域,使用reuse=True
with tf.variable_scope("model"):
    output1 = my_image_filter(input1)
with tf.variable_scope("model", reuse=True):    output2 = my_image_filter(input2)

# 你也可以调用scope.reuse_variables()来触发一个重用:
with tf.variable_scope("model") as scope:
    output1 = my_image_filter(input1)
    scope.reuse_variables()
    output2 = my_image_filter(input2)

# 因为解析一个变量域的名字是有危险的
# 通过一个变量来初始化另一个变量也是可行的
with tf.variable_scope("model") as scope:
    output1 = my_image_filter(input1)
with tf.variable_scope(scope, reuse=True):
    output2 = my_image_filter(input2)

 

转载于:https://www.cnblogs.com/weizhen/p/8451481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值