ai - llm 从头开始 pytorch 1 上手
访问量: 10
refer to: https://www.doubao.com/thread/wa01e19bc66e8aa03
安装好 cuda, pytorch https://pytorch.org/get-started/locally/
(我的显卡是3080 20G, windows, 所以豆包推荐了我这个:)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
验证:
1 import torch 2 3 print(torch.cuda.is_available()) # 返回 True x = torch.rand(5,3) print(x)

PS C:\workspace\llm\test_pytorch> python .\run1.py
tensor([[0.7051, 0.7190, 0.4745],
[0.0782, 0.2797, 0.1021],
[0.4358, 0.5520, 0.9624],
[0.5475, 0.9502, 0.9342],
[0.4325, 0.3547, 0.9727]])
开始 https://docs.pytorch.org/tutorials/
一步一步的把这个 教程的代码敲下来,运行即可。

解释见:https://www.doubao.com/thread/wdacb54258bc3cce5

比較核心的地方:
34 class NeuralNetwork(nn.Module): 35 def __init__(self): 36 super().__init__() 37 self.flatten = nn.Flatten() # 展平图片 38 39 # 创建3层神经网络 40 self.linear_relu_stack = nn.Sequential( 41 nn.Linear(28*28, 512), # 783 -> 512个神经元 42 nn.ReLU(), 43 nn.Linear(512, 512), # 512 -> 512 个神经元 44 nn.ReLU(), 45 nn.Linear(512,10) # 输出 10个数字 46 ) 47 48 def forward(self, x): 49 x = self.flatten(x) 50 logits = self.linear_relu_stack(x) 51 return logits 52 53 model = NeuralNetwork().to(device) 54 print(model)
根據這個解释: https://www.doubao.com/thread/we21293ffe34d46ac , 可以是多层,可以不是512,而是 400 这样。
dataset:
这里提供的是训练用的数据。
为了把代码弄简洁,更好使用,所以直接用一个lib 来引入了。
想打印这些图片,用这个脚本:
import torch
from torchvision import datasets
from torchvision.transforms import ToTensor
import numpy as np
from PIL import Image # 用来保存图片
# 加载数据集
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor()
)
# 衣服类别名称
classes = [
"T-shirt-top",
"Trouser",
"Pullover",
"Dress",
"Coat",
"Sandal",
"Shirt",
"Sneaker",
"Bag",
"Ankle boot"
]
# 保存前 20 张图片
for i in range(20):
img_tensor, label = training_data[i]
# torch 张量 → numpy → 28x28 灰度图
img_np = img_tensor.squeeze().numpy()
img_np = (img_np * 255).astype(np.uint8) # 转回 0~255 像素
# 保存成图片
img = Image.fromarray(img_np)
filename = f"fashion_{i:02d}_{classes[label]}.png"
img.save(filename)
print(f"已保存:{filename}")
运行后,就可以看到很多鞋,裤子,衣服的黑白小图了。
