Skip to content

Commit

Permalink
PyTorch: Avoid creating ragged tensors supporting NumPy>=1.24 (#2435)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirudhDagar committed Feb 1, 2023
1 parent 55ece0c commit 4f04e54
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion chapter_optimization/sgd.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def f_grad(x1, x2): # Gradient of the objective function
```

```{.python .input}
#@tab mxnet, pytorch
#@tab mxnet
def sgd(x1, x2, s1, s2, f_grad):
g1, g2 = f_grad(x1, x2)
# Simulate noisy gradient
Expand All @@ -82,6 +82,17 @@ def sgd(x1, x2, s1, s2, f_grad):
return (x1 - eta_t * g1, x2 - eta_t * g2, 0, 0)
```

```{.python .input}
#@tab pytorch
def sgd(x1, x2, s1, s2, f_grad):
g1, g2 = f_grad(x1, x2)
# Simulate noisy gradient
g1 += torch.normal(0.0, 1, (1,)).item()
g2 += torch.normal(0.0, 1, (1,)).item()
eta_t = eta * lr()
return (x1 - eta_t * g1, x2 - eta_t * g2, 0, 0)
```

```{.python .input}
#@tab tensorflow
def sgd(x1, x2, s1, s2, f_grad):
Expand Down

0 comments on commit 4f04e54

Please sign in to comment.