python中history()_keras中的History对象用法
keras中的fit_generator和fit函數均返回History對象,那么History怎么用呢?事實上History對象已經記錄了運行輸出。在了解之前,我們甚至自己定義回調函數記錄損失和準確率等。
相關keras源碼位于網址:
class History(Callback):
"""Callback that records events into a `History` object.
This callback is automatically applied to
every Keras model. The `History` object
gets returned by the `fit` method of models.
"""
def on_train_begin(self, logs=None):
self.epoch = []
self.history = {}
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
self.epoch.append(epoch)
for k, v in logs.items():
self.history.setdefault(k, []).append(v)
可以看出History類對象包含兩個屬性,分別為epoch和history,epoch為訓練輪數。
根據compile參數metrics,history包含不同的內容。比如,當某一次metrics=['accuracy']時,運行如下部分代碼我們可以看出,history字典類型,包含val_loss,val_acc,loss,acc四個key值。
####省略若干
history = model.fit_generator(
mp.train_flow,
steps_per_epoch=32,
epochs=3,
validation_data=mp.test_flow,
validation_steps=32)
print(history.history)
print(history.epoch)
print(history.history['val_loss'])
{‘val_loss': [0.4231100323200226, 0.3713115310668945, 0.3836631367206573], ‘val_acc': [0.815, 0.84, 0.83], ‘loss': [0.8348453622311354, 0.5010451343324449, 0.4296100065112114], ‘acc': [0.630859375, 0.7509920634920635, 0.783203125]}
[0, 1, 2]
[0.4231100323200226, 0.3713115310668945, 0.3836631367206573]
補充知識:在ipython中使用%history快速查找歷史命令
1、輸出所有歷史記錄,且帶有序號
%history -n
150: %cpaste
151: %cpaste
152: print(r">>>>>>>>>")
153: print(r'>>>>>>>>>')
154: print(r'>>>>>>>>>
155: print(r'>')
156: print(r'>>')
157: print(r'>>>')
...
2、按序號,查找某些序號區間的歷史紀錄
%history -n 168-170 178 185-190
168: planets
169:
for method, group in planets.groupby('method'):
print(f'{method:30s} method={group}')
170:
for method, group in planets.groupby('method'):
print(f'{method:30s} method={group.shape}')
178: %history?
185: %history -u
186: %history -n -u
187: ?%history
188: %history -g method
189: %history -g method print
190: %history -g for method,
3、模糊查找
%history -g print*metho*
120:
for method, group in planets.groupby('method'):
print(f"{method:30s} shape={groupe.shape}")
121:
for method, group in planets.groupby('method'):
print(f"{method:30s} shape={group.shape}")
169:
for method, group in planets.groupby('method'):
print(f'{method:30s} method={group}')
170:
for method, group in planets.groupby('method'):
print(f'{method:30s} method={group.shape}')
182:
for method, group in planets.groupby('method'):
print(f"{method:30s shape=group.shape}")
198: %history -g print*metho*
以上這篇keras中的History對象用法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的python中history()_keras中的History对象用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 破骸骨天赋(骸骨镀层天赋)
- 下一篇: 怎样重置路由器wifi密码wifi路由器