python中history()_keras中的History对象用法
keras中的fit_generator和fit函數(shù)均返回History對(duì)象,那么History怎么用呢?事實(shí)上History對(duì)象已經(jīng)記錄了運(yùn)行輸出。在了解之前,我們甚至自己定義回調(diào)函數(shù)記錄損失和準(zhǔn)確率等。
相關(guān)keras源碼位于網(wǎng)址:
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類對(duì)象包含兩個(gè)屬性,分別為epoch和history,epoch為訓(xùn)練輪數(shù)。
根據(jù)compile參數(shù)metrics,history包含不同的內(nèi)容。比如,當(dāng)某一次metrics=['accuracy']時(shí),運(yùn)行如下部分代碼我們可以看出,history字典類型,包含val_loss,val_acc,loss,acc四個(gè)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], ‘a(chǎn)cc': [0.630859375, 0.7509920634920635, 0.783203125]}
[0, 1, 2]
[0.4231100323200226, 0.3713115310668945, 0.3836631367206573]
補(bǔ)充知識(shí):在ipython中使用%history快速查找歷史命令
1、輸出所有歷史記錄,且?guī)в行蛱?hào)
%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、按序號(hào),查找某些序號(hào)區(qū)間的歷史紀(jì)錄
%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對(duì)象用法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
總結(jié)
以上是生活随笔為你收集整理的python中history()_keras中的History对象用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 破骸骨天赋(骸骨镀层天赋)
- 下一篇: python正则表达式面试_Python