1、怎樣在GPU上運行Keras
- 如果是使用TensorFlow或者CNTK作爲後端的話,在檢測到GPU後,代碼是會自動在GPU上運行的。
- 如果使用Theano作爲後端,可以在代碼的前面添加以下的語句就可以讓代碼在GPU上運行
import theano
theano.config.device = 'gpu'
theano.config.floatX = 'float32'
2、sample, batch, epoch的含義
- sample:數據集裏的一個元素,比如卷積網絡裏面的一個圖像就是一個sample,再或者語音識別模型中的一個音頻文件就是一個sample。
- batch:N個sample的集合。一個batch裏面的sample都是相互獨立的進行處理的。如果是在進行訓練,那麼一個batch只造成一個更新。一個batch通常會更接近數據的分佈,batch越大,越接近數據的分佈,不過太大會造成更長的處理時間,並且也只進行一次更新。在evaluate或predict中,儘可能的是選擇較大的batch。(大的batch在evaluate和predict中能夠更快進行結果更新)
- eopch:將訓練分成不同的階段,有利於記錄信息和定期進行evaluate。在fit方法裏,當使用evaluation_data或evaluation_split參數時,在每一次epoch後都會進行一次evaluate。
- 在Keras裏,可以在每次epoch結束時,使用專門設計的callbacks。(比如說學習率的改變)
3、保存模型(architecture + weights + optimizer state)
- 使用model.save(filepath)將Keras模型保存爲一個HDF5文件:
模型的結構,可用來重構模型
模型的權重
訓練的一些配置(loss, optimizer) 優化器的狀態,可以從離開的位置繼續開始訓練
- 使用keras.models.load_model(filepath)來重新實例化一個模型。
- 如果只是想保存模型的結構,沒有權重,也沒有訓練配置參數,那麼可以使用:
json_string = model.to_json()
yaml_string = model.to_yaml()
這些文件都是可讀的,也可以被編輯
可以從這些文件裏重建一個模型
from keras.models import model_from_json
model = model_from_json(json_string)
from keras.models import model_from_yaml
model = model_from_yaml(yaml_string)
只保存模型的權重,首先是要安裝HDF5和python的h5py庫
model.save_weights('my_model_weights.h5')
若實例化了一個模型後,可以根據以下語句來將保存的權重加載到這個模型上
model.load_weights('my_model_weights.h5')
如果想要將權重加載到不同結構的模型上(有一些層是相同的),例如在進行微調或學習轉化時,可以通過層的名字來加載權重
model.load_weights('my_model_weights.h5', by_name=True)
"""
Assuming the original model looks like this:
model = Sequential()
model.add(Dense(2, input_dim=3, name='dense_1'))
model.add(Dense(3, name='dense_2))
...
model.save_weights(fname)
"""
# new model
model = Sequential()
model.add(Dense(2, input_dim=3, name='dense_1')) # will be loaded
model.add(Dense(10, name='new_dense')) # will not be loaded
# load weights from first model
# will only affect the first layer, dense_1
model.load_weights(fname, by_name=True)
處理自定義的層,如果想要導入的模型包含有自定義的層,那麼可以將他們傳遞給custom_objects參數
from keras.models import load_model
# Assuming you model includes instance of an "AttentionLayer" class
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLaer})
或者
from keras.utils import CustomObjectScope
with CustomObjectScope({'AttentionLayer': AttentionLayer}):
model = load_model('my_model.h5')
from keras.models import model_from_json
model = model_from_json(json_string, custom_objects={'AttentionLayer': AttentionLayer})
4、爲什麼訓練的loss要比測試的loss要高
- 一個Keras模型擁有兩個模式:training and testing。
- 正則機制——Dropout,L1/L2正則化方法這些在testing時是不會工作的。
- 另外,訓練的loss是訓練數據中每個batch的loss的平均,隨着模型的改變,第一次batch的loss通常比後面batch的loss要高。另一方面,測試loss是根據模型的最後一次epoch的loss得到的,因此loss會較小。
5、如何獲得中間層的輸出
一個簡單的辦法是建一個新的模型
from keras.models import Model
model = ... # create the original model
layer_name = 'my_layer'
intermediate_layer_model = Model(inputs=model.input, output=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)
或者,定義一個keras函數,輸入是給定的輸入,輸出的特定層的輸出
from keras import backend as K
# with a Sequential model
get_3rd_layer_output = K.function([model.layers[0].input], [model.layers[3].output])
layer_output = get_3rd_layer_output([x])[0]
6、如何不將數據全部導入內存
- 可以使用model.train_on_batch(x, y)和model.test_on_batch(x, y)
- 或者,使用generator來產生訓練數據集的的batch,
model.fit_generator(data_generator, steps_per_epoch, epochs)
7、當validation的loss不再下降時,怎樣中止訓練
EarlyStopping callback
from keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
model.fit(x, y, validation_split=0.2, callbacks=[early_stopping])
8、validation split 是怎麼計算的
如果在fit方法裏設置了參數validation_split=0.1,那麼數據集後面的10%將作爲驗證數據集。
9、在訓練時,數據集的順序會打亂嗎
- 如果fit方法裏的shuffle參數設置爲True,那麼在每次epoch時,數據集都會被隨機打亂。
validation data is never shuffled.
10、記錄每次epoch的loss和accuracy
hist = model.fit(x, y, validation_split=0.2)
print(hist.history)
# history屬性包含了連續的loss和其他的度量
11、凍結keras的某一層
凍結的意思是,在訓練過程中,某一層的權重永遠不會更新。這個方法在對模型進行微調時很有用。
frozen_layer = Dense(32, trainable=False) # 權重是不會更新的
也可以在實例化後設置層的trainable屬性。在修改了trainable的屬性後,需要對模型進行編譯
x = Input(shape=(32, ))
layer = Dense(32)
layer.trainable = False
y = layer(x)
frozen_model = Model(x, y)
# in the model below, the weights of 'layer' will not be updated
# during training
frozen_model.compile(optimizer='rmsprop', loss='mse')
layer.trainable = True
trainable_model = Model(x, y)
# with this model the weights of the layer will be updated during training
# (which will alse affect the above model since it uses the same layer instance)
trainable_model.compile(optimizer='rmsprop', loss='mse')
frozen_model.fit(data, labels) # this does not update the weights of 'layer'
trainable_model.fit(data, labels) # this updates the weights of 'layer'
12、如何使用stateful RNNs
RNN stateful是指每一個batch裏面samples的狀態將用於下一個batch裏sample的狀態
x # 假設是輸入數據,shape:(32, 21, 16)
# we will feed it to our model in sequences of length 10
model = Sequential()
model.add(LSTM(32, input_shape=(10, 16), batch_size=32, stateful=True))
model.add(Dense(16, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
# we train the network to predict the 11th timestep give the first 10
model.train_on_batch(x[:, :10, :], np.reshape(x[:, 10, :], (32, 16)))
# the state of the network has changed. we can feed the follow-up sequences:
model.train_on_batch(x[:, 10:20, :], np.reshape(x[:, 20, :], (32, 16)))
# let's reset the states of the LSTM layer:
model.reset_states()
# another way to do it in this case:
model.layers[0].reset_states()
注意:predict , fit , train_on_batch , predict_classes這些方法都會更新stateful layers的狀態
13、Keras的配置文件保存在哪裏
如果是window系統,則在用戶文件夾裏,比如說C:\Users\xxx.keras\keras.json
{
"backend": "tensorflow",
"floatx": "float32",
"epsilon": 1e-07,
"image_dim_ordering": "tf"
"image_data_format": "channels_last"
}
那些通過get_file()方法下載得到的數據集則在C:\Users\xxx.keras\datasets
14、移除Sequential模型中的層
目前通常model.pop()可以刪除model最後添加的那個層
15、
16、
17、
18、
19、
20、