提交 f7a1bfb4 编写于 作者: S ShusenTang

fix bug #54: specify dtype

上级 d248817a
因为 它太大了无法显示 source diff 。你可以改为 查看blob
......@@ -27,9 +27,11 @@ num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
features = torch.from_numpy(np.random.normal(0, 1, (num_examples, num_inputs)))
features = torch.randn(num_examples, num_inputs,
dtype=torch.float32)
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += torch.from_numpy(np.random.normal(0, 0.01, size=labels.size()))
labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()),
dtype=torch.float32)
```
注意,`features`的每一行是一个长度为2的向量,而`labels`的每一行是一个长度为1的向量(标量)。
......@@ -113,8 +115,8 @@ tensor([[-1.4239, -1.3788],
我们将权重初始化成均值为0、标准差为0.01的正态随机数,偏差则初始化成0。
``` python
w = torch.tensor(np.random.normal(0, 0.01, (num_inputs, 1)), dtype=torch.float64)
b = torch.zeros(1, dtype=torch.float64)
w = torch.tensor(np.random.normal(0, 0.01, (num_inputs, 1)), dtype=torch.float32)
b = torch.zeros(1, dtype=torch.float32)
```
之后的模型训练中,需要对这些参数求梯度来迭代参数的值,因此我们要让它们的`requires_grad=True`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册