import tensorflow as tf
import numpy as np
# 设置随机种子以确保结果的可重复性
np.random.seed(0)
tf.random.set_seed(0)
# 定义网络参数
input_size = 4 # 输入层的神经元数量
hidden_size = 10 # 隐藏层的神经元数量
output_size = 1 # 输出层的神经元数量
# 创建输入数据和标签
# 假设我们有100个样本,每个样本有4个特征
x = np.random.rand(100, input_size).astype(np.float32)
y = np.random.rand(100, output_size).astype(np.float32)
# 定义模型类
class F(tf.keras.Model):
def __init__(self):
super(F, self).__init__()
# 设置为不可训练
self.dense1 = tf.keras.layers.Dense(hidden_size, activation='relu', input_shape=(input_size,))
def call(self, inputs):
# 前向传播函数
x = self.dense1(inputs)
return x
class S(tf.keras.Model):
def __init__(self):
super(S, self).__init__()
self.dense2 = tf.keras.layers.Dense(output_size)
def call(self, inputs):
# 前向传播函数
x = self.dense2(inputs)
return x
# 创建模型实例
input = tf.keras.layers.Input(shape=input_size)
f_instance = F()
# f_instance.trainable=False
s_instance = S()
mid = f_instance(input)
output = s_instance(mid)
model = tf.keras.Model(inputs=input, outputs=output)
# 编译模型
model.compile(optimizer='adam', loss='mse')
# for layer in model.layers:
# layer.trainable = True
# if layer.name == 'f':
# layer.trainable = False
# 训练模型
model.fit(x, y, epochs=1)
model.summary()
# 检查F的权重是否发生了变化
print("F's weights trainable after training:", f_instance.trainable)
# 比较F的权重在训练前后是否相同
initial_weights_f = f_instance.dense1.get_weights()
model.fit(x, y, epochs=1)
final_weights_f = f_instance.dense1.get_weights()
reload_weights_are_identical = np.all(
[np.array_equal(orig, load) for orig, load in zip(initial_weights_f, final_weights_f)])
print("F's weights 相同:", reload_weights_are_identical)
为什么蓝色的代码就可以让F模块的参数不变,而红色模块的代码就不行?