CS231n作业小结

2020.05.18

本地环境搭建:https://blog.csdn.net/u014485485/article/details/89607483

参考资料:https://blog.csdn.net/zhangxb35/category_6727687.html

2020.05.19


(base) C:\Users\Administrator>jupyter notebook --generate-config
Traceback (most recent call last):
  File "F:\python\Anaconda3\lib\site-packages\notebook\services\sessions\session
manager.py", line 9, in <module>
    import sqlite3
  File "F:\python\Anaconda3\lib\sqlite3\__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "F:\python\Anaconda3\lib\sqlite3\dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ImportError: DLL load failed: 找不到指定的程序。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\python\Anaconda3\Scripts\jupyter-notebook-script.py", line 6, in <mod
ule>
    from notebook.notebookapp import main
  File "F:\python\Anaconda3\lib\site-packages\notebook\notebookapp.py", line 83,
 in <module>
    from .services.sessions.sessionmanager import SessionManager
  File "F:\python\Anaconda3\lib\site-packages\notebook\services\sessions\session
manager.py", line 12, in <module>
    from pysqlite2 import dbapi2 as sqlite3
ModuleNotFoundError: No module named 'pysqlite2'

(base) C:\Users\Administrator>

解决办法:https://blog.csdn.net/tianjie5768/article/details/104154062

Windows7旗舰版安装 Anaconda3 详细图文教程:https://blog.csdn.net/qq_29774003/article/details/85289612

其他的Python版本的Anaconda的安装地址:https://repo.anaconda.com/archive/

2020.06.04

对于32位操作系统来说,安装深度学习的环境会出现各种各样无法解决的问题。同时,pytorch不支持32位。

2020.06.05

相关软件国内镜像下载地址:https://mirror.tuna.tsinghua.edu.cn/help/anaconda/

(anaconda、pytorch、cuda、cudnn)

2020.06.09

修改默认打开目录与及默认打开浏览器:https://www.pianshen.com/article/8253572919/

2020.06.11

jupyter notebook使用技巧:https://blog.csdn.net/xiaodongxiexie/article/details/54633183

pytorch使用手册:https://pytorch.org/docs/stable/index.html

2020.06.12

pytorch实战|手写数字的识别:https://blog.csdn.net/qq_38276972/article/details/105287490

2020.06.17

构建图像分类器:http://pytorch123.com/SecondSection/training_a_classifier/

1.加载数据

import torch
import torchvision  #加载并归一化CIFAR10的训练集和测试集
import torchvision.transforms as transforms

transform = transforms.Compose([
     transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

#加载训练集trainset,下载到当前目录下的data文件
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

#加载测试集trainloader,下载到当前目录下的data文件
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)

#标签or分类
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

显示图片

import matplotlib.pyplot as plt
import numpy as np

def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()

dataiter = iter(trainloader)
images, labels = dataiter.next()

#显示测试集中的图片
imshow(torchvision.utils.make_grid(images))
#显示对应的标签
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

2.神经网络模型

nn.Conv2d(

   in_channels,  //输入图像中的通道数

   out_channels,   //由卷积产生的通道数

   kernel_size,  //卷积核的大小,整数输入或者(a,b,c,...)输入

   stride=1,  //卷积步长

   padding=0,  //加零填充,整数输入或者(a,b,c,...)输入

   dilation=1,  //核元素之间的间隔。

   groups=1,  //从输入通道到输出通道被阻塞的连接数。

   bias=True,  //True=>在输出中添加一个可学习的偏差

   padding_mode='zeros' //'zeros', 'reflect', 'replicate' or 'circular'

)

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5) #第一层卷积层
        self.pool = nn.MaxPool2d(2, 2) #池化
        self.conv2 = nn.Conv2d(6, 16, 5) #第二层卷积层
        self.fc1 = nn.Linear(16 * 5 * 5, 120) #
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        #池化操作
        x = self.pool(F.relu(self.conv1(x))) 
        x = self.pool(F.relu(self.conv2(x)))
        #将输入拉长为1*n
        x = x.view(-1, 16 * 5 * 5)
        #定义激活函数ReLu函数
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()

3.定义损失函数&优化器

optim.SGD(

   params,  //参数的可迭代性,以优化或dicts定义参数组

   lr=<required parameter>,  //学习率

   momentum=0,  //动量因子

   dampening=0,  //抑制的动量

   weight_decay=0,  //权值衰减

   nesterov=False //是否使用nesterov动量

)

import torch.optim as optim

#采用分类交叉熵Cross-Entropy作损失函数
criterion = nn.CrossEntropyLoss()
#采用动量SGD做优化器(加上加速度)
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

4.训练网络

#训练两次
for epoch in range(2):
    running_loss = 0.0  #损失量清零
    for i, data in enumerate(trainloader, 0):  #成对获取训练集的数据
        inputs, labels = data  #数据的图片输入&标签
        optimizer.zero_grad()  #优化器的梯度清零
        outputs = net(inputs)  #仍入卷积神经网络
        loss = criterion(outputs, labels)  #获取经过损失函数的结果
        loss.backward()  #反馈神经网络
        optimizer.step()  #优化器
        running_loss += loss.item()  #累加损失量
        #每训练2000个数据就输出损失量,并清零
        if i % 2000 == 1999:   
            print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')

5.评价网络表现

#计算训练好神经网络在测试集上的正确率
correct = 0
total = 0
with torch.no_grad():
    for data in testloader:  #遍历测试集
        images, labels = data
        outputs = net(images)  #用训练好神经网络计算结果
        _, predicted = torch.max(outputs.data, 1)  #获取最有可能的检测结果
        total += labels.size(0)
        correct += (predicted == labels).sum().item()  #累计检测结果的正确率
#输出训练好神经网络在测试集上的正确率
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))


#计算训练好神经网络在测试集每个类别上的正确率
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        for i in range(4):
            label = labels[i]
            class_correct[label] += c[i].item()
            class_total[label] += 1
#输出训练好神经网络在测试集每个类别上的正确率
for i in range(10):
    print('Accuracy of %5s : %2d %%' % (classes[i], 100 * class_correct[i] / class_total[i]))

2020.06.20

jupyter notebook自动补全代码:https://zhuanlan.zhihu.com/p/81160925

2020.06.30

numpy中dot与multiply的区别:https://www.cnblogs.com/AlvinSui/p/8733470.html

如果 a和 b都是 1-D arrays,它的作用是计算内积。(不进行复共轭)

numpy中a*b:https://blog.csdn.net/like4501/article/details/79753346

星乘表示矩阵内各对应位置相乘,矩阵a*b下标(0,0)=矩阵a下标(0,0) x 矩阵b下标(0,0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值