數據分析師養成之路之keras:(Modelcheckpoint,交叉驗證等實現篇)


數據分析師養成之路之keras:(Modelcheckpoint,交叉驗證等實現篇)

魯魯醬1996 2018-07-23 14:40:27 4626 收藏 4
展開
1.數據集的划分和打亂:
主要學習一下,permutation的使用方法,代碼簡單,這里不多講解

data=np.load(open('/home/LLwang/桌面/wang/bottle_train.npy','rb'))
train_labels=fold1train_generator.classes
y=utils.to_categorical(train_labels,2)
permutation1=np.random.permutation(int(len(data)*0.30))
test_data=data[permutation1,:,:]
test_label=y[permutation1]
permutation2=np.random.permutation(int(len(data)*0.70))
train_data=data[permutation2,:,:]
train_label=y[permutation2]
1
2
3
4
5
6
7
8
9
2.keras中的交叉驗證:

from sklearn.model_selection import StratifiedKFold,train_test_split
# cross validation
# n-fold=5
skf = StratifiedKFold(n_splits=5)
for cnt,(train,test) in enumerate(skf.split(data,lic1)):
#注意,如何取數據!當然若是df型,df.iloc[train]取值
train_data=data[train,:,:]
test_data=data[test,:,:]
train_labels=lic1
y=utils.to_categorical(train_labels,2)
y_train=y[train]
y_test=y[test]
#只輸出最好的
filepath="/home/mrewang/桌面/wang/weights.best.hdf5"
#每提高一次,輸出一次
#filepath='weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5'
#為保存val_acc最大時模型的權重
mc=ModelCheckpoint(filepath,monitor='val_acc',verbose=1,save_best_only=True,mode='max')
callbacks_list=[mc]
model.fit(train_data,y_train,epochs=20, batch_size=32,validation_data=(test_data,y_test),callbacks=callbacks_list)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
3.load weight

model.load_weights('/home/LLwang/桌面/wang/weights.best.hdf5')
model.compile(optimizer='...',
loss='binary_crossentropy',metrics=['accuracy'])
scores=model.evaluate(test_data,test_label,verbose=0)
print('%s:%.2f%%'%(model.metrics_names[1],scores[1]*100))
print(model.predict(test_data))
————————————————
版權聲明:本文為CSDN博主「魯魯醬1996」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/lulujiang1996/java/article/details/81167362

 

 

 

在keras中使用交叉驗證或者網格搜索踩的坑

qq_28935065 2019-05-26 01:53:35 1629 收藏
展開
在keras中提供了sklearn的API:from keras.wrappers.scikit_learn import KerasClassifier,這個是分類的,也有回歸的,具體的使用方法

1.create_model。即創建自己的模型,在keras中自己根據需要搭模型,例如

    def create_model(self):
        """

        :return:
        """
        max_squence_len = 16
        max_token_num = 2874
        embedding_dim = 80
        input_layer=Input(shape=[max_squence_len,])
        embedding_layer=Embedding(input_dim=max_token_num, output_dim=embedding_dim)(input_layer)
        convs = []
        filter_sizes = [2, 3, 4]
        for fs in filter_sizes:
            l_conv = Conv1D(filters=50, kernel_size=fs, activation="relu")(embedding_layer)
            l_pool = MaxPooling1D(pool_size=2)(l_conv)
            ##pool_size:池化窗口的大小,如果在文中,表示每次選擇幾個詞也就是每次選擇幾行
            ##strides:步長,在文本中表示一次移動多少行
            ##padding:取值為’val’或者’same’,當取’same’時,會在進行填充,當取‘val‘不會進行填充
            l_pool = Flatten()(l_pool)
            ##Flatten層用來將輸入“壓平”,即把多維的輸入一維化,常用在從卷積層到全連接層的過渡
            convs.append(l_pool)
        merge = concatenate(convs,
                            axis=1)  ###將所有的卷積層都連接起來,除了使用concatenate以外,還可以使用add,兩者的不同在於add未改變維度,而concatenate可以理解為維度的聯合
        out = Dropout(0.5)(merge)  ##按照一定的概率將其暫時從網絡中丟棄
        output = Dense(32, activation='relu')(out)  ##全鏈接層
        output = Dense(units=31, activation='softmax')(output)  ##全鏈接輸出層
        model = Model([input_layer], output)
        adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)  ##優化器
        model.compile(loss="categorical_crossentropy", optimizer=adam, metrics=['accuracy'])
        return model

2.定義進行調參或者交叉驗證的方法,以供其他方法進行調用

    def adj_para(self,x_train, y_train):

        estimator=KerasClassifier(build_fn=self.create_model)
        batch_size = [10, 20, 30]
        epochs = [10, 50]
        param_grid = dict(batch_size=batch_size, nb_epoch=epochs)
        grid=GridSearchCV(estimator=estimator,param_grid=param_grid,cv=5,scoring = 'accuracy')
        grid_result=grid.fit(x_train,y_train)
        print("best_score:", grid_result.best_score_)
        print("best_para:", grid_result.best_params_)

注意:這兩個方法的定義所存在的類中,類的構造方法 def __init__(self):中,不能存在類變量 ,即在類構造方法中不能實例化其他類,否則會報TypeError: cannot deepcopy this pattern object。具體還不知道什么原因,只知道這樣做不會再報深復制的錯誤了。如果有哪位大神知道,還希望分享

具體實例

class A(object):

     def __init__(self):

         self.a="aa"

   

def create_model(self):
        """

        create model function

        :return:
        """

    def adj_para(self,x_train, y_train):

    """調參方法或者交叉驗證方法的定義"""

上面這種方式可以,但是下面的方式不可以

class A(object):

     def __init__(self):

         self.a="aa"

        self.objectB=B()###實例化B

   

def create_model(self):
        """

        create model function

        :return:
        """

    def adj_para(self,x_train, y_train):

    """調參方法或者交叉驗證方法的定義"""
————————————————
版權聲明:本文為CSDN博主「qq_28935065」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_28935065/java/article/details/90557676

 

 

 

keras入門 ---在預訓練好網絡模型上進行fine-tune

CIA_agent 2017-05-15 17:10:26 15466 收藏 4
展開
在深度學習的學習過程中,我們可能會用到一些已經訓練好的模型,比如 Alex Net, google net, VGG net, ResNet等,那我們怎么對這些已經訓練好的模型進行fine-tune來提高准確率呢?
在這篇博客中,我們使用已經訓練好的VGG16模型來幫助我們進行這個分類任務,因為我們要分類的是貓,狗這類物體,而VGG net是在imageNet上訓練的,而imageNet實際上已經包含了這2中物體。
我們的方法是這樣的:

首先載入VGG16的權重
接下來在初始化好的VGG網絡上添加我們預訓練好的模型
最后將最后一個卷積塊的層數凍結,然后以很低的學習率開始訓練(我們只選擇最后一個卷積塊進行訓練,是因為訓練樣本很少,而VGG模型層數很多,全部訓練肯定不能訓練好,會過擬合。 其次fine-tune時由於是在一個已經訓練好的模型上進行的,故權值更新應該是一個小范圍的,以免破壞預訓練好的特征)
首先構造VGG16模型:

model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height)))

model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
加載VGG16訓練好的權重(我們只要全連接層以前的權重):

assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
f = h5py.File(weights_path)
for k in range(f.attrs['nb_layers']):
if k >= len(model.layers):
# we don't look at the last (fully-connected) layers in the savefile
break
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
model.layers[k].set_weights(weights)
f.close()
print('Model loaded.')
1
2
3
4
5
6
7
8
9
10
11
然后在VGG16結構基礎上添加一個簡單的分類器及預訓練好的模型:

top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
top_model.load_weights(top_model_weights_path)
# add the model on top of the convolutional base
model.add(top_model)
1
2
3
4
5
6
7
8
把隨后一個卷積塊前的權重設置為不訓練:

for layer in model.layers[:25]:
layer.trainable = False
model.compile(loss='binary_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
1
2
3
4
5
這樣一個很簡單fine-tune在50個epoch后就可以達到一個大概0.94的auc.
源代碼和數據庫在code and dataset.
參考文檔https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html.
————————————————
版權聲明:本文為CSDN博主「CIA_agent」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/hnu2012/java/article/details/72179437


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM