新手小白的pytorch学习第十四弹------十一、十二、十三弹卷积神经网络CNN的习题

news2024/11/23 19:39:58

习题编号目录

  • No 1
  • No 2
  • No 3
  • No 4
  • No 5
  • No 6
  • No 7
  • No 8
  • No 9
  • No 10
  • No 11
  • No 12
  • No 13

练习题主要就是 写代码,所以这篇文章大部分是代码哟~

No 1

  1. What are 3 areas in industry where computer vision is currently being used?

No 2

工业异常检测,目标检测

  1. Search “what is overfitting in machine learning” and write down a sentence about what you find.

“overfitting”,就是过拟合,学的特征太多了,太细节了。
举个例子:
一个模型需要学习树叶,来分辨这个图片是不是树叶,但我们的数据比较有特点,是锯齿状的树叶,模型学到了锯齿状,于是就判定圆形的树叶不是树叶啦,这就是过拟合。
很明显过拟合会影响模型的泛化性能。

No 3

  1. Search “ways to prevent overfitting in machine learning”, write down 3 of the things you find and a sentence about each. Note: there are lots of these, so don’t worry too much about all of them, just pick 3 and start with those.
  • 数据集扩充:通过增加训练数据集的样本数量来减少模型对训练数据的过度拟合,从而提高模型的泛化性能。eg:生成一些类似但不完全相同的样本来扩充训练集。这些类似但不完全相同的样本可以通过对原始数据集进行一些变换操作得到,比如图像数据可以进行翻转、旋转、缩放等操作。

  • 正则化:可以通过在模型的损失函数中加入一项正则化项,对模型进行约束,使其在学习过程中对训练数据的拟合程度受到一定程度的限制。正则化的思想是通过对模型的参数进行惩罚,使得模型的权重变得更小,从而降低模型的复杂度,提高泛化能力。

  • 数据降维:将高维数据映射到低维空间,可以减少特征之间的冗余信息,从而提高模型的泛化能力。有两种方法:特征选择和特征提取。特征选择是指从原始特征中选择最相关的特征,从而达到降低数据维度的目的。而特征提取则是通过某种算法将原始特征转化为新的特征,从而达到降低数据维度的目的。

No 4

  1. Spend 20-minutes reading and clicking through the CNN Explainer website.
    * Upload your own example image using the “upload” button and see what happens in each layer of a CNN as your image passes through it.

No 5

  1. Load the torchvision.datasets.MNIST() train and test datasets.
import torchvision
import torchvision.datasets as datasets
from torchvision.transforms import ToTensor

train_data = datasets.MNIST(root='data',
                            train=True,
                            download=True,
                            transform=ToTensor(),
                            target_transform=None)
test_data = datasets.MNIST(root='data',
                           train=False,
                           download=True,
                           transform=ToTensor(),
                           target_transform=None)

No 6

  1. Visualize at least 5 different samples of the MNIST training dataset.
image, label = train_data[0]
image, label

(tensor([[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0118, 0.0706, 0.0706, 0.0706,
0.4941, 0.5333, 0.6863, 0.1020, 0.6510, 1.0000, 0.9686, 0.4980,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,

[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000]]]),
5)
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings…

class_names = train_data.classes
class_names

[‘0 - zero’,
‘1 - one’,
‘2 - two’,
‘3 - three’,
‘4 - four’,
‘5 - five’,
‘6 - six’,
‘7 - seven’,
‘8 - eight’,
‘9 - nine’]

train_data

Dataset MNIST
Number of datapoints: 60000
Root location: data
Split: Train
StandardTransform
Transform: ToTensor()

import matplotlib.pyplot as plt
image, label = train_data[0]
plt.imshow(image.squeeze(), cmap="gray")
plt.title(class_names[label])

在这里插入图片描述

import random
train_samples = []
train_labels = []
for sample, label in random.sample(list(train_data), k=5):
    train_samples.append(sample)
    train_labels.append(label)

train_samples[0].shape

torch.Size([1, 28, 28])

import torch
import matplotlib.pyplot as plt

torch.manual_seed(42)
plt.figure(figsize=(10, 5))
nrows = 1
ncols = 5
for i, sample in enumerate(train_samples):
    plt.subplot(nrows, ncols, i+1)
    plt.imshow(sample.squeeze(), cmap="gray")
    plt.axis(False)
    # plt.title(class_names[train_label])

在这里插入图片描述

No 7

  1. Turn the MNIST train and test datasets into dataloaders using torch.utils.data.DataLoader, set the batch_size=32.
from torch.utils.data import DataLoader

BATCH_SIZE=32

train_dataloader = DataLoader(dataset=train_data,
                              shuffle=True,
                              batch_size=BATCH_SIZE)
test_dataloader = DataLoader(dataset=test_data,
                             batch_size=BATCH_SIZE,
                             shuffle=False)
train_dataloader, test_dataloader

(<torch.utils.data.dataloader.DataLoader at 0x1f4d79a95e0>,
<torch.utils.data.dataloader.DataLoader at 0x1f4df3b0e50>)

No 8

  1. Recreate model_2 used in this notebook (the same model from the CNN Explainer website, also known as TinyVGG) capable of fitting on the MNIST dataset.
# 设备无关代码
device = "cuda" if torch.cuda.is_available() else "cpu"
device

‘cuda’

创建模型

import torch.nn as nn
# 创建 CNN 模型
class MNISTModelV0(nn.Module):
    def __init__(self, input_shape:int, hidden_units:int, output_shape:int):
        super().__init__()
        self.conv_block_1 = nn.Sequential(
            nn.Conv2d(in_channels=input_shape,
                      out_channels=hidden_units,
                      kernel_size=(3, 3),
                      stride=1,
                      padding=1),
            nn.ReLU(),
            nn.Conv2d(in_channels=hidden_units,
                      out_channels=hidden_units,
                      kernel_size=(3, 3),
                      stride=1,
                      padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(2, 2))
        )
        self.conv_block_2 = nn.Sequential(
            nn.Conv2d(in_channels=hidden_units,
                      out_channels=hidden_units,
                      kernel_size=(3, 3),
                      stride=1,
                      padding=1),
            nn.ReLU(),
            nn.Conv2d(in_channels=hidden_units,
                      out_channels=hidden_units,
                      kernel_size=(3, 3),
                      stride=1,
                      padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(2, 2))
        )
        self.classifier = nn.Sequential(
            nn.Flatten(),
            nn.Linear(in_features=hidden_units*7*7,
                      out_features=output_shape)
        )
        
        
    def forward(self, x):
        x = self.conv_block_1(x)
        # print(f"Output shape of conv_block_1:{x.shape}")
        x = self.conv_block_2(x)
        # print(f"Output shape of conv_block_2:{x.shape}")
        x = self.classifier(x)
        # print(f"Output shape of classifier:{x.shape}")
        return x
    
# 实例化模型
model_0 = MNISTModelV0(input_shape=1,
                       hidden_units=10,
                       output_shape=len(class_names))
model_0.to(device)

MNISTModelV0(
(conv_block_1): Sequential(
(0): Conv2d(1, 10, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU()
(2): Conv2d(10, 10, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU()
(4): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
)
(conv_block_2): Sequential(
(0): Conv2d(10, 10, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU()
(2): Conv2d(10, 10, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU()
(4): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
)
(classifier): Sequential(
(0): Flatten(start_dim=1, end_dim=-1)
(1): Linear(in_features=490, out_features=10, bias=True)
)
)

先搞个测试数据传一下,后面报错了,我们来找找是什么原因这个方法真的好用!!!

random_image_tensor = torch.randn(size=(1, 28, 28))
random_image_tensor.shape

torch.Size([1, 28, 28])

# 把这个测试数据传递给模型看看会不会报错
model_0.to(device)
model_0(random_image_tensor.unsqueeze(0).to(device))

tensor([[ 0.0366, -0.0940, 0.0686, -0.0485, 0.0068, 0.0290, 0.0132, 0.0084,
-0.0030, -0.0185]], device=‘cuda:0’, grad_fn=< AddmmBackward0>)

通过print方法,知道是模型中传递给网络的输入输出有问题

No 9

  1. Train the model you built in exercise 8. on CPU and GPU and see how long it takes on each.
image, label  = train_data[0]
image.shape

torch.Size([1, 28, 28])

BB,这些函数都是我们之前学习过的喔喔喔

# 损失函数和优化器
import torch.optim as optim
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.SGD(params=model_0.parameters(),
                      lr=0.1)
# 训练函数
def train_step(model:nn.Module,
               dataloader:DataLoader,
               loss_fn:nn.Module,
               optimizer:torch.optim.Optimizer,
               accuracy_fn,
               device:torch.device = device):
    # 设置循环周期
    train_loss, train_acc = 0, 0
    
    model.train()
    
    for batch, (X, y) in enumerate(dataloader):
        # 将数据放到统一的设备上
        X, y = X.to(device), y.to(device)
        
        y_pred = model(X)
        loss = loss_fn(y_pred,
                    y)
        train_loss += loss
        train_acc += accuracy_fn(y_true=y,
                                y_pred=y_pred.argmax(dim=1))
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
    train_loss /= len(dataloader)
    train_acc /= len(dataloader)
    
    print(f"Train Loss:{train_loss:.4f} | Train Acc:{train_acc:.2f}%")
# 测试函数
def test_step(model:nn.Module,
              dataloader:DataLoader,
              loss_fn:nn.Module,
              accuracy_fn,
              device:torch.device=device):
    """Peroforms a testing loop step on model going over data_loader"""
    test_loss, test_acc = 0, 0
    
    model.eval()
    
    with torch.inference_mode():
        for X, y in dataloader:
            X, y = X.to(device), y.to(device)
            y_pred = model(X)
            test_loss += loss_fn(y_pred, y)
            test_acc += accuracy_fn(y_true=y,
                            y_pred=y_pred.argmax(dim=1))
            
        test_loss /= len(dataloader)
        test_acc /= len(dataloader)
    print(f"Test Loss:{test_loss:.4f} | Test Acc:{test_acc:.2f}%\n")
# 评估函数
def eval_model(model:nn.Module,
               dataloader:DataLoader,
               loss_fn:nn.Module,
               accuracy_fn,
               device=device):
    """Returns a dictionary containing the results of model predicting on data_loader..."""
    loss, acc = 0, 0
    model.eval()
    with torch.inference_mode():
        for X, y in dataloader:
            X, y = X.to(device), y.to(device)
            y_pred = model(X)
            loss += loss_fn(y_pred, 
                           y)
            acc += accuracy_fn(y_true=y,
                              y_pred=y_pred.argmax(dim=1))
        loss /= len(dataloader)
        acc /= len(dataloader)
    
    return {"model_name":model.__class__.__name__,
            "model_loss":loss.item(),
            "model_acc":acc}
from timeit import default_timer as timer
def print_train_time(start:float,
                     end:float,
                     device:torch.device=None):
    """Prints difference between start and end time."""
    total_time = end - start
    print(f"Train time on {device}:{total_time:.3f} seconds")
    return total_time
# Calculate accuracy (a classification metric)
def accuracy_fn(y_true, y_pred):
    """Calculates accuracy between truth labels and predictions.

    Args:
        y_true (torch.Tensor): Truth labels for predictions.
        y_pred (torch.Tensor): Predictions to be compared to predictions.

    Returns:
        [torch.float]: Accuracy value between y_true and y_pred, e.g. 78.45
    """
    correct = torch.eq(y_true, y_pred).sum().item()
    acc = (correct / len(y_pred)) * 100
    return acc

训练模型

from tqdm.auto import tqdm

from helper_functions import accuracy_fn
# train and test on GPU
train_start_time_on_gpu = timer()

epochs=3

for epoch in tqdm(range(epochs)):
    print(f"Epoch:{epoch}\n")
    train_step(model=model_0,
               dataloader=train_dataloader,
               loss_fn=loss_fn,
               optimizer=optimizer,
               accuracy_fn=accuracy_fn,
               device=device)
    test_step(model=model_0,
              dataloader=test_dataloader,
              loss_fn=loss_fn,
              accuracy_fn=accuracy_fn,
              device=device)

train_end_time_on_gpu = timer()
total_time_on_gpu = print_train_time(start=train_start_time_on_gpu,
                                     end=train_end_time_on_gpu,
                                     device=device)

在这里插入图片描述
我简直了,我都不敢信,这准确率是多少!
来,Ada,张开你的卡姿兰大眼睛看看这个数字,到底是多少
啊啊啊啊啊,98%啊,天哪噜,98%,震惊!
喔霍霍,妈呀,好好好

model_0_result = eval_model(model=model_0,
                            dataloader=test_dataloader,
                            loss_fn=loss_fn,
                            accuracy_fn=accuracy_fn,
                            device=device)
model_0_result

{‘model_name’: ‘MNISTModelV0’,
‘model_loss’: 0.05393431708216667,
‘model_acc’: 98.10303514376997}

No 10

  1. Make predictions using your trained model and visualize at least 5 of them comparing the prediciton to the target label.
def make_prediction(model:nn.Module,
                    data: list,
                    device:torch.device=device):
    pred_probs=[]
    model.to(device)
    model.eval()
    with torch.inference_mode():
        for sample in data:
            sample = torch.unsqueeze(sample, dim=0).to(device)
            
            pred_logit = model(sample)
            pred_prob = torch.softmax(pred_logit.squeeze(),dim=0)
            pred_probs.append(pred_prob.cpu())
            
    # Stack the pred_probs to turn list into a tensor
    return torch.stack(pred_probs)
import random
test_samples = []
test_labels = []
for sample, label in random.sample(list(test_data),k=9):
    test_samples.append(sample)
    test_labels.append(label)
# make predictions
pred_probs = make_prediction(model=model_0,
                             data=test_samples,
                             device=device)
pred_probs

tensor([[1.0177e-07, 9.6744e-08, 9.0552e-05, 4.1145e-07, 3.1134e-09, 9.2933e-08,
1.0455e-07, 8.3808e-07, 9.9990e-01, 1.0561e-05],
[7.0788e-10, 1.9963e-08, 1.1445e-07, 7.4835e-07, 1.0650e-04, 1.4251e-06,
2.5626e-12, 5.2849e-06, 8.3278e-06, 9.9988e-01],
[2.0574e-11, 2.7979e-05, 9.9959e-01, 3.6853e-04, 1.0323e-13, 1.5242e-13,
1.8023e-12, 9.5447e-06, 5.1182e-07, 4.4869e-12],
[2.2204e-10, 6.1201e-10, 4.8230e-05, 1.4246e-06, 3.8657e-12, 6.7251e-07,
9.1211e-10, 1.9719e-08, 9.9995e-01, 4.0643e-07],
[2.1181e-15, 8.9004e-13, 4.7079e-13, 3.2457e-08, 5.5606e-11, 1.0000e+00,
9.1563e-09, 4.4056e-17, 3.3977e-08, 3.1905e-08],
[1.4000e-05, 5.6517e-10, 1.0992e-05, 4.6077e-11, 9.9994e-01, 5.4655e-09,
4.8744e-06, 8.1010e-06, 7.0937e-08, 2.0188e-05],
[9.9992e-01, 6.8991e-11, 7.1014e-05, 6.2839e-11, 3.7751e-11, 2.0786e-07,
7.2334e-07, 9.5343e-10, 4.6806e-06, 7.3413e-08],
[7.6309e-13, 3.0353e-10, 7.0295e-10, 4.9777e-07, 2.5954e-14, 8.0768e-12,
9.5778e-17, 1.0000e+00, 1.0831e-10, 1.9796e-08],
[1.4944e-11, 7.4691e-13, 2.8261e-12, 6.1482e-08, 1.7732e-11, 1.0000e+00,
4.6807e-09, 2.5018e-11, 1.3533e-07, 2.7457e-06]])

pred_classes = pred_probs.argmax(dim=1)
pred_classes

tensor([8, 9, 2, 8, 5, 4, 0, 7, 5])

# plot predictions
plt.figure(figsize=(9,9))
nrows = 3
ncols = 3
for i, sample in enumerate(test_samples):
    plt.subplot(nrows, ncols, i+1)
    
    plt.imshow(sample.squeeze(), cmap="gray")
    pred_label = class_names[pred_classes[i]]
    truth_label = class_names[test_labels[i]]
    
    title_text = f"Pred:{pred_label} | Truth:{truth_label}"
    
    # 预测正确是绿色的title, 预测错误是红色的title
    if pred_label == truth_label:
        plt.title(title_text, fontsize=10, c="g")
    else:
        plt.title(title_text, fontsize=10, c="r")
        
    plt.axis(False);

在这里插入图片描述

No 11

  1. Plot a confusion matrix comparing your model’s predictions to the truth labels.
from tqdm.auto import tqdm

# 1 在测试数据上使用我们训练好的模型
y_preds = []
model_0.eval()
with torch.inference_mode():
    for X, y in test_dataloader:
        X, y = X.to(device), y.to(device)
        y_logit = model_0(X)
        y_pred = torch.softmax(y_logit.squeeze(), dim=0).argmax(dim=1)
        y_preds.append(y_pred.cpu())

# Concatenate list of predictions into a tensor
y_pred_tensor = torch.cat(y_preds)
y_pred_tensor

tensor([7, 2, 1, …, 4, 5, 6])

from torchmetrics import ConfusionMatrix
from mlxtend.plotting import plot_confusion_matrix

# 2 制作confusion实例,将预测和标签进行比较
confmat = ConfusionMatrix(num_classes=len(class_names),
                          task='multiclass')
confmat_tensor = confmat(preds = y_pred_tensor,
                         target = test_data.targets)

# 3 plot the confusion matrix
fig, ax = plot_confusion_matrix(
    conf_mat=confmat_tensor.numpy(), # matplotlib likes working with numpy
    class_names = class_names,
    figsize=(10, 7)
)

在这里插入图片描述

No 12

  1. Create a random tensor of shape [1, 3, 64, 64] and pass it through a nn.Conv2d() layer with various hyperparameter settings (these can be any settings you choose), what do you notice if the kernel_size parameter goes up and down?
import random
random_tensor = torch.randn(size=(1, 3, 64, 64))
random_tensor

tensor([[[[-0.6684, 0.4637, -0.3516, …, -0.6252, 0.6887, 0.2075],
[-1.1143, -1.2353, 0.3464, …, -0.7403, 0.3211, 0.2074],
[-0.1284, -1.0946, 0.4482, …, 0.7574, -0.2992, -0.0710],
…,
[ 1.5077, 0.2374, 0.2925, …, -0.2159, -0.6532, -1.2062],
[ 1.9584, 0.8458, 0.3636, …, 0.3946, 0.1667, -1.6578],
[ 1.1362, 1.9202, 1.0445, …, -0.4046, 0.9407, -0.3916]],
[-0.8766, -0.2172, 0.5522, …, -0.0553, -0.6218, 0.3710],
…,
[ 1.8482, -0.2374, 1.4276, …, -0.2379, 0.7371, -1.8488],
[-0.6760, 1.0012, -0.3990, …, -1.4388, -2.0143, -0.9468],
[-0.7640, 0.7572, 0.8566, …, -0.8063, -1.2623, -0.0394]]]])

conv_layer = nn.Conv2d(in_channels=3,
                       out_channels=2,
                       kernel_size=(3, 3),
                       stride=1,
                       padding=1)
# 将随机生成的数据放到 conv_layer 中
random_tensor_through_conv = conv_layer(random_tensor)
random_tensor_through_conv.shape

torch.Size([1, 2, 64, 64])

conv_layer = nn.Conv2d(in_channels=3,
                       out_channels=2,
                       kernel_size=(5,5),
                       stride=1,
                       padding=1)

# 将随机生成的数据放到 conv_layer 中
random_tensor_through_conv = conv_layer(random_tensor)
random_tensor_through_conv.shape

torch.Size([1, 2, 62, 62])

conv_layer = nn.Conv2d(in_channels=3,
                       out_channels=2,
                       kernel_size=(10, 10),
                       stride=1,
                       padding=1)

# 将随机生成的数据放到 conv_layer 中
random_tensor_through_conv = conv_layer(random_tensor)
random_tensor_through_conv.shape

torch.Size([1, 2, 57, 57])

conv_layer = nn.Conv2d(in_channels=3,
                       out_channels=2,
                       kernel_size=(30, 30),
                       stride=1,
                       padding=1)

# 将随机生成的数据放到 conv_layer 中
random_tensor_through_conv = conv_layer(random_tensor)
random_tensor_through_conv.shape

torch.Size([1, 2, 37, 37])

conv_layer = nn.Conv2d(in_channels=3,
                       out_channels=2,
                       kernel_size=(64, 64),
                       stride=1,
                       padding=1)

# 将随机生成的数据放到 conv_layer 中
random_tensor_through_conv = conv_layer(random_tensor)
random_tensor_through_conv.shape

torch.Size([1, 2, 3, 3])

conv_layer = nn.Conv2d(in_channels=3,
                       out_channels=2,
                       kernel_size=(65, 65),
                       stride=1,
                       padding=1)

# 将随机生成的数据放到 conv_layer 中
random_tensor_through_conv = conv_layer(random_tensor)
random_tensor_through_conv.shape

torch.Size([1, 2, 2, 2])

conv_layer = nn.Conv2d(in_channels=3,
                       out_channels=2,
                       kernel_size=(66, 66),
                       stride=1,
                       padding=1)

# 将随机生成的数据放到 conv_layer 中
random_tensor_through_conv = conv_layer(random_tensor)
random_tensor_through_conv.shape

torch.Size([1, 2, 1, 1])

卷积核的大小,padding,stride都影响降低维度的量,比如我的padding是1,那我的kernel_size就可以达到66,而不是刚开始的64,就可以将维度降为1

No 13

  1. Use a model similar to the trained model_2 from this notebook to make predictions on the test torchvision.datasets.FashionMNIST dataset.
    * Then plot some predictions where the model was wrong alongside what the label of the image should’ve been.
    * After visualing these predictions do you think it’s more of a modelling error or a data error?
    * As in, could the model do better or are the labels of the data too close to each other (e.g. a “Shirt” label is too close to “T-shirt/top”)?
fashion_train_data = datasets.FashionMNIST(root='data',
                                           train=True,
                                           transform=ToTensor(),
                                           download=True,
                                           target_transform=None)
fashion_test_data = datasets.FashionMNIST(root='data',
                                          train=False,
                                          download=True,
                                          transform=ToTensor(),
                                          target_transform=None)
fashion_train_data,fashion_test_data

(Dataset FashionMNIST
Number of datapoints: 60000
Root location: data
Split: Train
StandardTransform
Transform: ToTensor(),
Dataset FashionMNIST
Number of datapoints: 10000
Root location: data
Split: Test
StandardTransform
Transform: ToTensor())

fashion_train_dataloader = DataLoader(dataset=fashion_train_data,
                                      batch_size=BATCH_SIZE,
                                      shuffle=True)
fashion_test_dataloader = DataLoader(dataset=fashion_test_data,
                                     batch_size=BATCH_SIZE,
                                     shuffle=True)

len(fashion_train_dataloader),len(fashion_test_dataloader)

使用我们创建的模型来训练一下这个FashionMNIST数据集

train_start_time_on_fashion = timer()

epochs = 3
for epoch in tqdm(range(epochs)):
    print(f"Epoch:{epoch}\n======================")
    train_step(model=model_0,
               dataloader=fashion_train_dataloader,
               loss_fn=loss_fn,
               optimizer=optimizer,
               device=device,
               accuracy_fn=accuracy_fn)
    test_step(model=model_0,
              dataloader=fashion_test_dataloader,
              loss_fn=loss_fn,
              accuracy_fn=accuracy_fn,
              device=device)
    
train_end_time_on_fashion = timer()
total_time_on_fashion = print_train_time(start=train_start_time_on_fashion,
                                         end=train_end_time_on_fashion,
                                         device=device)

在这里插入图片描述

model_0_result_fashion = eval_model(model=model_0,
                                    dataloader=fashion_test_dataloader,
                                    loss_fn=loss_fn,
                                    accuracy_fn=accuracy_fn,
                                    device=device)
model_0_result_fashion

{‘model_name’: ‘MNISTModelV0’,
‘model_loss’: 0.31344807147979736,
‘model_acc’: 89.12739616613419}

model_0_result

{‘model_name’: ‘MNISTModelV0’,
‘model_loss’: 0.05393431708216667,
‘model_acc’: 98.10303514376997}

import pandas as pd
compare_results = pd.DataFrame([model_0_result,
                                model_0_result_fashion])
compare_results

在这里插入图片描述

compare_results["training_time"] = [total_time_on_gpu,
                                    total_time_on_fashion]
compare_results

在这里插入图片描述

compare_results["dataset"] = ["MNIST",
                              "FashionMNIST"]
compare_results

在这里插入图片描述

compare_results.set_index("dataset")["model_acc"].plot(kind="barh")
plt.ylabel("dataset")
plt.xlabel("accuracy(%)");

在这里插入图片描述

import random
test_samples = []
test_labels = []
for sample, label in random.sample(list(fashion_test_data), k=9):
    test_samples.append(sample)
    test_labels.append(label)
    
#查看第一个样本的形状
test_samples[0].shape

torch.Size([1, 28, 28])

# make predictions
pred_probs = make_prediction(model=model_0,
                             data=test_samples,
                             device=device)
pred_classes = pred_probs.argmax(dim=1)

# plot predictions
plt.figure(figsize=(9,9))
nrows = 3
ncols = 3
for i, sample in enumerate(test_samples):
    plt.subplot(nrows, ncols, i+1)
    
    plt.imshow(sample.squeeze(), cmap="gray")
    pred_label = class_names[pred_classes[i]]
    truth_label = class_names[test_labels[i]]
    
    title_text = f"Pred:{pred_label} | Truth:{truth_label}"
    
    # 预测正确是绿色的title, 预测错误是红色的title
    if pred_label == truth_label:
        plt.title(title_text, fontsize=10, c="g")
    else:
        plt.title(title_text, fontsize=10, c="r")
        
    plt.axis(False);

在这里插入图片描述
这这这,两个不同的数据集用了同一个模型,效果很明显是不一样的昂,我觉得数据的关系大一点吧,我们需要根据数据来调整模型的结构,或者用手段来提取数据特征让模型更好的学习,感觉都有关系,不过我认为还是要了解数据的结构,来构建模型,让模型学习数据中的模式。

OK,BB,终于把习题搞完了,oh, yeah, 取得了今天的小小胜利。

我今天在群里抢红包,运气王哎,获得了买一送一的优惠,刚刚尝了一下,很好吃的冬枣,看到这里,就把好运传给你咯,记得接一下~

晚上的米线很好吃,以至于忘记带回我的冬枣了,又回去拿啦,我送了一盒给那个小哥,一是感谢他,而是想分点好运给他,很开心,他应该也很开心吧~

好耶,BB,文档有用的话,给我点个赞赞~
谢谢您~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1953249.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C语言 -- sizeof和strlen的用法

C语言 -- sizeof和strlen的用法 1. sizeof和strlen的对比​1.1 sizeof​1.2 strlen​1.3 sizeof 和 strlen的对比​ 2. 数组和指针笔试题解析2.1 一维数组​2.2 字符数组​2.3 二维数组 3. 指针运算笔试题解析3.1 题目1&#xff1a;3.2 题目2​3.3 题目3​3.4 题目4​3.5 题目5…

力扣高频SQL 50题(基础版)第十九题

文章目录 力扣高频SQL 50题&#xff08;基础版&#xff09;第十九题1211. 查询结果的质量和占比题目说明思路分析实现过程准备数据实现方式结果截图 力扣高频SQL 50题&#xff08;基础版&#xff09;第十九题 1211. 查询结果的质量和占比 题目说明 Queries 表&#xff1a; …

学习c语言第十五天(初阶测评)

选择题 1.下列程序输出结果为 67 2.下列程序输出结果为 死循环打印 3.i 和j的值分别为什么 2 1 4.k的终值是什么 90 5.输出结果是什么 1 6.正确的是 C 7. C语言规定main函数位置 C 8.不正确的是 D 9.正确的是 c 库函数独立c语言之外 10…

comsol在静水压力下的地应力平衡

comsol在静水压力下的地应力平衡 条件: 水深3m,粗砂层厚度10m,模型长度100m土体的饱和密度1950kg/m3水的密度1000kg/m3浮重度 γ ′ = γ s a t − γ w \gamma=\gamma_{sat}-\gamma_w γ

GD32 PWM输入捕获

前言 通过本次的学习&#xff0c;了解定时器的分类和配置&#xff0c;高级定时器&#xff0c;通用定时器等不同等级的定时器拥有的功能&#xff0c;学习定时器的输入捕获与输出比较功能&#xff08;PWM&#xff09;脉冲宽度调制&#xff0c;来控制电机等外设。 定义 定时器的分…

Pytorch学习笔记——在GPU上进行训练

文章目录 1. 环境准备2. 导入必要库3. 加载数据集4. 定义简单的神经网络模型5. 检查和设置GPU设备6. 定义损失函数和优化器7. 训练模型8. 全部代码展示及运行结果 1. 环境准备 首先&#xff0c;确保PyTorch已经安装&#xff0c;且CUDA&#xff08;NVIDIA的并行计算平台和编程模…

go-kratos 学习笔记(6) 数据库gorm使用

数据库是项目的核心&#xff0c;数据库的链接数据是data层的操作&#xff0c;选择了比较简单好用的gorm作为数据库的工具&#xff1b;之前是PHP开发&#xff0c;各种框架都是orm的操作&#xff1b;gorm还是很相似的&#xff0c;使用起来比较顺手 go-kratos官网的实例是ent&…

鸿蒙UI系统组件10——菜单(Menu)

果你也对鸿蒙开发感兴趣&#xff0c;加入“Harmony自习室”吧&#xff01;扫描下面名片&#xff0c;关注公众号。 Menu是菜单接口&#xff0c;一般用于鼠标右键弹窗、点击弹窗等。 1、创建默认样式的菜单 菜单需要调用bindMenu接口来实现。bindMenu响应绑定组件的点击事件&am…

ModuleNotFoundError: No module named ‘py3langid‘ 以及如何将包安在虚拟环境下

前提&#xff1a;已经安装过改包&#xff08;pip install py3langid&#xff09;&#xff0c;但仍报错 原因&#xff1a;安装在其他目录下了 解决办法&#xff1a; 1、再次在终端输入pip install py3langid 显示安装位置 Requirement already satisfied: py3langid in c:\…

css大屏设置中间元素四周渐变透明效果

css大屏设置中间元素四周渐变透明效果 四周透明效果&#xff1a; // 设置蒙版上下左右渐变显示mask-image: linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 1) 10%, rgba(0, 0, 0, 1) 90%, rgba(0, 0, 0, 0) 100%),linear-gradient(to bottom, rgba(0, 0, 0…

性能测试的指标及流程

性能测试指标 相应时间 并发数 吞吐量&#xff1a; 点击数 错误率 资源使用率 所有的东西是存在磁盘里的&#xff0c;在代码运行的时候会将磁盘的东西读取到内存里&#xff0c;磁盘IO和网络都是衡量速度&#xff0c;在任务管理器可查看资源使用率 题&#xff1a; 答案&#xf…

创建vue3项目,以及使用示例

1.在根目录下cmd&#xff1a;vue create myobj&#xff08;没有切换淘宝镜像记得切换&#xff0c;这样创建项目运行快&#xff09; 2. 3.(按空格键选中&#xff0c;选好回撤就到下一步了) 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.然后输入执行以下两步就已经运行项目了 以…

Java算法之递归算法-如何计算阶乘的值

上一篇学了递归之后&#xff0c;练习一下递归算法。 题目&#xff1a;使用递归算法计算阶乘的值&#xff0c;也就是5&#xff01;5*4*3*2*1&#xff0c;直接使用循环是非常简单的&#xff0c;这边练习一下递归算法。 先写一下两个条件 基线条件&#xff1a;等于1的时候返回1…

windows下实现mongodb备份还原

添加环境变量 把mongodb安装目录下的bin路径添加到环境变量的path路径: 备份库 打开CMD&#xff0c;执行以下命令&#xff1a; mongodump -u test -p test -d test -o D://backup_mongodb//20220706 –gzip 参数说明&#xff1a; -u 用户名 -p 密码 -d 需要备份的库名称…

GraphHopper路劲规划导航(Android源码调试运行)

本文主要记录在运行graphhopper安卓版路径规划导航源码的步骤和遇到的问题。 成功运行了程序&#xff0c;但是路劲规划一直不成功&#xff0c;问题一开始是服务地址&#xff0c;后来又是key的问题&#xff0c;在这个项目中涉及到了graphhopper、mapbox、mapilion的key&#xff…

map、foreach、filter这些方法你还不知道什么时候该用哪个吗?那就看过来

forEach&#xff1a;‌主要用于遍历数组并对每个元素执行某种操作&#xff0c;‌通常用于改变当前数组里的值。‌它不会返回新数组&#xff0c;‌而是直接在原数组上进行操作。‌forEach方法不支持return、‌break、‌continue等语句&#xff0c;‌因为这些语句在forEach中不会…

多线程实例-线程池

线程池&#xff0c;就是把线程提前从系统中申请好&#xff0c;放到一个地方&#xff0c;后面需要使用线程的时候&#xff0c;直接从这个地方取&#xff0c;而不是从系统重新申请&#xff0c;线程用完之后也回到刚才的地方。 线程池的优点&#xff1a;降低线程创建和销毁的开销…

MICA:面向复杂嵌入式系统的混合关键性部署框架

背景 在嵌入式场景中&#xff0c;虽然 Linux 已经得到了广泛应用&#xff0c;但并不能覆盖所有需求&#xff0c;例如高实时、高可靠、高安全的场合。这些场合往往是实时操作系统的用武之地。有些应用场景既需要 Linux 的管理能力、丰富的生态&#xff0c;又需要实时操作系统的高…

戴尔vostro15-3568硬盘升级+系统重装

硬盘升级 原2.5机械硬盘换成了SATA2.5的固态硬盘 按F2进入bios后看到的电池信息如下&#xff1a; 需要重新换一个电池 系统重装 步骤如下 1.U盘需要格式化成 NTFS 类型的&#xff0c;并且从官网下载后介质 2.BV1z3411K7AD b站这个视频前三步可以参考设置