import tensorflow as tf
import keras
def get_model():
x1 = keras.layers.Dense(6, activation='relu',input_shape=(10,))
x2 = keras.layers.Dense(3, activation='relu')(x1)
output_ = keras.layers.Dense(10,acitvation='sigmoid')(x2)
model = keras.model(inputs=[x1], outputs=[output_])
return model
model = get_model()
model.compile(...)
chk_point = keras.callbacks.ModelCheckpoint(f'./best_model.h5',
monitor='val_loss', save_best_only=True, mode='min')
model.fit(..., callbacks=[chk_point])
def new_model():
old = '../best_model.h5' #using old model for training new model
(我正在嘗試使用預訓練的功能模型進行遷移學習)
現在我想得到 best_model 的所有層。如果可能,我想洗掉我的 best_model 的最后一層。我想凍結 best_model 的所有層,即 trainable = False。并向該模型添加新層。
我正在嘗試對訓練資料集進行去噪自動編碼器,其中 best_model.h5 的輸入和輸出相同(例如 input_shape=(100,) 和 output_shape=(100,))。然后我想凍結所有這些層并在向該模型添加新層之后洗掉該模型的最后一層。然后像往常一樣在 X 和 y 上訓練該模型
uj5u.com熱心網友回復:
一種方法是定義新模型,然后從舊模型(最后一層除外)復制層權重并將可訓練設定為 False。例如,假設您要洗掉最后一層并添加兩個密集層(這只是一個示例)。請注意,您當前模型的輸入和輸出大小為 (10,)。另請注意,函式式 API 中的第一層是輸入層。這是代碼:
import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split
import numpy as np
def get_model():
inputs = keras.Input(shape=(10, ))
x1 = keras.layers.Dense(6, activation='relu')(inputs)
x2 = keras.layers.Dense(3, activation='relu')(x1)
output_ = keras.layers.Dense(10,activation='sigmoid')(x2)
model = keras.Model(inputs=inputs, outputs=[output_])
return model
def get_new_model():
inputs = keras.Input(shape=(10, ))
x1 = keras.layers.Dense(6, activation='relu')(inputs)
x2 = keras.layers.Dense(3, activation='relu')(x1)
# new layers
x3 = keras.layers.Dense(15, activation='relu')(x2)
output_ = keras.layers.Dense(10, activation='sigmoid')(x3)
model = keras.Model(inputs=inputs, outputs=[output_])
return model
model = get_model()
model.compile(optimizer='adam', loss='mse')
batch_size = 16
_ = model.call(inputs=tf.random.normal(shape=(batch_size, 10)))
model.summary()
# create x data using two normal distributions with different mean
# y data is unused in auto encoder
x0 = np.random.normal(loc=0.0, size=(100, 10))
x1 = np.random.normal(loc=0.3, size=(100, 10))
x = np.concatenate((x0, x1), axis=0)
# y is unused
y0 = np.zeros((100, 10))
y1 = np.ones((100, 10))
y = np.concatenate((y0, y1), axis=0)
# split train/validation data
x_train, x_val, y_train, y_val = train_test_split(x, y, train_size=0.7)
print(x_train.shape)
print(y_train.shape)
chk_point = keras.callbacks.ModelCheckpoint(f'./best_model.h5',
monitor='val_loss', save_best_only=True, mode='min')
history = model.fit(x=x_train, y=x_train, batch_size=batch_size, epochs=100, callbacks=[chk_point], validation_data=(x_val, x_val))
# reload old model
model_old = keras.models.load_model('./best_model.h5')
model_old.summary()
# get new model
model_new = get_new_model()
model_new.compile(optimizer='adam', loss='mse')
_ = model_new.call(inputs=tf.random.normal(shape=(batch_size, 10)))
model_new.summary()
# copy the two dense layer weights and set trainable to False
# skip the first layer which is an InputLayer
for count, (layer_old, layer_new) in enumerate(zip(model_old.layers[1:3], model_new.layers[1:3])):
layer_new.trainable = False
layer_new.set_weights(layer_old.get_weights())
model_new.layers[count 1] = layer_new
model_new.summary()
uj5u.com熱心網友回復:
你可以做這樣的事情。請注意,只有 中的最后一層old_model可在 中訓練new_model:
import tensorflow as tf
def get_model():
_input = tf.keras.layers.Input(shape=(10,))
x1 = tf.keras.layers.Dense(6, activation='relu')(_input)
x2 = tf.keras.layers.Dense(3, activation='relu')(x1)
_output = tf.keras.layers.Dense(10, activation='sigmoid')(x2)
model = tf.keras.Model(inputs=_input, outputs=_output)
return model
model = get_model()
model.compile(optimizer='adam', loss='binary_crossentropy')
chk_point = keras.callbacks.ModelCheckpoint(f'./best_model.h5',
monitor='val_loss', save_best_only=True, mode='min')
x_train, y_train = tf.random.normal((100, 10)), tf.random.uniform((100, 10), maxval=2)
x_valid, y_valid = tf.random.normal((100, 10)), tf.random.uniform((100, 10), maxval=2)
model.fit(x_train, y_train, validation_data=(x_valid, y_valid),callbacks=[chk_point], epochs= 10)
old_model = tf.keras.models.load_model('best_model.h5')
for layer in old_model.layers[:-1]:
layer.trainable = False
_input = tf.keras.layers.Input(shape=(10,))
x = old_model(_input)
x1 = tf.keras.layers.Dense(6, activation='relu')(x)
x2 = tf.keras.layers.Dense(3, activation='relu')(x1)
_output = tf.keras.layers.Dense(10, activation='sigmoid')(x2)
new_model = tf.keras.Model(inputs=_input, outputs=_output)
new_model.compile(optimizer='adam', loss='binary_crossentropy')
x_train, y_train = tf.random.normal((100, 10)), tf.random.uniform((100, 10), maxval=2)
new_model.fit(x_train, y_train, epochs= 10)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/351703.html
標籤:Python 张量流 凯拉斯 keras keras层
