add fourth chapter

This commit is contained in:
skindhu 2024-11-05 17:20:03 +08:00
parent 3e3c9d6634
commit 9ec8efd0d8
1 changed files with 6 additions and 6 deletions

View File

@ -108,10 +108,10 @@ class DummyGPTModel(nn.Module):
class DummyTransformerBlock(nn.Module): #C class DummyTransformerBlock(nn.Module): #C
def __init__(self, cfg): def __init__(self, cfg):
super().__init__() super().__init__()
def forward(self, x): #D def forward(self, x): #D
return x return x
class DummyLayerNorm(nn.Module): #E class DummyLayerNorm(nn.Module): #E
def __init__(self, normalized_shape, eps=1e-5): #F def __init__(self, normalized_shape, eps=1e-5): #F
@ -244,8 +244,8 @@ print(out)
```python ```python
tensor([[0.2260, 0.3470, 0.0000, 0.2216, 0.0000, 0.0000], tensor([[0.2260, 0.3470, 0.0000, 0.2216, 0.0000, 0.0000],
[0.2133, 0.2394, 0.0000, 0.5198, 0.3297, 0.0000]], [0.2133, 0.2394, 0.0000, 0.5198, 0.3297, 0.0000]],
grad_fn=<ReluBackward0>) grad_fn=<ReluBackward0>)
``` ```
我们实现的神经网络层包含一个线性层,后接一个非线性激活函数 ReLU这是神经网络中的标准激活函数。如果你不熟悉 ReLU只需了解它的作用是将负值设为 0确保输出层中没有负值。在 GPT 中,我们将使用另一种更复杂的激活函数,后续章节会介绍。 我们实现的神经网络层包含一个线性层,后接一个非线性激活函数 ReLU这是神经网络中的标准激活函数。如果你不熟悉 ReLU只需了解它的作用是将负值设为 0确保输出层中没有负值。在 GPT 中,我们将使用另一种更复杂的激活函数,后续章节会介绍。
@ -264,11 +264,11 @@ print("Variance:\n", var)
```python ```python
Mean: Mean:
tensor([[0.1324], tensor([[0.1324],
[0.2170]], grad_fn=<MeanBackward1>) [0.2170]], grad_fn=<MeanBackward1>)
Variance: Variance:
tensor([[0.0231], tensor([[0.0231],
[0.0398]], grad_fn=<VarBackward0>) [0.0398]], grad_fn=<VarBackward0>)
``` ```
以上均值张量的第一行包含第一个输入样本的均值,第二行输出包含第二个输入样本的均值。 以上均值张量的第一行包含第一个输入样本的均值,第二行输出包含第二个输入样本的均值。