最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

keras訓練曲線,混淆矩陣,CNN層輸出可視化實例

 更新時間:2020年06月15日 11:50:17   作者:3D_DLW  
這篇文章主要介紹了keras訓練曲線,混淆矩陣,CNN層輸出可視化實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

訓練曲線

def show_train_history(train_history, train_metrics, validation_metrics):
 plt.plot(train_history.history[train_metrics])
 plt.plot(train_history.history[validation_metrics])
 plt.title('Train History')
 plt.ylabel(train_metrics)
 plt.xlabel('Epoch')
 plt.legend(['train', 'validation'], loc='upper left')

# 顯示訓練過程
def plot(history):
 plt.figure(figsize=(12, 4))
 plt.subplot(1, 2, 1)
 show_train_history(history, 'acc', 'val_acc')
 plt.subplot(1, 2, 2)
 show_train_history(history, 'loss', 'val_loss')
 plt.show()

效果:

plot(history)

混淆矩陣

def plot_confusion_matrix(cm, classes,
    title='Confusion matrix',
    cmap=plt.cm.jet):
 cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
 plt.imshow(cm, interpolation='nearest', cmap=cmap)
 plt.title(title)
 plt.colorbar()
 tick_marks = np.arange(len(classes))
 plt.xticks(tick_marks, classes, rotation=45)
 plt.yticks(tick_marks, classes)
 thresh = cm.max() / 2.
 for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
 plt.text(j, i, '{:.2f}'.format(cm[i, j]), horizontalalignment="center",
   color="white" if cm[i, j] > thresh else "black")
 plt.tight_layout()
 plt.ylabel('True label')
 plt.xlabel('Predicted label')
 plt.show()

# 顯示混淆矩陣
def plot_confuse(model, x_val, y_val):
 predictions = model.predict_classes(x_val)
 truelabel = y_val.argmax(axis=-1) # 將one-hot轉化為label
 conf_mat = confusion_matrix(y_true=truelabel, y_pred=predictions)
 plt.figure()
 plot_confusion_matrix(conf_mat, range(np.max(truelabel)+1))

其中y_val以one-hot形式輸入

效果:

x_val.shape # (25838, 48, 48, 1)
y_val.shape # (25838, 7)
plot_confuse(model, x_val, y_val)

CNN層輸出可視化

# 卷積網(wǎng)絡可視化
def visual(model, data, num_layer=1):
 # data:圖像array數(shù)據(jù)
 # layer:第n層的輸出
 data = np.expand_dims(data, axis=0) # 開頭加一維
 layer = keras.backend.function([model.layers[0].input], [model.layers[num_layer].output])
 f1 = layer([data])[0]
 num = f1.shape[-1]
 plt.figure(figsize=(8, 8))
 for i in range(num):
 plt.subplot(np.ceil(np.sqrt(num)), np.ceil(np.sqrt(num)), i+1)
 plt.imshow(f1[0, :, :, i] * 255, cmap='gray')
 plt.axis('off')
 plt.show()

num_layer : 顯示第n層的輸出

效果

visual(model, data, 1) # 卷積層
visual(model, data, 2) # 激活層
visual(model, data, 3) # 規(guī)范化層
visual(model, data, 4) # 池化層

補充知識:Python sklearn.cross_validation.train_test_split及混淆矩陣實現(xiàn)

sklearn.cross_validation.train_test_split隨機劃分訓練集和測試集

一般形式:

train_test_split是交叉驗證中常用的函數(shù),功能是從樣本中隨機的按比例選取train data和testdata,形式為:

X_train,X_test, y_train, y_test =
cross_validation.train_test_split(train_data,train_target,test_size=0.4, random_state=0)

參數(shù)解釋:

train_data:所要劃分的樣本特征集

train_target:所要劃分的樣本結果

test_size:樣本占比,如果是整數(shù)的話就是樣本的數(shù)量

random_state:是隨機數(shù)的種子。

隨機數(shù)種子:其實就是該組隨機數(shù)的編號,在需要重復試驗的時候,保證得到一組一樣的隨機數(shù)。比如你每次都填1,其他參數(shù)一樣的情況下你得到的隨機數(shù)組是一樣的。但填0或不填,每次都會不一樣。隨機數(shù)的產(chǎn)生取決于種子,隨機數(shù)和種子之間的關系遵從以下兩個規(guī)則:種子不同,產(chǎn)生不同的隨機數(shù);種子相同,即使實例不同也產(chǎn)生相同的隨機數(shù)。

示例

fromsklearn.cross_validation import train_test_split
train= loan_data.iloc[0: 55596, :]
test= loan_data.iloc[55596:, :]
# 避免過擬合,采用交叉驗證,驗證集占訓練集20%,固定隨機種子(random_state)
train_X,test_X, train_y, test_y = train_test_split(train,
             target,
             test_size = 0.2,
             random_state = 0)
train_y= train_y['label']
test_y= test_y['label']

plot_confusion_matrix.py(混淆矩陣實現(xiàn)實例)

print(__doc__)

import numpy as np
import matplotlib.pyplot as plt

from sklearn import svm, datasets
from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix

# import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

# Run classifier, using a model that is too regularized (C too low) to see
# the impact on the results
classifier = svm.SVC(kernel='linear', C=0.01)
y_pred = classifier.fit(X_train, y_train).predict(X_test)

def plot_confusion_matrix(cm, title='Confusion matrix', cmap=plt.cm.Blues):
 plt.imshow(cm, interpolation='nearest', cmap=cmap)
 plt.title(title)
 plt.colorbar()
 tick_marks = np.arange(len(iris.target_names))
 plt.xticks(tick_marks, iris.target_names, rotation=45)
 plt.yticks(tick_marks, iris.target_names)
 plt.tight_layout()
 plt.ylabel('True label')
 plt.xlabel('Predicted label')

# Compute confusion matrix
cm = confusion_matrix(y_test, y_pred)
np.set_printoptions(precision=2)
print('Confusion matrix, without normalization')
print(cm)
plt.figure()
plot_confusion_matrix(cm)

# Normalize the confusion matrix by row (i.e by the number of samples
# in each class)
cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print('Normalized confusion matrix')
print(cm_normalized)
plt.figure()
plot_confusion_matrix(cm_normalized, title='Normalized confusion matrix')

plt.show()

以上這篇keras訓練曲線,混淆矩陣,CNN層輸出可視化實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • python3中超級好用的日志模塊-loguru模塊使用詳解

    python3中超級好用的日志模塊-loguru模塊使用詳解

    loguru默認的輸出格式是上面的內容,有時間、級別、模塊名、行號以及日志信息,不需要手動創(chuàng)建?logger,直接使用即可,另外其輸出還是彩色的,看起來會更加友好,這篇文章主要介紹了python3中超級好用的日志模塊-loguru模塊使用詳解,需要的朋友可以參考下
    2022-11-11
  • 詳解Python的文件處理

    詳解Python的文件處理

    這篇文章主要為大家介紹了Python的文件處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 通過python連接Linux命令行代碼實例

    通過python連接Linux命令行代碼實例

    這篇文章主要介紹了通過python連接Linux命令行代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Python smtp郵件發(fā)送模塊用法教程

    Python smtp郵件發(fā)送模塊用法教程

    這篇文章主要介紹了Python smtp郵件發(fā)送模塊用法教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Python使用百度翻譯開發(fā)平臺實現(xiàn)英文翻譯為中文功能示例

    Python使用百度翻譯開發(fā)平臺實現(xiàn)英文翻譯為中文功能示例

    這篇文章主要介紹了Python使用百度翻譯開發(fā)平臺實現(xiàn)英文翻譯為中文功能,結合實例形式分析了Python使用request請求與百度翻譯API接口交互實現(xiàn)翻譯功能相關操作技巧,需要的朋友可以參考下
    2019-08-08
  • pytorch中[..., 0]的用法說明

    pytorch中[..., 0]的用法說明

    這篇文章主要介紹了pytorch中[..., 0]的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python3實現(xiàn)讀取chrome瀏覽器cookie

    python3實現(xiàn)讀取chrome瀏覽器cookie

    這里給大家分享的是python3讀取chrome瀏覽器的cookie(CryptUnprotectData解密)的代碼,主要思路是讀取到的cookies被封裝成字典,可以直接給requests使用。
    2016-06-06
  • python報錯: ''list'' object has no attribute ''shape''的解決

    python報錯: ''list'' object has no attribute ''shape''的解決

    這篇文章主要介紹了python報錯: 'list' object has no attribute 'shape'的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python文件操作利器的十大庫使用實例

    Python文件操作利器的十大庫使用實例

    Python擁有多個庫用于文件操作,提供了各種功能來管理、讀取和寫入文件,這些庫覆蓋了從基本的文件系統(tǒng)交互到高級的文件壓縮和數(shù)據(jù)格式處理等多個方面,文件操作是編程中不可或缺的一部分,因此了解這些庫對于開發(fā)者來說是至關重要的
    2024-01-01
  • 在Python中使用dict和set方法的教程

    在Python中使用dict和set方法的教程

    這篇文章主要介紹了在Python中使用dict和set方法的教程,dict字典是Python中的重要基礎知識,set與其類似,需要的朋友可以參考下
    2015-04-04

最新評論

准格尔旗| 秦皇岛市| 湛江市| 桂林市| 兴宁市| 明光市| 建瓯市| 介休市| 布拖县| 杭锦旗| 尤溪县| 屯门区| 合川市| 庄河市| 南城县| 泽库县| 禄劝| 大理市| 广河县| 遂溪县| 进贤县| 冕宁县| 唐河县| 德格县| 濮阳县| 紫金县| 德阳市| 雅江县| 平阴县| 芷江| 武强县| 清水河县| 延川县| 阿瓦提县| 荆门市| 沧州市| 民乐县| 昂仁县| 桦川县| 和龙市| 秦皇岛市|