tensorflow在代码不同位置调用神经网络想要共享权重

一个简单的代码:
cnn网络定义了四个卷积层,权重为随机初始化的权重,输入为随机的(50,64,64,3)的向量
x1,x2的输出都调用了cnn这个函数

> import tensorflow as tf
> 
> def cnn(hidden):
>     kwargs = dict(strides=2, activation=tf.nn.relu)
>     hidden = tf.layers.conv2d(hidden, 32, 6, **kwargs)
>     hidden = tf.layers.conv2d(hidden, 64, 4, **kwargs)
>     hidden = tf.layers.conv2d(hidden, 128, 4, **kwargs)
>     hidden = tf.layers.conv2d(hidden, 256, 6, **kwargs)
>     return hidden
> 
> 
> x = tf.random_uniform(shape=(1,64,64,3)) x1 = cnn(x) x2 = cnn(x)
> 
> 
> with tf.Session() as sess:
>     sess.run(tf.global_variables_initializer())
>     x1,x2 = sess.run((x1,x2)) 

此时程序的输出为
x1:


[[[[0.02014228 0.         0.         0.         0.         0.01977238
    0.03838697 0.         0.02983167 0.02799959 0.         0.06397045
    0.00736133 0.         0.03821873 0.0057195  0.         0.
    0.04449557 0.         0.00071993 0.01928715 0.         0.00838473
    0.01739994 0.0649211  0.02711494 0.04341522 0.         0.
    0.         0.         0.00908231 0.         0.07131048 0.10422069
    0.04077441 0.         0.         0.         0.         0.
    0.         0.03626646 0.0288934  0.05571283 0.         0.04144262
    0.         0.         0.         0.0188169  0.07488646 0.
    0.02443938 0.03237902 0.         0.         0.05923894 0.
    0.03736772 0.03978631 0.         0.         0.02545817 0.05508033
    0.         0.02230431 0.         0.07633069 0.         0.
    0.08178918 0.         0.         0.         0.00414837 0.
    0.05754855 0.03113919 0.         0.         0.05155324 0.
    0.09053664 0.         0.0110682  0.0217151  0.06611994 0.02389411
    0.02607417...

x2:

[[[[0.00000000e+00 4.19012196e-02 0.00000000e+00 0.00000000e+00
    0.00000000e+00 0.00000000e+00 0.00000000e+00 2.27831006e-02
    0.00000000e+00 0.00000000e+00 1.51350833e-02 0.00000000e+00
    0.00000000e+00 0.00000000e+00 3.25169414e-02 0.00000000e+00
    0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
    0.00000000e+00 1.05397804e-02 4.10364196e-03 0.00000000e+00
    0.00000000e+00 2.62358692e-02 0.00000000e+00 0.00000000e+00
    0.00000000e+00 1.30762607e-02 0.00000000e+00 2.36116201e-02
    5.30595221e-02 0.00000000e+00 2.96120755e-02 4.43295240e-02
    6.72412962e-02 1.44396229e-02 6.79600835e-02 0.00000000e+00
    4.11668187e-03 2.66320305e-03 0.00000000e+00 6.97394609e-02
    0.00000000e+00 8.66800826e-03 4.97324169e-02 1.58384461e-02
    6.17823116e-02 2.54427711e-03 4.39084396e-02 7.62259439e-02
    3.68163846e-02 0.00000000e+00 4.27600034e-02 1.58234145e-02
    0.00000000e+00 1.29800113e-02 0.00000000e+00 4.92318310e-02
    0.00000000e+00 0.00000000e+00 0.0000...

若想让x1和x2输出一样的值:

import tensorflow as tf

def cnn(hidden):
    kwargs = dict(strides=2, activation=tf.nn.relu)
    hidden = tf.layers.conv2d(hidden, 32, 6, **kwargs)
    hidden = tf.layers.conv2d(hidden, 64, 4, **kwargs)
    hidden = tf.layers.conv2d(hidden, 128, 4, **kwargs)
    hidden = tf.layers.conv2d(hidden, 256, 6, **kwargs)
    return hidden


x = tf.random_uniform(shape=(1,64,64,3))
with tf.variable_scope("cnn"):
    x1 = cnn(x)
with tf.variable_scope("cnn",reuse=True):
    x2 = cnn(x)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    x1,x2 = sess.run((x1,x2))

此时x1和x2输出都是

[[[[0.         0.         0.         0.00473333 0.         0.
    0.         0.02189207 0.         0.02265558 0.         0.0203282
    0.         0.07767714 0.01926881 0.         0.         0.01530384
    0.05857469 0.         0.07027114 0.05844937 0.         0.00194241
    0.03735007 0.         0.05242127 0.0738355  0.         0.
    0.00370752 0.00985879 0.01877109 0.         0.         0.
    0.09586586 0.         0.         0.06655864 0.03449909 0.
    0.         0.04952947 0.05264677 0.0590784  0.         0.10032271
    0.         0.         0.         0.         0.         0.
    0.05604002 0.04438177 0.02950861 0.04137567 0.         0.05043553
    0.         0.         0.00433128 0.01197986 0.03473867 0.
    0.03830672 0.00620859 0.00809103 0.         0.         0.01776852
    0.05212681 0.         0.03483381 0.         0.         0.01069366
    0.02201606 0.01776179 0.         0.         0.         0.0233967
    0.02833988 0.         0.         0.         0.         0.02155995
...
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值