我是靠谱客的博主 紧张美女,最近开发中收集的这篇文章主要介绍tf.varible_scope()和tf.AUTO_REUSE,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

class G():
    def __init__(self):
        with tf.variable_scope('a'):
            self.a=tf.Variable([[[0.1,0.2,0.5,0.2],
                [0.1,0.1,0.1,0.7]],

               [[0.2,0.3,0.2,0.3],
                [0.2,0.2,0.2,0.4]]])
        with tf.variable_scope('c'):
            outputs = self.conv(self.a)
            self.outputs = self.conv(outputs)
            outputs = self.conv(outputs)
            self.outputs1 = self.conv(outputs)
    def conv(self,inputs,scope="m",reuse=tf.AUTO_REUSE):
        # with tf.variable_scope(scope, reuse=reuse):
            outputs=tf.layers.conv1d(inputs=inputs,filters=3,kernel_size=1,activation=None,use_bias=False,
                                     kernel_initializer=tf.ones_initializer())
            outputs = tf.layers.conv1d(inputs=outputs, filters=4, kernel_size=1, activation=None, use_bias=False,kernel_initializer=tf.ones_initializer())
            # outputs=outputs #+ inputs
            return outputs

    def normalize(self,inputs,
                  epsilon=1e-8,
                  scope="ln",
                  reuse=None):

        with tf.variable_scope(scope, reuse=reuse):
            inputs_shape = inputs.get_shape()
            params_shape = inputs_shape[-1:]

            mean, variance = tf.nn.moments(inputs, [-1], keep_dims=True)
            beta = tf.Variable(tf.zeros(params_shape))
            gamma = tf.Variable(tf.ones(params_shape))
            normalized = (inputs - mean) / ((variance + epsilon) ** (.5))
            outputs = gamma * normalized + beta

        return outputs
g=G()
a=g.a
outputs=g.outputs
outputs1=g.outputs1


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(a.name)
    print(sess.run(outputs))
    print('o',outputs.name)
    print(sess.run(outputs1))
    print('o1',outputs1.name)

最后

以上就是紧张美女为你收集整理的tf.varible_scope()和tf.AUTO_REUSE的全部内容,希望文章能够帮你解决tf.varible_scope()和tf.AUTO_REUSE所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部