<reponame>GenBill/notebooks
import random
import numpy as np

import torch
import torch.nn as nn
import torchvision

from IPython import display
from torch.utils import data
from torchvision import transforms
from matplotlib import pyplot as plt


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())
print(type(mnist_train))
print(len(mnist_train),len(mnist_test))

# 来访问一个样本:
feature,label = mnist_train[0]
print(feature.shape,label)
# 数值标签转为文本标签
def get_fashion_mnist_labels(labels):
    text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
                   'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
    return [text_labels[int(i)] for i in labels]
def show_fashion_mnist(images, labels):
    # 可以在一行内画出多个图
    # 这里的_表示我们忽略（不使用）的变量
    _, figs = plt.subplots(1, len(images), figsize=(14, 14))
    for f, img, lbl in zip(figs, images, labels):
        # f.imshow(img.view((56, 14)).numpy())
        f.imshow(img.view((28, 28)).numpy())
        f.set_title(lbl)
        f.axes.get_xaxis().set_visible(False)
        f.axes.get_yaxis().set_visible(False)
    plt.show()

x, y = [], []
for i in range(10):
    x.append(mnist_train[i][0])
    y.append(mnist_train[i][1])
show_fashion_mnist(x, get_fashion_mnist_labels(y))
print(type(x))

# 定义模型
num_inputs=784  #28*28 图片像素尺寸
num_outputs=10
num_hiddens=256

class Mymodel(nn.Module):
    def __init__(self, num_inputs, num_hiddens, num_outputs):
        super().__init__()
        self.linear = nn.Sequential(
            nn.Linear(num_inputs, num_hiddens),
            nn.ReLU(),
            nn.Linear(num_hiddens,num_outputs)
        )
    def forward(self, x):
        # x.shape: (batch, 1, 28, 28)
        # view(-1) 自动计算剩余行列数
        y = self.linear(x.view(x.shape[0],-1))
        # y = self.linear(x.view(x.shape[0],784))
        return y
        
net = Mymodel(num_inputs, num_hiddens, num_outputs)
#net = torch.nn.DataParallel(net)
print(net)

# 初始化参数
for params in net.parameters():
    nn.init.normal_(params, mean=0, std=0.01)

batch_size = 256
train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True)
test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False)
print(len(train_iter))


## 在这停顿！




def evaluate_accuracy(data_iter, net):
    acc_sum, n = 0.0, 0
    for X, y in data_iter:
        acc_sum += (net(X).argmax(dim=1) == y).float().sum().item()
        n += y.shape[0]
    return acc_sum / n
# 因为重复性较强,这里定义成函数
def train_model(net, train_iter, test_iter, loss,num_epochs,
              params=None, lr=None, optimizer=None):
    print('num_epochs:',num_epochs)
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
        #train_l_sum loss的求和
        #train_acc_sum acc的求和
        for x, y in train_iter:
            #y_hat  y预测
            y_hat = net(x)
            #计算loss
            l = loss(y_hat, y).sum()

            # 梯度清零
            if optimizer is not None:
                optimizer.zero_grad()
            elif params is not None and params[0].grad is not None:
                for param in params:
                    param.grad.data.zero_()

            l.backward()
            if optimizer is None:
                d2l.sgd(params, lr, batch_size)
            else:
                optimizer.step()  # “多层感知器的简洁实现”一节将用到


            train_l_sum += l.item()
            #argmax 取最大值,相当于取预测里面的最大值
            train_acc_sum += (y_hat.argmax(dim=1) == y).float().sum().item()
            n += y.shape[0]
        test_acc = evaluate_accuracy(test_iter, net)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f'
              % (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc)) 

batch_size = 256
loss = torch.nn.CrossEntropyLoss()#.cuda()

# 需要看
optimizer = torch.optim.SGD(net.parameters(), lr=0.5)
num_epochs = 5
train_model(net, train_iter, test_iter, loss, num_epochs, None, None, optimizer)

import random
import numpy as np

import torch
import torch.nn as nn
import torchvision

from IPython import display
from torch.utils import data
from torchvision import transforms
from matplotlib import pyplot as plt


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())
print(type(mnist_train))
print(len(mnist_train),len(mnist_test))

# 来访问一个样本:
feature,label = mnist_train[0]
print(feature.shape,label)
# 数值标签转为文本标签
def get_fashion_mnist_labels(labels):
    text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
                   'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
    return [text_labels[int(i)] for i in labels]
def show_fashion_mnist(images, labels):
    # 可以在一行内画出多个图
    # 这里的_表示我们忽略（不使用）的变量
    _, figs = plt.subplots(1, len(images), figsize=(14, 14))
    for f, img, lbl in zip(figs, images, labels):
        # f.imshow(img.view((56, 14)).numpy())
        f.imshow(img.view((28, 28)).numpy())
        f.set_title(lbl)
        f.axes.get_xaxis().set_visible(False)
        f.axes.get_yaxis().set_visible(False)
    plt.show()

x, y = [], []
for i in range(10):
    x.append(mnist_train[i][0])
    y.append(mnist_train[i][1])
show_fashion_mnist(x, get_fashion_mnist_labels(y))
print(type(x))

# 定义模型
num_inputs=784  #28*28 图片像素尺寸
num_outputs=10
num_hiddens=256

class Mymodel(nn.Module):
    def __init__(self, num_inputs, num_hiddens, num_outputs):
        super().__init__()
        self.linear = nn.Sequential(
            nn.Linear(num_inputs, num_hiddens),
            nn.ReLU(),
            nn.Linear(num_hiddens,num_outputs)
        )
    def forward(self, x):
        # x.shape: (batch, 1, 28, 28)
        # view(-1) 自动计算剩余行列数
        y = self.linear(x.view(x.shape[0],-1))
        # y = self.linear(x.view(x.shape[0],784))
        return y
        
net = Mymodel(num_inputs, num_hiddens, num_outputs)
#net = torch.nn.DataParallel(net)
print(net)

# 初始化参数
for params in net.parameters():
    nn.init.normal_(params, mean=0, std=0.01)

batch_size = 256
train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True)
test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False)
print(len(train_iter))


## 在这停顿！




def evaluate_accuracy(data_iter, net):
    acc_sum, n = 0.0, 0
    for X, y in data_iter:
        acc_sum += (net(X).argmax(dim=1) == y).float().sum().item()
        n += y.shape[0]
    return acc_sum / n
# 因为重复性较强,这里定义成函数
def train_model(net, train_iter, test_iter, loss,num_epochs,
              params=None, lr=None, optimizer=None):
    print('num_epochs:',num_epochs)
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
        #train_l_sum loss的求和
        #train_acc_sum acc的求和
        for x, y in train_iter:
            #y_hat  y预测
            y_hat = net(x)
            #计算loss
            l = loss(y_hat, y).sum()

            # 梯度清零
            if optimizer is not None:
                optimizer.zero_grad()
            elif params is not None and params[0].grad is not None:
                for param in params:
                    param.grad.data.zero_()

            l.backward()
            if optimizer is None:
                d2l.sgd(params, lr, batch_size)
            else:
                optimizer.step()  # “多层感知器的简洁实现”一节将用到


            train_l_sum += l.item()
            #argmax 取最大值,相当于取预测里面的最大值
            train_acc_sum += (y_hat.argmax(dim=1) == y).float().sum().item()
            n += y.shape[0]
        test_acc = evaluate_accuracy(test_iter, net)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f'
              % (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc)) 

batch_size = 256
loss = torch.nn.CrossEntropyLoss()#.cuda()

# 需要看
optimizer = torch.optim.SGD(net.parameters(), lr=0.5)
num_epochs = 5
train_model(net, train_iter, test_iter, loss, num_epochs, None, None, optimizer)
# 来访问一个样本: # 数值标签转为文本标签 # 可以在一行内画出多个图 # 这里的_表示我们忽略（不使用）的变量 # f.imshow(img.view((56, 14)).numpy()) # 定义模型 #28*28 图片像素尺寸 # x.shape: (batch, 1, 28, 28) # view(-1) 自动计算剩余行列数 # y = self.linear(x.view(x.shape[0],784)) #net = torch.nn.DataParallel(net) # 初始化参数 ## 在这停顿！ # 因为重复性较强,这里定义成函数 #train_l_sum loss的求和 #train_acc_sum acc的求和 #y_hat  y预测 #计算loss # 梯度清零 # “多层感知器的简洁实现”一节将用到 #argmax 取最大值,相当于取预测里面的最大值 #.cuda() # 需要看