IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 基于Pytorch的Fashion mnist实战 -> 正文阅读

[人工智能]基于Pytorch的Fashion mnist实战

import torch
from torchvision import datasets, transforms
import time
import numpy as np
import matplotlib.pyplot as plt
    
from PIL import Image
# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5,), (0.5,))])
# Download and load the training data
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

# Download and load the test data
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)

# TODO: Define your network architecture here
from torch import nn, optim
import torch.nn.functional as F
from sklearn.metrics import accuracy_score

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1) 
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear(4 * 4 * 50, 500)
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 4 * 4 * 50)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)
    
    def predict(self,x):
        pred = self.forward(x).argmax(dim=1, keepdim=True).cpu().detach().numpy().reshape(1,-1)[0]
        
        return pred


# TODO: Create the network, define the criterion and optimizer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# model = Classifier_fashion()
criterion = nn.NLLLoss()
# optimizer = optim.Adam(model.parameters(),lr=0.003)

model = Net().to(device)
optimizer = optim.SGD(model.parameters(),lr=0.003)
import numpy as np
import torch

class EarlyStopping:

    def __init__(self, patience=7, verbose=False, delta=0):

        self.patience = patience
        self.verbose = verbose
        self.counter = 0
        self.best_score = None
        self.early_stop = False
        self.val_loss_min = np.Inf
        self.delta = delta

    def __call__(self, val_loss, model):

        score = -val_loss
        
        if self.best_score is None:
            self.best_score = score
            self.save_checkpoint(val_loss, model)
        elif score < self.best_score + self.delta:
            self.counter += 1
            print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
            if self.counter >= self.patience:
                self.early_stop = True
        else:
            self.best_score = score
            self.save_checkpoint(val_loss, model)
            self.counter = 0

    def save_checkpoint(self, val_loss, model):
        '''Saves model when validation loss decrease.'''
        if self.verbose:
            print(f'Validation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}).  Saving model ...')
        torch.save(model.state_dict(), 'checkpoint.pt')
        self.val_loss_min = val_loss


# TODO: Train the network here
epochs = 200


losses = []
losses_test = []
for e in range(epochs):
    since = time.time()
    print("epoch:",e+1)
    running_loss = 0
    acc = 0
    for images, labels in trainloader:
        images = images.to(device)
        labels = labels.to(device)
        log_ps = model(images)
        loss = criterion(log_ps, labels)
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        acc = acc + accuracy_score(model(images).argmax(dim=1, keepdim=True).cpu().detach().numpy().reshape(1,-1)[0],labels.cpu().detach().numpy().reshape(1,-1)[0])
        running_loss += loss.item()
    
    print("Average trainning loss:{:.4f}".format(running_loss/len(trainloader)))
    losses.append(running_loss/len(trainloader))
    print("Average Accuracy:{:.4f}".format(acc/len(trainloader)))
    
    
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in testloader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            test_loss += F.nll_loss(output, target, reduction='sum').item() 
            pred = output.argmax(dim=1, keepdim=True)
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(testloader.dataset)
    losses_test.append(test_loss)
    print('Test set: Average loss: {:.4f}\nAccuracy: {}/{} ({:.0f}%)'.format(
        test_loss, correct, len(testloader.dataset),
        100. * correct / len(testloader.dataset)))
    print("total acc:{:.4f}".format((correct / len(testloader.dataset)+acc/len(trainloader))/2))
    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s\n'.format(
        time_elapsed // 60, time_elapsed % 60))
epoch: 1
Average trainning loss:1.8171
Average Accuracy:0.4848
Test set: Average loss: 1.0191
Accuracy: 6506/10000 (65%)
total acc:0.5677
Training complete in 0m 17s

epoch: 2
Average trainning loss:0.8103
Average Accuracy:0.7237
Test set: Average loss: 0.7470
Accuracy: 7318/10000 (73%)
total acc:0.7277
Training complete in 0m 17s
...

epoch: 198
Average trainning loss:0.0796
Average Accuracy:0.9936
Test set: Average loss: 0.3552
Accuracy: 9007/10000 (90%)
total acc:0.9471
Training complete in 0m 18s

epoch: 199
Average trainning loss:0.0789
Average Accuracy:0.9940
Test set: Average loss: 0.3161
Accuracy: 9079/10000 (91%)
total acc:0.9509
Training complete in 0m 18s

epoch: 200
Average trainning loss:0.0761
Average Accuracy:0.9946
Test set: Average loss: 0.3057
Accuracy: 9109/10000 (91%)
total acc:0.9527
Training complete in 0m 18s
fig = plt.figure(figsize=(14,8))
plt.plot(range(len(losses)),losses,label="train_loss")
plt.plot(range(len(losses)),losses_test,label="losses_test")
plt.legend()
<matplotlib.legend.Legend at 0x1913bfd7e80>

在这里插入图片描述



device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

criterion = nn.NLLLoss()


model = Net().to(device)
optimizer = optim.SGD(model.parameters(),lr=0.003)

epochs = 200
patience = 10
early_stopping = EarlyStopping(patience, verbose=True)
losses = []
losses_test = []
for e in range(epochs):
    since = time.time()
    print("epoch:",e+1)
    running_loss = 0
    acc = 0
    for images, labels in trainloader:
        images = images.to(device)
        labels = labels.to(device)
        log_ps = model(images)
        loss = criterion(log_ps, labels)
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        acc = acc + accuracy_score(model(images).argmax(dim=1, keepdim=True).cpu().detach().numpy().reshape(1,-1)[0],labels.cpu().detach().numpy().reshape(1,-1)[0])
        running_loss += loss.item()
    
    print("Average trainning loss:{:.4f}".format(running_loss/len(trainloader)))
    losses.append(running_loss/len(trainloader))
    print("Average Accuracy:{:.4f}".format(acc/len(trainloader)))
    
    
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in testloader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            test_loss += F.nll_loss(output, target, reduction='sum').item()
            pred = output.argmax(dim=1, keepdim=True) 
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(testloader.dataset)
    losses_test.append(test_loss)
    print('Test set: Average loss: {:.4f}\nAccuracy: {}/{} ({:.0f}%)'.format(
        test_loss, correct, len(testloader.dataset),
        100. * correct / len(testloader.dataset)))
    
    early_stopping(test_loss, model)
    if early_stopping.early_stop:
        print("Early stopping")
        print("total acc:{:.4f}\n".format((correct / len(testloader.dataset)+acc/len(trainloader))/2))
        time_elapsed = time.time() - since
        print('Training complete in {:.0f}m {:.0f}s'.format(
            time_elapsed // 60, time_elapsed % 60))
        break
    print("total acc:{:.4f}".format((correct / len(testloader.dataset)+acc/len(trainloader))/2))
    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s\n'.format(
        time_elapsed // 60, time_elapsed % 60))
epoch: 1
Average trainning loss:1.5747
Average Accuracy:0.5249
Test set: Average loss: 0.8811
Accuracy: 6810/10000 (68%)
Validation loss decreased (inf --> 0.881058).  Saving model ...
total acc:0.6029
Training complete in 0m 18s

epoch: 2
Average trainning loss:0.7524
Average Accuracy:0.7442
Test set: Average loss: 0.7077
Accuracy: 7396/10000 (74%)
Validation loss decreased (0.881058 --> 0.707704).  Saving model ...
total acc:0.7419
Training complete in 0m 18s
...    
epoch: 57
Average trainning loss:0.2447
Average Accuracy:0.9316
Test set: Average loss: 0.3118
Accuracy: 8885/10000 (89%)
EarlyStopping counter: 9 out of 10
total acc:0.9100
Training complete in 0m 19s

epoch: 58
Average trainning loss:0.2426
Average Accuracy:0.9325
Test set: Average loss: 0.3106
Accuracy: 8875/10000 (89%)
EarlyStopping counter: 10 out of 10
Early stopping
total acc:0.9100

Training complete in 0m 19s
fig = plt.figure(figsize=(14,8))
plt.plot(range(len(losses)),losses,label="train_loss")
plt.plot(range(len(losses)),losses_test,label="losses_test")
plt.legend()
<matplotlib.legend.Legend at 0x1908097d400>

在这里插入图片描述

print(model)
Net(
  (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (conv2): Conv2d(20, 50, kernel_size=(5, 5), stride=(1, 1))
  (fc1): Linear(in_features=800, out_features=500, bias=True)
  (fc2): Linear(in_features=500, out_features=10, bias=True)
)

text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat', 'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
dic = {}
for i in text_labels:
    dic[i] = 0
trainloader_total = torch.utils.data.DataLoader(trainset, batch_size=1, shuffle=True)
for images, labels in trainloader_total:

#     img = images[0].reshape((28, 28)).numpy()
#     plt.imshow(img)
#     plt.title(text_labels[labels[0]])
    dic[text_labels[labels[0]]] = dic[text_labels[labels[0]]] + 1 
#     plt.show()

num = []
for i in dic:
    num.append(dic[i])
fig = plt.figure(figsize=(14,8))
plt.bar(range(len(num)),num)
plt.xticks(range(len(num)),text_labels)
plt.ylabel("Number of samples")
Text(0, 0.5, 'Number of samples')

在这里插入图片描述

trainloader_tota2 = torch.utils.data.DataLoader(trainset, batch_size=60000, shuffle=True)
for images, labels in trainloader_tota2:
    
    img = images[0].reshape((28, 28)).numpy()
    break


#     plt.show()
Text(0.5, 1.0, 'coat')

在这里插入图片描述

import seaborn as sns
fig = plt.figure(figsize=(14,8))
fig = plt.subplot(121)
plt.imshow(img)
plt.title(text_labels[labels[0]])
fig = plt.subplot(122)

sns.heatmap(img,square=True)
<AxesSubplot:>

在这里插入图片描述

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-04-28 11:50:53  更:2022-04-28 11:52:34 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 8:24:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码