繁体   English   中英

如何微调目标 model 中的最后一层神经网络以进行迁移学习(从二分类迁移到多分类)

[英]how to fine tune the last layers of neural network in target model for transfer learning (transfer from binary to multiclassification)

我正在学习如何使用这些数据进行迁移学习https://www.kaggle.com/competitions/santander-customer-satisfaction/data ..所以这是我在 Z2C39BC19B761ACFE36DC 中的简单源 model 代码我正在保存这个 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
""" # Read in the csv data using pandas 
train  = pd.read_csv('Z:\ADwork2\python\PM/train.csv',index_col=0)
test   = pd.read_csv('Z:\ADwork2\python\PM/test.csv', index_col=0)
sample = pd.read_csv('Z:\ADwork2\python\PM/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')

我正在保存这个 model 然后将此 model 加载到目标 Z20F35E630DAF3594DBFA4C3F6D8 中的新 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()

现在我只是冻结所有 model 层但是如果我需要微调最后一层怎么办,例如我在源 model 中有二进制分类,如果在目标 model 中有多分类。 如何微调最后一层? 我正在关注这个 repo 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)

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

鉴于您的来源 model:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM