# 优化器

## 定义模型

```python
import torch
from torch.nn import Linear

model=Linear(1, 1)
```

## 1\_ torch.optim.SGD

随机梯度下降算法，带有动量（momentum）的算法作为一个可选参数可以进行设置，样例如下：

```python
#lr参数为学习率，对于SGD来说一般选择0.1 0.01.0.001
#如果设置了momentum，就是带有动量的SGD
optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
```

## 2\_ torch.optim.RMSprop

加快梯度下降的算法，利用RMSprop算法，可以减小某些维度梯度更新波动较大的情况，使其梯度下降的速度变得更快

```python
optimizer = torch.optim.RMSprop(model.parameters(), lr=0.01, alpha=0.99)
```

## 3\_ torch.optim.Adam

```python
# 这里的lr，betas，还有eps都是用默认值即可，所以Adam是一个使用起来最简单的优化方法
optimizer = torch.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-08)
```


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/pytorch/you-hua-suan-fa.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.
