python中使用matlabplit繪製動態直方圖

  1. 通過這樣的動態作圖,可以用來分析深度學習模型的性能。
    示例代碼1:
    動態直方圖
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
y1 = []
for i in range(50):
    y1.append(i)  # 每迭代一次,將i放入y1中畫出來
    ax.cla()   # 清除鍵,清除所有的數值
    #ax.bar(y1, label='test', height=y1, width=0.3)
    ax.bar(y1, label='test', height=y1, width=0.3)
    ax.legend()
    plt.pause(0.1)


示例代碼2:兩條動態曲線疊加

# 測試程序
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 2 * np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

plt.figure(1)
plt.plot(x, y1)
plt.pause(2)  #在figure(1)上繪製sin曲線,2s後自動關閉窗口

plt.figure(1)
plt.plot(x, y2)
plt.pause(2)  #在figure(1)上繪製cos曲線,2s後自動關閉窗口
#plt.show()
plt.close()

示例代碼3:
統計直方圖比較

import numpy as np

import matplotlib.pyplot as plt

men_means = (20, 35, 30, 35, 27)

women_means = (25, 32, 34, 20, 25)

ind = np.arange(len(men_means))  # the x locations for the groups

width = 0.35  # the width of the bars

fig, ax = plt.subplots()

rects1 = ax.bar(ind - width / 2, men_means, width, color='SkyBlue', label='Men')

rects2 = ax.bar(ind + width / 2, women_means, width, color='IndianRed', label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.

ax.set_ylabel('Scores')

ax.set_title('Scores by group and gender')

plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))

ax.legend()

plt.show()

示例代碼4:
綜合示例代碼1和示例代碼3
該動態直方圖比較,可以用來觀察深度學習模型中分類的準確率的比較

#該行代碼必須放在循環外,只允許建立一次,如果放到循環裏,會被反覆建立多次
import numpy as np
import random
import matplotlib.pyplot as plt

EPOCH = 100
#標籤索引
ind = np.arange(10)
#直方圖寬度
width = 0.5
#產生隨機整數值數組
def random_int_list(start, stop, length):
#start:下限     stop:上限    length:數組長度
  start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
  length = int(abs(length)) if length else 0
  random_list = []
  for i in range(length):
    random_list.append(random.randint(start, stop))
  return random_list

#模擬樣本標籤值數組,必須放到循環外面,只初始化一次
label_list = random_int_list(10, 100, 10)
#只產生一個figure,放到循環外面
fig, ax = plt.subplots()
for epoch in range(EPOCH):
        ax.cla()#清除所有數值
        #模擬模型每次的預測輸出值
        predicted_list = random_int_list(10, 100, 10)
        rects1 = ax.bar(ind - width / 2, label_list, width, color='IndianRed', label='label_list')
        rects2 = ax.bar(ind + width / 2, predicted_list, width, color='SkyBlue', label='predicted_list')
        ax.legend()
        plt.pause(0.1)

示例代碼5:全連接模型訓練代碼

import torch
import numpy as np
import torch.nn as nn
from torch.autograd import Variable
from tqdm import *
import matplotlib.pyplot as plt

#定義一些參數
EPOCH = 1000#訓練輪次
LR = 0.01  #learing rate
class_number = 10 #類別數
stop_threshold = 0.99  #訓練截止的條件之一:準確率閾值,當準確率超過該閾值,則停止訓練
sample_sum = 1000#樣本數目
model_input = 1000 #模型輸入的特徵個數
#標籤編碼,將樣本中每個隨機數據的1000個隨機特徵值求均值,將均值分爲10個區間,對應標籤值爲0到9
def code_label(data):
    data_label = np.sum(data, 1) #將每一行的元素相加,壓縮行
    data_label = np.around((data_label /model_input), decimals= 2)#decimals = 2: 保留小數點幾位
    min_label = np.around(min(data_label), decimals=2)
    max_label = np.around(max(data_label), decimals=2)
    step = np.around((max_label - min_label) / class_number, decimals=2)
    section_lower = min_label #樣本輸出最小值
    data_label_code = np.zeros((len(data_label), class_number))

    for i in range(class_number):
        section_upper = section_lower + step
        for index in range(len(data_label)):
            if (data_label[index] >= section_lower)&(data_label[index] < section_upper):
                data_label_code[index][i] = 1

        section_lower = section_upper

    return data_label_code

#定義全連接層
class Linear(nn.Module):
    def __init__(self, in_features,out_features):
        super(Linear, self).__init__()
        w = torch.empty(in_features, out_features)
        #隨機初始化
        #self.w = nn.Parameter(torch.randn(in_features, out_features))
        # Xavier 正態分佈初始化
        #nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))
        nn.init.xavier_normal_(w, gain=1)
        #self.w = nn.Parameter(w)
        #凱明初始化
        #nn.init.kaiming_normal_(w, a=0, mode='fan_out', nonlinearity='relu')
        #self.w = nn.Parameter(w)
        #self.w = nn.Parameter(torch.from_numpy(np.random.randn(in_features, out_features) / np.sqrt(out_features*0.5)).float())
        #零初始化
        self.w = nn.Parameter(torch.ones(in_features, out_features))
        self.b = nn.Parameter(torch.zeros(out_features))
    def forward(self,x):
        x = x.mm(self.w)
        return x + self.b.expand_as(x)

#定義網絡
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.layer1 = Linear(1000, 800)
        self.layer2 = Linear(800, 500)
        self.layer3 = Linear(500, 300)
        self.layer4 = Linear(300, 200)
        self.layer5 = Linear(200, 100)
        self.layer6 = Linear(100, 80)
        self.layer7 = Linear(80, 10)


    def forward(self,x):
        x = self.layer1(x)
        x = torch.relu(x)
        x = self.layer2(x)
        x = torch.relu(x)
        x = self.layer3(x)
        x = torch.relu(x)
        x = self.layer4(x)
        x = torch.relu(x)
        x = self.layer5(x)
        x = torch.relu(x)
        x = self.layer6(x)
        x = torch.relu(x)
        x = self.layer7(x)
        x = torch.relu(x)
        #x = F.softmax(x, dim=1)
        return(x)

#模型初始化
net = Net()
#定義loss函數
#loss_func = torch.nn.MSELoss()
#查看模型的初始化參數
for parameters in net.parameters():
    print(parameters)
for name,parameters in net.named_parameters():
    print(name,':',parameters.size())

loss_func = torch.nn.CrossEntropyLoss()
losses_his = []   # 記錄不同epoch下的損失函數
#定義優化方法
opt = torch.optim.SGD(net.parameters(), lr=LR)
loss_hist = []
corr = []

#生成數據
init_seed = 1
np.random.seed(init_seed)
data = np.random.randn(sample_sum, 1000)
# 對生成數據標籤編碼
data_label_code = code_label(data)
b_x = torch.from_numpy(data)  # 務必要用 Variable 包一下

b_x = torch.tensor(b_x, dtype=torch.float32)
b_x = Variable(b_x)

b_y = torch.from_numpy(data_label_code)
_, labels = torch.max(b_y, dim=1)
b_y = torch.tensor(b_y, dtype=torch.float32)
b_y = Variable(b_y)

fig, ax = plt.subplots()
for epoch in tqdm(range(EPOCH)):
    model_corr = 0
    output = net(b_x)
    output = output.view(len(b_y), class_number)
    _, predicted = torch.max(output, dim=1)
    loss = loss_func(output, labels)  # compute loss for every net
    losses_his.append(loss.data)
    opt.zero_grad()  # clear gradients for next train
    loss.backward()  # backpropagation, compute gradients
    opt.step()
    #計算準確率
    model_corr += (predicted == labels).sum()
    model_corr = model_corr.numpy()
    corr.append(model_corr/len(b_y))
    if epoch % 100 == 99:
        print('%5d epoch loss: %.3f'\
              %(epoch, loss.data))
        #print(model_corr)
        print('%5d epoch correc_rate: %.3f' \
              % (epoch, model_corr/len(b_y)))

        arr_labels = labels.numpy()
        arr_labels = arr_labels.tolist()
        label_dict = {}
        for i in set(arr_labels):
            label_dict[i] = arr_labels.count(i)
        label_list = list(label_dict.values())


        arr_predicted = predicted.numpy()
        arr_predicted = arr_predicted.tolist()
        predicted_dict = {}
        for i in set(arr_labels):
            predicted_dict[i] = arr_predicted.count(i)
        predicted_list = list(predicted_dict.values())

        ind = np.arange(10)
        width = 0.5

        ax.cla()
        rects1 = ax.bar(ind - width / 2, label_list, width, color='IndianRed', label='label_list')
        rects2 = ax.bar(ind + width / 2, predicted_list, width, color='SkyBlue', label='predicted_list')
        ax.legend()
        plt.pause(0.1)
        model_corr_rate = model_corr/sample_sum
        if model_corr_rate > stop_threshold:
            break



plt.show()
#print(len(losses_his))
plt.plot(losses_his, label='loss')
plt.legend(loc = 'best')
plt.xlabel('steps')
plt.ylabel('loss')
#縱軸座標範圍
plt.ylim((0, max(losses_his)))
plt.show()

plt.plot(corr, label='corr')
plt.legend(loc = 'best')
plt.xlabel('steps')
plt.ylabel('corr')
#縱軸座標範圍
plt.ylim((0, 1))
plt.show()

參考鏈接:
https://blog.csdn.net/xyisv/article/details/80651334
https://blog.csdn.net/beyond9305/article/details/82958683

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章