首页 > 其他分享 >第三章:3.8.1 绘制各层参数分布图 hist

第三章:3.8.1 绘制各层参数分布图 hist

时间:2024-12-16 12:52:59浏览次数:4  
标签:loss plt val batch 分布图 hist train model 3.8

Chapter03/Varying_learning_rate_on_scaled_data.ipynb

绘制 各层参数分布图

# https://github.com/PacktPublishing/Modern-Computer-Vision-with-PyTorch
# https://github.com/PacktPublishing/Modern-Computer-Vision-with-PyTorch

###################  Chapter Three #######################################

# 第三章  读取数据集并显示
from torch.utils.data import Dataset, DataLoader
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
########################################################################
from torchvision import datasets
import torch
data_folder = '~/data/FMNIST' # This can be any directory you want to
# download FMNIST to
fmnist = datasets.FashionMNIST(data_folder, download=True, train=True)
tr_images = fmnist.data
tr_targets = fmnist.targets

val_fmnist = datasets.FashionMNIST(data_folder, download=True, train=False)
val_images = val_fmnist.data
val_targets = val_fmnist.targets


########################################################################
import matplotlib.pyplot as plt
#matplotlib inline
import numpy as np
from torch.utils.data import Dataset, DataLoader
import torch
import torch.nn as nn
device = 'cuda' if torch.cuda.is_available() else 'cpu'

########################################################################
class FMNISTDataset(Dataset):
    def __init__(self, x, y):
        x = x.float()/255 #归一化
        x = x.view(-1,28*28)
        self.x, self.y = x, y
    def __getitem__(self, ix):
        x, y = self.x[ix], self.y[ix]
        return x.to(device), y.to(device)
    def __len__(self):
        return len(self.x)

from torch.optim import SGD, Adam
def get_model():
    model = nn.Sequential(
        nn.Linear(28 * 28, 1000),
        nn.ReLU(),
        nn.Linear(1000, 10)
    ).to(device)

    loss_fn = nn.CrossEntropyLoss()
    optimizer = Adam(model.parameters(), lr=1e-2)
    return model, loss_fn, optimizer

def train_batch(x, y, model, optimizer, loss_fn):
    model.train()
    prediction = model(x)
    batch_loss = loss_fn(prediction, y)
    batch_loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    return batch_loss.item()

def accuracy(x, y, model):
    model.eval()
    # this is the same as @torch.no_grad
    # at the top of function, only difference
    # being, grad is not computed in the with scope
    with torch.no_grad():
        prediction = model(x)
    max_values, argmaxes = prediction.max(-1)
    is_correct = argmaxes == y
    return is_correct.cpu().numpy().tolist()

########################################################################
def get_data():
    train = FMNISTDataset(tr_images, tr_targets)
    trn_dl = DataLoader(train, batch_size=32, shuffle=True)#批大小
    val = FMNISTDataset(val_images, val_targets)
    val_dl = DataLoader(val, batch_size=len(val_images), shuffle=False)
    return trn_dl, val_dl
########################################################################
#@torch.no_grad()
def val_loss(x, y, model):
    with torch.no_grad():
        prediction = model(x)
    val_loss = loss_fn(prediction, y)
    return val_loss.item()

########################################################################
trn_dl, val_dl = get_data()
model, loss_fn, optimizer = get_model()

########################################################################
train_losses, train_accuracies = [], []
val_losses, val_accuracies = [], []
for epoch in range(10): #轮数 10次
    print(epoch)
    train_epoch_losses, train_epoch_accuracies = [], []
    for ix, batch in enumerate(iter(trn_dl)):
        x, y = batch
        batch_loss = train_batch(x, y, model, optimizer, loss_fn)
        train_epoch_losses.append(batch_loss)
    train_epoch_loss = np.array(train_epoch_losses).mean()

    for ix, batch in enumerate(iter(trn_dl)):
        x, y = batch
        is_correct = accuracy(x, y, model)
        train_epoch_accuracies.extend(is_correct)
    train_epoch_accuracy = np.mean(train_epoch_accuracies)
    for ix, batch in enumerate(iter(val_dl)):
        x, y = batch
        val_is_correct = accuracy(x, y, model)
        validation_loss = val_loss(x, y, model)
    val_epoch_accuracy = np.mean(val_is_correct)
    train_losses.append(train_epoch_loss)
    train_accuracies.append(train_epoch_accuracy)
    val_losses.append(validation_loss)
    val_accuracies.append(val_epoch_accuracy)

########################################################################
epochs = np.arange(10)+1
import matplotlib.ticker as mtick
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
#%matplotlib inline
# plt.figure(figsize=(20,5))
# plt.subplot(211)
# plt.plot(epochs, train_losses, 'bo', label='Training loss')
# plt.plot(epochs, val_losses, 'r', label='Validation loss')
# plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(1))
# plt.title('Training and validation loss when batch size is 32')
# plt.xlabel('Epochs')
# plt.ylabel('Loss')
# plt.legend()
# plt.grid('off')
# #plt.show()
# plt.subplot(212)
# plt.plot(epochs, train_accuracies, 'bo', label='Training accuracy')
# plt.plot(epochs, val_accuracies, 'r', label='Validation accuracy')
# plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(1))
# plt.title('Training and validation accuracy when batch size is 32')
# plt.xlabel('Epochs')
# plt.ylabel('Accuracy')
# plt.gca().set_yticklabels(['{:.0f}%'.format(x*100) for x in plt.gca().get_yticks()])
# plt.legend()
# plt.grid('off')
# plt.show()
plt.figure(figsize=(20,5))
for ix, par in enumerate(model.parameters()):
    print(f'绘图:{ix}')
    print(f'数据:{par.shape}')
    if(ix==0):
        plt.subplot(411)
        plt.hist(par.cpu().detach().numpy().flatten())
        plt.title('Distribution of weights conencting input to hidden layer')
        #plt.show()
    elif(ix ==1):
        plt.subplot(412)
        plt.hist(par.cpu().detach().numpy().flatten())
        plt.title('Distribution of biases of hidden layer')
        #plt.show()
    elif(ix==2):
        plt.subplot(413)
        plt.hist(par.cpu().detach().numpy().flatten())
        plt.title('Distribution of weights conencting hidden to output layer')
        #plt.show()
    elif(ix ==3):
        plt.subplot(414)
        plt.hist(par.cpu().detach().numpy().flatten())
        plt.title('Distribution of biases of output layer')
        plt.show()

 

标签:loss,plt,val,batch,分布图,hist,train,model,3.8
From: https://www.cnblogs.com/excellentHellen/p/18609839

相关文章

  • 强化学习(ChatGPT回答):Reward Landscape —— 奖励分布图
    奖励景观(机器学习、强化学习)在强化学习中,RewardLandscape指的是奖励函数随着状态和行为的变化所形成的空间结构。它可以帮助理解智能体如何通过探索奖励的分布来优化策略。翻译:奖励景观;奖励分布图。例句:Theagentlearnstonavigatetherewardlandscapeeffectivel......
  • 系统入侵后配置:禁止linux history和禁止记录登录成功IP,禁用最后一次登录成功提醒
    #禁用用户的显示lastlogin提示:  Lastlogin:MonDec903:05:132024from117.6.8.113touch~/.hushlogin   如果你只想禁用某个特定用户的history,可以编辑该用户的~/.bashrc文件并添加以下内容:#禁用特定用户的Bash命令历史 exportHISTSIZE=0 export......
  • 文献阅读笔记|将H&E图像转换为虚拟免疫组化图像的病理学工具|Accelerating histopatho
    论文链接:https://doi.org/10.1038/s42256-024-00889-5论文信息:发表于NatureMachineIntelligence。2023年12月4日投稿,2024年7月29日接收,2024年9月9日online目录AbstractIntroduction1、从HE染色病理图像合成多重免疫组化(IHC)染色图像的意义2、虚拟染色【1】含义介绍【2】配对模......
  • Python实现几何分布图(Geometric Distribution)
    几何分布(GeometricDistribution)是一个离散概率分布,用于表示在一次独立重复试验中,直到首次成功所需的试验次数的分布。几何分布的特点1.每次试验的结果只有两种:成功或失败(比如掷硬币)。2.每次试验是独立的,且成功的概率p是恒定的。3.随机变量X表示第一次成功发生所需......
  • Linux history 命令详解
    简介history 命令显示当前 shell 会话中以前执行过的命令列表。这对于无需重新输入命令即可重新调用或重新执行命令特别有用。示例用法显示命令历史列表history#示例输出如下:1ls-l2cd/var/log3catsyslog执行历史记录中的命令!<number>!2#number表......
  • Failed to execute goal org.apache.maven.pluginsmaven-compiler-plugin3.8.1compile
    1.报错信息Failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile(default-compile)onprojectrepair-wheelset-service:FatalerrorcompilingFailedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile(de......
  • Since Maven 3.8.1 http repositories are blocked.
    SinceMaven3.8.1httprepositoriesareblocked.SinceMaven3.8.1httprepositoriesareblocked.Possiblesolutions:-CheckthatMavensettings.xmldoesnotcontainhttprepositories-CheckthatMavenpomfilesdonotcontainhttprepositoryhttp://mv......
  • selenium cookie 登录 转载 from:https://www.cnblogs.com/CYHISTW/p/11685846.html
    seleniumcookie登录 前言爬虫方向的小伙伴们都知道网页爬虫经常遇到的问题就是登录账户,有些简单的网站我们可以简单的sendkey来输入账户密码就可以登录,但是有很多网站需要验证码之类的就不太好用了,这时候就体现到了cookie登录的优点了,前段时间网上搜了搜,发现没有什么完整......
  • 【YashanDB知识库】由于hist_head$中analyze time小于tab$中analyze time导致的sql语
    本文内容来自YashanDB官网,具体内容请见https://www.yashandb.com/newsinfo/7459465.html?templateId=1718516问题现象某局点yashandbcpu使用率100%,经线上分析是由于几个sql执行慢,其中一个sql为简单的单行等值绑定变量过滤+排序。经分析执行计划,相对以前有所变化,走了另外一个索引(......
  • Docker镜像、Spark支持多表...Apache SeaTunnel 2.3.8版本将带来的惊喜
    ApacheSeaTunnel2.3.8版本即将于大家见面,近日,ApacheSeaTunnelPMCMember范佳在社区的交流会上为大家提前透露了关于这个新版本即将进行的功能与特性更新概况,详细内容如下:SeaTunnel简介SeaTunnel是一个高性能的开源分布式数据集成系统,支持各种数据源的实时流式和离线批处理......