Files
2024-06-10 17:00:23 +08:00

3.7 KiB

练习 A.3

In [2]:
import torch

class NeuralNetwork(torch.nn.Module):
    def __init__(self, num_inputs, num_outputs):
        super().__init__()

        self.layers = torch.nn.Sequential(
                
            # 第一个隐藏层
            torch.nn.Linear(num_inputs, 30),
            torch.nn.ReLU(),

            # 第二个隐藏层
            torch.nn.Linear(30, 20),
            torch.nn.ReLU(),

            # 输出层
            torch.nn.Linear(20, num_outputs),
        )

    def forward(self, x):
        logits = self.layers(x)
        return logits
In [3]:
model = NeuralNetwork(2, 2)

num_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Total number of trainable model parameters:", num_params)
Total number of trainable model parameters: 752

练习 A.4

In [1]:
import torch
# 创建随机向量
a = torch.rand(100, 200)
b = torch.rand(200, 300)
In [2]:
# 使用 @ 符号进行矩阵相乘,并计算执行时间
# %timeit 是 IPython 提供的魔术命令,用于多次执行代码以获取平均执行时间
# 它会自动选择执行次数以确保结果的准确性
%timeit a @ b
63.8 µs ± 8.7 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [3]:
# 将 a 和 b 移动到 CUDA 设备上以利用 GPU 加速计算
a, b = a.to("cuda"), b.to("cuda")
In [4]:
%timeit a @ b
13.8 µs ± 425 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)