代码拉取完成,页面将自动刷新
from tensorflow import keras
import numpy as np
data_x = np.random.normal(size=[1000, 1])
noise = np.random.normal(size=[1000, 1]) * 0.2
data_y = data_x * 3. + 2. + noise
train_x, train_y = data_x[:900], data_y[:900]
test_x, test_y = data_x[900:], data_y[900:]
# define your reusable layers in here
l1 = keras.layers.Dense(10, activation=keras.activations.relu)
class Model(keras.Model):
def __init__(self):
super(Model, self).__init__()
self.l1 = l1 # this is a reusable layer
self.l2 = keras.layers.Dense(1) # this is NOT a reusable layer
def call(self, x, training=None, mask=None):
x = self.l1(x)
x = self.l2(x)
return x
model1 = Model()
model2 = Model()
model1.build((None, 1))
model2.build((None, 1))
model1.compile(
optimizer=keras.optimizers.SGD(0.01),
loss=keras.losses.MeanSquaredError(),
metrics=[keras.metrics.MeanSquaredError()],
)
# train model1 for a while
model1.fit(train_x, train_y, batch_size=32, epochs=3, validation_split=0.2, shuffle=True)
print("l1 is reused: ", np.all(model1.l1.get_weights()[0] == model2.l1.get_weights()[0]))
print("l2 is reused: ", np.all(model1.l2.get_weights()[0] == model2.l2.get_weights()[0]))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。