首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >卷积层的改变滤波器CNN - Python/TensorFlow

卷积层的改变滤波器CNN - Python/TensorFlow
EN

Stack Overflow用户
提问于 2017-07-23 15:13:07
回答 3查看 1.6K关注 0票数 0

我有以下代码块:

代码语言:javascript
复制
def new_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

和:

代码语言:javascript
复制
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个过滤器(例如,预定值):

代码语言:javascript
复制
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,],]
]

我无法想象,我如何在我的代码中,这个修改,谁知道我如何能做到这一点?

非常感谢您提前!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-07-23 16:45:23

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

代码语言:javascript
复制
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_)
票数 1
EN

Stack Overflow用户

发布于 2020-11-24 14:44:59

您可以在TF2中这样做:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-07-23 16:21:19

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

代码语言:javascript
复制
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')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45266654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档