如何微调迁移学习目标模型中神经网络的最后几层



我正在学习如何使用这些数据进行迁移学习https://www.kaggle.com/competitions/santander-customer-satisfaction/data。。这是我在tensorflow中的简单源模型代码。我正在保存这个型号的

import pandas as pd
pd.set_option('display.max_rows', None)
import numpy  as np
from tensorflow import keras
import matplotlib.pyplot as plt
import tensorflow as tf
""" # Read in the csv data using pandas 
train  = pd.read_csv('Z:ADwork2pythonPM/train.csv',index_col=0)
test   = pd.read_csv('Z:ADwork2pythonPM/test.csv', index_col=0)
sample = pd.read_csv('Z:ADwork2pythonPM/sample_submission.csv')
"""
# Read in the csv data using pandas 
train  = pd.read_csv('train.csv',index_col=0)
test   = pd.read_csv('test.csv', index_col=0)
sample = pd.read_csv('sample_submission.csv')

train.dtypes.value_counts()
train.select_dtypes(include=['int64']).nunique()
features_to_drop = train.nunique()
features_to_drop = features_to_drop.loc[features_to_drop.values==1].index
# now drop these columns from both the training and the test datasets
train = train.drop(features_to_drop,axis=1)
test  = test.drop(features_to_drop,axis=1)
train.isnull().values.any()

X = train.iloc[:,:-1]
y = train['TARGET']
y.value_counts().to_frame().T

from imblearn.over_sampling import SMOTE
X_resampled, y_resampled = SMOTE().fit_resample(X, y)

y_resampled.value_counts().to_frame().T
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X_resampled, y_resampled, 
train_size=0.5,
test_size=0.2, 
random_state=42, 
shuffle=True)

from sklearn.preprocessing import MinMaxScaler
scaler  = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_val   = scaler.transform(X_val)
test    = scaler.transform(test)

model = keras.Sequential(
[
keras.layers.Dense(units=9, activation="relu", input_shape=(X_train.shape[-1],) ),
# randomly delete 30% of the input units below
keras.layers.Dropout(0.3),
keras.layers.Dense(units=9, activation="relu"),
# the output layer, with a single neuron
keras.layers.Dense(units=1, activation="sigmoid"),
]
)
# save the initial weights for later
initial_weights = model.get_weights()
model.summary()
#keras.utils.plot_model(model, show_shapes=True)
learning_rate = 0.001
model.compile(optimizer=keras.optimizers.Adam(learning_rate=learning_rate), 
loss="binary_crossentropy", 
metrics=keras.metrics.AUC()
)
history = model.fit(X_train, y_train, 
epochs=500, 
batch_size=1000, 
validation_data=(X_val, y_val),
verbose=0)
from tensorflow.keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(
min_delta = 0.0002, # minimium amount of change to count as an improvement
patience  = 20,     # how many epochs to wait before stopping
restore_best_weights=True,
)
model.set_weights(initial_weights)
history = model.fit(X_train, y_train, 
epochs=500, 
batch_size=1000, 
validation_data=(X_val, y_val),
verbose=0,
# add in our early stopping callback
callbacks=[early_stopping]
)
sample['TARGET'] = model.predict(test)
sample.to_csv('submission.csv',index=False)
#tf.keras.models.save_model()
model.save('modelcentral.h5')

我正在保存这个模型,然后将这个模型加载到目标模型中的新python文件中

from pyexpat import model
import pandas as pd
pd.set_option('display.max_rows', None)
import numpy  as np
from tensorflow import keras
import matplotlib.pyplot as plt
import tensorflow as tf
import tryt
# Read in the csv data using pandas 
train  = pd.read_csv('train.csv',index_col=0)
test   = pd.read_csv('test.csv', index_col=0)
sample = pd.read_csv('sample_submission.csv')

train.dtypes.value_counts()
train.select_dtypes(include=['int64']).nunique()
features_to_drop = train.nunique()
features_to_drop = features_to_drop.loc[features_to_drop.values==1].index
# now drop these columns from both the training and the test datasets
train = train.drop(features_to_drop,axis=1)
test  = test.drop(features_to_drop,axis=1)
train.isnull().values.any()

X = train.iloc[:,:-1]
y = train['TARGET']
y.value_counts().to_frame().T

from imblearn.over_sampling import SMOTE
X_resampled, y_resampled = SMOTE().fit_resample(X, y)

y_resampled.value_counts().to_frame().T
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X_resampled, y_resampled, 
train_size=0.5,
test_size=0.2, 
random_state=42, 
shuffle=True)

from sklearn.preprocessing import MinMaxScaler
scaler  = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_val   = scaler.transform(X_val)
test    = scaler.transform(test)
#f.keras.models.load_model()
# It can be used to reconstruct the model identically.
model = keras.models.load_model("modelcentral.h5")
model.trainable=False
#layer1.trainable = False
#inputs = keras.Input(shape=(150, 150, 3))
learning_rate = 0.001
model.compile(optimizer=keras.optimizers.Adam(learning_rate=learning_rate), 
loss="binary_crossentropy", 
metrics=keras.metrics.AUC()
)
history = model.fit(X_train, y_train, 
epochs=500, 
batch_size=1000, 
validation_data=(X_val, y_val),
verbose=0)
model.summary()

现在我只是冻结所有的模型层,但如果我需要微调最后的层呢?例如,我在源模型中有二元分类,如果在目标模型中有多个分类呢。如何微调最后一层?我正在关注这个回购https://github.com/rasbt/stat453-deep-learning-ss21/blob/main/L14/5-transfer-learning-vgg16_small.ipynb学习用于转移学习的最终层的微调,但该代码在pytorch和图像数据中。。所以我很困惑

model.classifier[1].requires_grad = True
model.classifier[3].requires_grad = True
#For the last layer, because the number of class labels differs compared to ImageNet, we replace the output layer with your own output layer:
model.classifier[6] = torch.nn.Linear(4096, 10)

请帮忙,如果当前代码中有任何错误,请指导我

给定源模型:

import tensorflow as tf
model = tf.keras.Sequential(
[
tf.keras.layers.Dense(units=9, activation="relu", input_shape=(10,) ),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(units=9, activation="relu"),
tf.keras.layers.Dense(units=1, activation="sigmoid"),
])
model.save('model.h5')

你可以这样做,用其他层替换你的最后一层:

model = tf.keras.models.load_model("model.h5")
transfer_model = tf.keras.Sequential()
for idx, l in enumerate(model.layers):
if idx == len(model.layers) - 1:
transfer_model.add(tf.keras.layers.Dense(units=10, activation="softmax")) # add output layer with 10 different classes
else: transfer_model.add(l)

print(transfer_model.summary())

您可以使用l.trainable = True / False决定要冻结哪些图层或使其可训练。如果你喜欢的话,你也可以在没有for循环的情况下完成这一切:

model.layers[0].trainable = True
model.layers[2].trainable = True
outputs = tf.keras.layers.Dense(units=10, activation="softmax")(model.layers[-2].output)
transfer_model = tf.keras.Model(inputs=model.input, outputs=outputs)

最新更新