1.线性回归

1.1 线性回归基本要素:模型,数据集(训练集,测试集),损失函数,优化函数
1.2 线性回归模型的实现
(1)生成数据集
(2)读取数据集
(3)初始化模型参数
(4)定义模型
(5)定义损失函数
(6)定义优化函数
(7)训练
1.3拓展笔记
task1笔记
task1笔记

2.softmax和分类模型

2.1获取Fashion-MNIST训练集和读取数据
import needed package
%matplotlib inline
from IPython import display
import matplotlib.pyplot as plt

import torch
import torchvision
import torchvision.transforms as transforms
import time

import sys
sys.path.append("/home/kesci/input")
import d2lzh1981 as d2l

print(torch.version)
print(torchvision.version)

mnist_train = torchvision.datasets.FashionMNIST(root=’/home/kesci/input/FashionMNIST2065’, train=True, download=True, transform=transforms.ToTensor())
mnist_test = torchvision.datasets.FashionMNIST(root=’/home/kesci/input/FashionMNIST2065’, train=False, download=True, transform=transforms.ToTensor())
feature, label = mnist_train[0]
print(feature.shape, label)
mnist_PIL = torchvision.datasets.FashionMNIST(root=’/home/kesci/input/FashionMNIST2065’, train=True, download=True)
PIL_feature, label = mnist_PIL[0]
print(PIL_feature)
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):
d2l.use_svg_display()
# 这里的_表示我们忽略(不使用)的变量
_, figs = plt.subplots(1, len(images), figsize=(12, 12))
for f, img, lbl in zip(figs, images, labels):
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]) # 将第i个feature加到X中
y.append(mnist_train[i][1]) # 将第i个label加到y中
show_fashion_mnist(X, get_fashion_mnist_labels(y))
batch_size = 256
num_workers = 4
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))
2.2softmax的实现

3.多层感知机

相关文章:

  • 2021-12-02
  • 2021-12-02
  • 2021-10-28
  • 2021-07-19
  • 2021-07-18
  • 2021-10-24
  • 2021-09-30
猜你喜欢
  • 2021-05-14
  • 2021-09-06
  • 2022-01-20
  • 2021-05-10
  • 2021-04-05
  • 2021-12-15
  • 2021-10-09
相关资源
相似解决方案