# 1️⃣ PyTorch cơ bản

### 1. PyTorch là gì? (hiểu đúng vai trò)

**PyTorch** là thư viện Deep Learning:

* Viết bằng Python
* Được dùng rất phổ biến trong:
  * Nghiên cứu
  * Học thuật
  * Prototype nhanh

Vì sao chọn PyTorch cho GitBook này:

* Code **dễ đọc**
* Gần với tư duy toán & mô hình
* Rất phù hợp cho **học từng bước**

***

### 2. Tensor – nền tảng của PyTorch

Trong PyTorch:

* **Tensor = dữ liệu số nhiều chiều**
* Có thể hiểu đơn giản:
  * Scalar → vector → matrix → tensor

Ví dụ trực giác:

* 1 số → tensor 0D
* Danh sách số → tensor 1D
* Bảng số → tensor 2D

👉 **Mọi thứ trong PyTorch đều là tensor**:

* Dữ liệu
* Weight
* Output
* Gradient

***

### 3. Mô hình trong PyTorch tương ứng Group 4 thế nào?

| Khái niệm lý thuyết | PyTorch                |
| ------------------- | ---------------------- |
| Neuron / Layer      | `nn.Module`            |
| Weight, Bias        | Tự quản lý trong layer |
| Activation          | `ReLU`, `Sigmoid`, …   |
| Loss Function       | `nn.*Loss`             |
| Backprop            | `loss.backward()`      |

👉 PyTorch **không che giấu** các khái niệm bạn đã học.

***

### 4. Cấu trúc tối thiểu của một mô hình PyTorch

Một mô hình PyTorch **luôn có 3 phần**:

1. **Định nghĩa model**
2. **Forward pass**
3. **Training loop**

Không hơn, không kém.

***

### 5. Ví dụ mô hình nhỏ nhất (đọc để hiểu, chưa cần chạy)

```python
import torch
import torch.nn as nn

class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = nn.Linear(3, 1)   # 3 input → 1 output
        self.act = nn.Sigmoid()

    def forward(self, x):
        x = self.fc(x)
        x = self.act(x)
        return x
```

Liên hệ ngay với Group 4:

* `nn.Linear` → **Layer + Neuron**
* `Sigmoid` → **Activation**
* `forward()` → **Forward pass**

***

### 6. Loss Function & Optimizer (ở mức nhận diện)

```python
criterion = nn.BCELoss()          # Loss
optimizer = torch.optim.SGD(
    model.parameters(), lr=0.01   # Learning rate
)
```

* **Loss**: đo sai (Group 4.4)
* **Optimizer**:
  * Dùng gradient
  * Cập nhật weight (Group 4.5)

👉 Optimizer **không phải mô hình**, nó chỉ giúp **sửa weight**.

***

### 7. Training loop – nơi mọi thứ ghép lại

```python
for epoch in range(epochs):
    optimizer.zero_grad()      # reset gradient
    output = model(x)          # forward
    loss = criterion(output, y)
    loss.backward()            # backprop
    optimizer.step()           # update weight
```

Liên hệ logic:

```
Forward → Loss → Backprop → Update → lặp lại
```

👉 Đây chính là **chuỗi 4.1 → 4.5** ở dạng code.

***

### 8. Những điều **chưa cần quan tâm** ở 6.1

⛔ Dataset phức tạp\
⛔ GPU / CUDA\
⛔ CNN / Transformer\
⛔ Tuning hyperparameter

👉 6.1 **chỉ để hiểu cấu trúc & dòng chảy**, không phải để “làm mạnh”.

***

### 9. Tóm tắt 6.1 (để ghi nhớ)

> **PyTorch không dạy Deep Learning mới.**\
> **PyTorch chỉ là cách viết lại Deep Learning bạn đã học bằng code.**

Câu nhớ nhanh:

```
Group 4 = hiểu logic
Group 6 = thấy logic đó trong code

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://deeplearningcoban.gitbook.io/deeplearningcoban.com/thuc-hanh-deep-learning-co-ban-hands-on/pytorch-co-ban.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
