[英]reuse kernel weight in keras with tensorflow backend
I want to reuse kernel weight in keras with tensorflow backend. 我想在具有tensorflow后端的keras中重用内核权重。
tf.version = 1.11.0 tf.version = 1.11.0
My code is as following : 我的代码如下:
from tensorflow.python.keras.layers import Input
from tensorflow.python.keras import regularizers
def infer3(data_input, Reuse):
with tf.variable_scope('Network', reuse=Reuse):
inputs = Input(tensor = data_input)
network = tf.keras.layers.Conv2D(kernel_size=3, strides=2, filters=64, padding='same',kernel_regularizer=regularizers.l2(1),
activation='linear', kernel_initializer="glorot_normal", name='conv1', bias_initializer='zeros')(inputs)
return network
tf.reset_default_graph()
input_tensor = tf.placeholder(tf.float32,shape=[BATCH_SIZE,img_H,img_W,1])
output_tensor = tf.placeholder(tf.float32,shape=[BATCH_SIZE,img_H,img_W,1])
in_training_mode = tf.placeholder(tf.bool)
network = infer3(input_tensor,False)
network_test = infer3(input_tensor,True)
When I type " tf.trainable_variables() " 当我输入“ tf.trainable_variables()”时
It show message: 它显示消息:
[<tf.Variable 'Network/conv1/kernel:0' shape=(3, 3, 1, 64) dtype=float32>,
<tf.Variable 'Network/conv1/bias:0' shape=(64,) dtype=float32>,
<tf.Variable 'Network_1/conv1/kernel:0' shape=(3, 3, 1, 64) dtype=float32>,
<tf.Variable 'Network_1/conv1/bias:0' shape=(64,) dtype=float32>]
How can I reuse kernel weight? 如何重用内核权重?
As stated here , you can use get_weights()
method on an instance of layer to get its weights. 如前所述在这里 ,你可以使用get_weights()
上层的一个实例方法来获取它的权重。 Then you can assign those weights to a variable and initialize another layer of the same type and shape with those weights. 然后,您可以将这些权重分配给变量,并使用这些权重初始化具有相同类型和形状的另一层。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.