# 本书链接https://tangshusen.me/Dive-into-DL-PyTorch/#/chapter03_DL-basics/3.6_softmax-regression-scratch
# 3.7节
# 注释:黄文俊
# E-mail:[email protected]
import torch
from torch import nn
from torch.nn import init
import numpy as np
import sys
sys.path.append("..")
import d2lzh_pytorch as d2l
from collections import OrderedDict

batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

num_inputs = 784
num_outputs = 10

# class LinearNet(nn.Module):
#     def __init__(self, num_inputs, num_outputs):
#         super(LinearNet, self).__init__()
#         self.linear = nn.Linear(num_inputs, num_outputs)
#     def forward(self, x): # x shape: (batch, 1, 28, 28)
#         y = self.linear(x.view(x.shape[0], -1))
#         return y
# net = LinearNet(num_inputs, num_outputs)
# print(net) # 使用print可以打印出网络的结构


# 本函数已保存在d2lzh_pytorch包中方便以后使用
Exemplo n.º 2
0
    nn.Sigmoid(),
    nn.Linear(84, 10))

print('构造一个高和宽均为28的单通道数据样本,并逐层进行前向计算来查看每个层的输出形状')
X = torch.randn(size=(1, 1, 28, 28), dtype=torch.float32)
for layer in net:
    X = layer(X)
    print(layer.__class__.__name__, 'output shape: \t', X.shape)

print('-----------------------------------------------')
print('训练')
print('-----------------------------------------------')
print('获取数据,我们仍然使用Fashion-MNIST作为训练数据集')
# 数据
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size,
                                                    root='../../dataset')
print(len(train_iter))

print('查看gpu设备是否可用')


# This function has been saved in the d2l package for future use
#use GPU
def try_gpu():
    """If GPU is available, return torch.device as cuda:0; else return torch.device as cpu."""
    if torch.cuda.is_available():
        device = torch.device('cuda:0')
    else:
        device = torch.device('cpu')
    return device
#%% [markdown]
import torch
from torch import nn
from torch.nn import init
import numpy as np
import sys
sys.path.append("..")
import d2lzh_pytorch as d2l

#%% [markdown]
# 1.读取数据
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size,
                                                    resize=None,
                                                    root="Datasets")
#%% [markdown]
# 2.定义和初始化模型
num_inputs = 784
num_outputs = 10


class LinearNet(nn.Module):
    def __init__(self, num_inputs, num_outputs):
        super(LinearNet, self).__init__()
        self.linear = nn.Linear(num_inputs, num_outputs)

    def forward(self, x):
        y = self.linear(x.view(x.shape[0], -1))
        return y

@description: Softmax与分类模型
"""
import torch
from torch import nn
from torch.nn import init  # 初始化参数
import numpy as np
import sys

sys.path.append('.')
import d2lzh_pytorch

print(torch.__version__)

# 读取数据
batch_size = 256
train_iter, test_iter = d2lzh_pytorch.load_data_fashion_mnist(batch_size)

# 定义和初始化模型
num_inputs = 784
num_outputs = 10

# class LinearNet(nn.Module):
#     def __init__(self, num_inputs, num_outputs):
#         super(LinearNet, self).__init__()
#         self.linear = nn.Linear(num_inputs, num_outputs)
#     def forward(self, x): # x shape: (batch, 1, 28, 28)
#         y = self.linear(x.view(x.shape[0], -1))
#         return y

# net = LinearNet(num_inputs, num_outputs)
Exemplo n.º 5
0
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)

num_channels, growth_rate = 64, 32
num_convs_in_dense_blocks = [4, 4, 4, 4]

for i, num_convs in enumerate(num_convs_in_dense_blocks):
    DB = DenseBlock(num_convs, num_channels, growth_rate)
    net.add_module("DenseBlock_%d" % i, DB)
    num_channels = DB.out_channels
    if i != len(num_convs_in_dense_blocks) - 1:
        net.add_module("transition_block_%d" % i, transition_block(num_channels, num_channels // 2))
        num_channels = num_channels // 2

net.add_module("BN", nn.BatchNorm2d(num_channels))
net.add_module("relu", nn.ReLU())
net.add_module("global_avg_pool", d2l.GlobalAvgPool2d())
net.add_module("fc", nn.Sequential(d2l.FlattenLayer(), nn.Linear(num_channels, 10)))

X = torch.rand(1, 1, 96, 96)
for name, layer in net.named_children():
    X = layer(X)
    print(name, 'output shape:\t', X.shape)

batch_size = 256
trian_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=96)

lr, num_epochs = 0.001, 5
optimizer = torch.optim.Adam(net.parameter(), lr=lr)
d2l.train_ch5(net, trian_iter, test_iter, batch_size, optimizer, device, num_epochs)
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import time
import sys
import d2lzh_pytorch as d2l
from PIL import Image

mnist_train = torchvision.datasets.FashionMNIST(
    root="./DataSets/FashionMNIST",
    train=True,
    download=True,
    transform=transforms.ToTensor())

mnist_test = torchvision.datasets.FashionMNIST(root="./DataSets/FashionMNIST",
                                               train=False,
                                               download=True,
                                               transform=transforms.ToTensor())

X, y = [], []
for i in range(10):
    X.append(mnist_test[i][0])
    y.append(mnist_test[i][1])
d2l.show_fashion_mnist(X, d2l.get_fashion_mnist_labels(y))

train_iter, test_iter = d2l.load_data_fashion_mnist(256)
start = time.time()
for X, y in train_iter:
    continue
print('%.2f sec' % (time.time() - start))
Exemplo n.º 7
0
train_iter = torch.utils.data.DataLoader(mnist_train,
                                         batch_size=batch_size,
                                         shuffle=True,
                                         num_workers=num_workers)
test_iter = torch.utils.data.DataLoader(mnist_test,
                                        batch_size=batch_size,
                                        shuffle=False,
                                        num_workers=num_workers)
start = time.time()
for X, y in train_iter:
    continue
print('%.2f sec' % (time.time() - start))

#####################softmax 从零开始实现#################################<<<
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(
    batch_size, root='Data/softmax/FashionMNIST2065')
#模型参数初始化
num_inputs = 784
print(28 * 28)
num_outputs = 10
W = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_outputs)),
                 dtype=torch.float)
b = torch.zeros(num_outputs, dtype=torch.float)
W.requires_grad_(requires_grad=True)
b.requires_grad_(requires_grad=True)


#定义softmax
def softmax(X):
    X_exp = X.exp()
    partition = X_exp.sum(dim=1, keepdim=True)
Exemplo n.º 8
0
# 模块5
# 2个Inception块接全局平均池化层
b5 = nn.Sequential(
    Inception(832, 256, (160, 320), (32, 128), 128),  # 256+320+128+128=832
    Inception(832, 384, (192, 384), (48, 128), 128),  # 384+384+128+128=1024
    d2l.GlobalAvgPool2d())

# 总模型
# 接一个维度转换和全连接层
net = nn.Sequential(b1, b2, b3, b4, b5, d2l.FlattenLayer(),
                    nn.Linear(1024, 10))

# 测试
# GoogLeNet计算复杂,而且不如vgg那样便于修改通道数
# 将输入的高宽从224降到96
# x = torch.rand(1,1,96,96)
# for blk in net.children():
#     x = blk(x)
#     print(x.shape)

# 参数
lr, num_epochs, batch_size = 0.001, 5, 128
# 数据
train_iter, test_iter = d2l.load_data_fashion_mnist(
    batch_size, resize=96)  # 为减小数据规模,resize减小为96
# 优化器
optimizer = optim.Adam(net.parameters(), lr=lr)
# 训练
d2l.train_ch5(net, train_iter, test_iter, batch_size, optimizer, device,
              num_epochs)