Home Work 1
这个是hungyi老师助教的pytorch教程,简单记录一下。
Dataset & Dataloader
Dataset
就是存数据结构的Dataloader
是把数据用batch的格式存起来,分成一批一批的,同时能够并行计算。1
dataset = MyDataset(file)
1
dataloader = DataLoader(dataset,batch_size,shuffle=True)
其中
shuffle
在training的时候设置为True,在testing的时候设置为False如何构建
Dataset
和Dataloader
1
2
3
4
5
6
7
8
9
10
11
12
13
14from torch.utils.data import Dataset, Dataloader
class Mydataset(Dataset):
# 读取数据和做预处理
def __init__(self, file):
self.data = ...
# 一次返回一个样例
def __getten__(self, index):
return self.data[index]
# 返回数据集的大小
def __len__(self):
return len(self.data)Dataloader
的作用就是从Dataset
中每次调用__getten__(i)
然后组成一个mini-batch(大小为batch_size
)
Tensors
简单来说就是高纬的数据结构
查看tensor的维度:
tensor.shape()
创建Tensors
可以直接从numpy或者list创建
1
2
3
4
5
6x = torch.tensor([[1,-1],[1,-1]])
x = torch.from_numpy(np.arry([[1,-1],[1,-1]]))
'''
tensor([[1., -1.],
[1., -1.]])
'''创建全为0或者全为1的
1
2
3
4
5
6x = torch.zeros([2,2]) # shape
'''
tensor([[0., 0.],
[0., 0.]])
'''
x = torch.ones([1,2,5])
经典的操作
Addition
- z = x + y
Subtraction
- z = x - y
Power
- y = x.pow(2)
Summation
- y = x.sum()
Mean
- y = x.mean()
转置 transpose
1
2
3
4
5
6
7
8
9
10
11
12
132,3]) x = torch.zeros([
x.shape
torch.Size([2, 3])
x
tensor([[0., 0., 0.],
[0., 0., 0.]])
0,1) # 交换dim = 0 和 dim = 1 x = x.transpose(
x.shape
torch.Size([3, 2])
x
tensor([[0., 0.],
[0., 0.],
[0., 0.]])
添加 or 删除维度,这并不会导致信息的丢失,只是改变维度
1
2
3
4
5
61,2,3]) x = torch.zeros([
x.shape
torch.Size([1, 2, 3])
0) # dim = 0 x = x.squeeze(
x.shape
torch.Size([2, 3])1
2
3
4
5x.shape
torch.Size([2, 3])
1) # 在原来的dim = 1的位置添加一个维度 x = x.unsqueeze(
x.shape
torch.Size([2, 1, 3])连接多个维度的tensors
数据类型
| Data Type | dtype | tensor |
| ———————— | —————- | ————————- |
| 32bit 浮点数 | torch.float | torch.FloatTensor |
| 64bit 带符号整数 | torch.long | torch.LongTensor |选择device
1
2
3torch.cuda.is_available() # 查看是否有GPU
X = x.to('cpu')
X = x.to('cuda')Gradient Calculation 计算梯度
直接放图吧
搭建模型
network layers
例如全连接层:
nn.Liner(in_features, out_features)
就是只需要定义最后一个输入维度和输出维度还有
nn.Sigmoid(), nn.ReLU()
build own neural network
很简单的例子:
等价写法
计算损失
- 类别
- MSE 用于回归任务
criterion = nn.MSELoss()
- Cross Entropy 用于分类任务
criterion = nn.CrossEntropyLoss()
- MSE 用于回归任务
- 计算loss很简单:
loss = criterion(model_output, labels)
选择优化方法
作业是只用了梯度下降法:
Stochastic Gradient Descent(SGS):torch.optim.SGD(model.parameters(), lr, momentum = 0)
for every batch of data:
- call
optimizer.zero_grad()
to reset gradients of model parameters. - call
loss.backward()
to backpropagate gradients of prediction loss. - call
optimizer.step()
to adjust model parameters.
- call
完整的流程
1 | dataset = MyDataset(file) # read data via MyDataset |
HomeWork 1
没去用kaggle测分数,简单写了下
https://github.com/qshen0629/machine-learing-HW/tree/main/ml2022spring-hw1