我正在学习如何使用这些数据进行迁移学习.这是我在TensorFlow的simple source model code.我正在拯救这个模型

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')

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

现在我只是冻结所有模型层,但如果我需要微调最后一层怎么办,例如,我在源模型中有二进制分类,如果在目标模型中有多分类怎么办.我如何才能微调最后一层?我正在学习迁移学习的最终层的微调,但这个代码是在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)

Python相关问答推荐

将大小为n*512的数组绘制到另一个大小为n*256的数组的PC组件

无法获得指数曲线_fit来处理日期

ValueRight:参数目标和输出必须具有相同的形状.接收:目标.形状=(无,512),输出.形状=(无,3)

当变量也可以是无或真时,判断是否为假

具有多个组的条形图的不同y标度

有条件地采样我的大型DF的最有效方法

在函数内部使用eval(),将函数的输入作为字符串的一部分

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

重新匹配{ }中包含的文本,其中文本可能包含{{var}

难以在Manim中正确定位对象

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

为什么符号没有按顺序添加?

将两只Pandas rame乘以指数

scikit-learn导入无法导入名称METRIC_MAPPING64'

关于Python异步编程的问题和使用await/await def关键字

利用Selenium和Beautiful Soup实现Web抓取JavaScript表

Pandas—在数据透视表中占总数的百分比

如何在TensorFlow中分类多个类

为什么常规操作不以其就地对应操作为基础?

在代码执行后关闭ChromeDriver窗口