zoukankan      html  css  js  c++  java
  • 117、TensorFlow变量共享

    # sharing variables
    # Tensorflow supports two ways of sharing variables
    # 1、Explicitly passing tf.Variable objects around
    # 2、Implicitly wrapping tf.Variable objects within tf.variable_scope objects
    # For example , let's write a function to create a convolutional / relu layer
    import tensorflow as tf
    def conv_relu(input, kernel_shape, bias_shape):
        # Create variable named "weights"
        weights = tf.get_variable("weights", kernel_shape, initializer=tf.random_normal_initializer())
        # Create variable named "biases"
        biases = tf.get_variable("biases", bias_shape, initializer=tf.constant_initializer(0.0))
        # stride表示步长 , weight表示卷积核 , padding 表示卷积核是否可以停留在图像的边缘
        conv = tf.nn.conv2d(input, weights, strides=[1, 1, 1, 1], padding='SAME')
        return tf.nn.relu(conv + biases)
    
    
    # 这个函数使用了简写的weights和biases 
    # 在声明的时候是有帮助的,
    # 但是在真实的模型中,我们更想要以下的卷积层,重复调用这个函数是不会起作用的
    input1 = tf.random_normal([1, 10, 10, 32])
    input2 = tf.random_normal([1, 20, 20, 32])
    x = conv_relu(input1, kernel_shape=[5, 5, 32, 32], bias_shape=32)
    # This fails 不支持重复调用 , 因为想要的行为还不清楚
    # 是创建新的变量还是使用现有的变量
    # x = conv_relu(x, kernel_shape=[5, 5, 32, 32], bias_shape=[32]) 
  • 相关阅读:
    BZOJ3439: Kpm的MC密码
    BZOJ2819: Nim
    BZOJ1901: Zju2112 Dynamic Rankings
    Bzoj3230: 相似子串
    Bzoj4504: K个串
    CF609E. Minimum spanning tree for each edge
    bzoj1832: [AHOI2008]聚会
    css 笔记1
    namespace 相关
    cmd 命令
  • 原文地址:https://www.cnblogs.com/weizhen/p/8451478.html
Copyright © 2011-2022 走看看