【问题标题】:Change filter on convolutional layer CNN - Python/TensorFlow更改卷积层 CNN 上的过滤器 - Python/TensorFlow
【发布时间】:2017-07-23 15:13:07
【问题描述】:

我有以下代码块:

def new_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

还有:

def new_conv_layer(input,              # The previous layer
                   use_pooling=True):  # Use 2x2 max-pooling

    shape = [3, 3, 1, 8]

    weights = new_weights(shape=shape)

    biases = new_biases(length=8)

    layer = tf.nn.conv2d(input=input,
                         filter=weights,
                         strides=[1, 1, 1, 1],
                         padding='SAME')

    layer += biases

    if use_pooling:
        layer = tf.nn.max_pool(value=layer,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')

    layer = tf.nn.relu(layer)

    # relu(max_pool(x)) == max_pool(relu(x)) we can
    # save 75% of the relu-operations by max-pooling first.

    return layer

所以我们可以观察到过滤器的大小是3x3,过滤器的数量是8。过滤器是用随机值定义的。

我需要做的是将我所有的8个过滤器定义为固定值,即预定值,例如:

weigths = [
    [[0,  1, 0,],[0, -1, 0,],[0,  0, 0,],],
    [[0,  0, 1,],[0, -1, 0,],[0,  0, 0,],],
    [[0,  0, 0,],[0, -1, 1,],[0,  0, 0,],],
    [[0,  0, 0,],[0, -1, 0,],[0,  0, 1,],],
    [[0,  0, 0,],[0, -1, 0,],[0,  1, 0,],],
    [[0,  0, 0,],[0, -1, 0,],[1,  0, 0,],], 
    [[0,  0, 0,],[1, -1, 0,],[0,  0, 0,],],
    [[1,  0, 0,],[0, -1, 0,],[0,  0, 0,],]
]

我无法想象,我怎么能在我的代码中做这个修改,有没有人知道我该怎么做?

非常感谢您!

【问题讨论】:

    标签: python python-2.7 tensorflow convolution


    【解决方案1】:

    在 TF2 中你可以这样做:

    model = models.Sequential()
    # one 3x3 filter
    model.add(layers.Conv2D(1, (3, 3), input_shape=(None, None, 1)))
    # access to the target layer
    layer = model.layers[0]
    current_w, current_bias = layer.get_weights()  # see the current weights
    new_w = tf.constant([[1,2, 3],
                         [4, 5, 6],
                         [7, 8, 9]])
    new_w = tf.reshape(new_w, custom_w.shape)  # fix the shape
    new_bias = tf.constant([0])
    layer.set_weights([new_w, new_bias])
    model.summary()
    # let's see ..
    tf.print(model.layers[0].get_weights())
    

    【讨论】:

      【解决方案2】:

      如果你想用一些预定义的值初始化权重,你可以使用tf.constant_initializer。如果您不想训练这些权重,可以将它们定义为 tf.constant 而不是 tf.Variable

      def new_weights(init_vaue, is_const):
          if (is_const) :
              return tf.constant(init_vaue, name='weights')
          else:
              initializer = tf.constant_initializer(init_vaue)
              return tf.get_variable('weights', shape = init_vaue.shape, initializer=initializer)
      
      weights = np.ones([3,3,1,8], dtype=np.float)
      print(weights.shape)
      
      value = new_weights(weights, True)
      with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          value_ = sess.run(value) 
          print(value_)
      

      【讨论】:

      • 起来,对不起。修复它。
      • 但是我在哪里设置我的 8 个过滤器?在new_weights()里面?
      • 您应该将weights = np.ones([3,3,1,8], dtype=np.float) 行更改为填充weights 数组的代码。
      【解决方案3】:

      您只需将权重定义为不可训练并将新权重定义为:

      new_weights = tf.Variable( tf.reshape(weights, (3,3,1,8)),trainable=False)
      # then apply on the inputs 
      layer = tf.nn.conv2d(inputs, filter=new_weights, strides=[1, 1, 1, 1], padding='SAME')
      

      【讨论】:

      • 没看懂,能多解释一下吗?
      • 我这样做并得到以下错误:TypeError: Input 'filter' of 'Conv2D' Op has type float64 that does not match type float32 of argument 'input'。
      • 将权重转换为 float32, tf.Variable(tf.reshape(tf.cast(weights, tf.float32), (3,3,1,8)), trainable=False),
      • 那样做,你不再使用 def new_weights(shape):?老实说,我不知道我是否正确设置了结构......
      猜你喜欢
      • 2018-11-24
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2016-07-08
      • 2015-12-15
      • 1970-01-01
      • 2021-09-11
      • 2019-11-28
      相关资源
      最近更新 更多