1
1
"""An example focused on training a network to denoise a time series."""
2
2
3
+ from typing import Dict
3
4
4
5
import matplotlib .pyplot as plt
5
6
import torch as th
7
+ from torch .func import grad_and_value
8
+ from tqdm import tqdm
6
9
7
10
8
11
def sigmoid (x : th .Tensor ) -> th .Tensor :
@@ -18,36 +21,18 @@ def sigmoid(x: th.Tensor) -> th.Tensor:
18
21
return 0.
19
22
20
23
21
- class Net ( th .nn . Module ) :
22
- """Decosine Network."""
24
+ def net ( params : Dict , x : th .Tensor ) -> th . Tensor :
25
+ """Set up a single layer network.
23
26
24
- def __init__ (
25
- self , input_neurons : int , output_neurons : int , hidden_neurons : int
26
- ) -> None :
27
- """Initialize the network.
28
-
29
- Args:
30
- input_neurons (int): Number of input neurons.
31
- output_neurons (int): Number of output neurons.
32
- hidden_neurons (int): Number of hidden neurons.
33
- """
34
- super ().__init__ ()
35
- # TODO: Create two layers using th.nn.Linear.
36
-
37
-
38
- def forward (self , x : th .Tensor ) -> th .Tensor :
39
- """Network forward pass.
40
-
41
- Args:
42
- x (th.Tensor): Input tensor of shape 1x200.
27
+ Args:
28
+ params (Dict): Dictionary containing W1, b, and W2.
29
+ x (th.Tensor): Network input.
43
30
44
- Returns:
45
- th.Tensor: Network prediction of shape 1x200.
46
- """
47
- # TODO: Implment the forward pass using our sigmoid function
48
- # as well as the layers you created in the __init__ function.
49
- # return the network output instead of 0.
50
- return 0.
31
+ Returns:
32
+ th.Tensor: Network prediction.
33
+ """
34
+ # TODO: Implement single layer pass.
35
+ return None
51
36
52
37
53
38
def cost (y : th .Tensor , h : th .Tensor ) -> th .Tensor :
@@ -60,79 +45,56 @@ def cost(y: th.Tensor, h: th.Tensor) -> th.Tensor:
60
45
Returns:
61
46
th.Tensor: Squared Error.
62
47
"""
63
- # TODO: Return squared error cost instead of 0 .
48
+ # TODO: Implement Squared Error loss .
64
49
return 0.
65
50
66
51
67
- def sgd ( model : Net , step_size : float ) -> Net :
68
- """Perform Stochastic Gradient Descent .
52
+ def net_cost ( params : Dict , x : th . Tensor , y : th . Tensor ) -> th . Tensor :
53
+ """Evaluate the network and compute the loss .
69
54
70
55
Args:
71
- model (Net): Network object.
72
- step_size (float): Step size for SGD.
56
+ params (Dict): Dictionary containing W1, b, and W2.
57
+ x (th.Tensor): Network input.
58
+ y (th.Tensor): Squared error loss.
73
59
74
60
Returns:
75
- Net: SGD applied model.
76
- """
77
- for param in model .parameters ():
78
- # TODO: compute an update for every parameter using param.data,
79
- # step size as well as param.grad.data
80
- pass
81
- return model
82
-
83
-
84
- def zero_grad (model : Net ) -> Net :
85
- """Make gradients zero after SGD.
86
-
87
- Args:
88
- model (Net): Network object.
89
-
90
- Returns:
91
- Net: Network with zeroed gradients.
61
+ th.Tensor: Squared Error.
92
62
"""
93
- for param in model .parameters ():
94
- # TODO: call zero_() for every parameter.
95
- pass
96
- return model
63
+ # TODO: Call network, compute and return the loss.
64
+ return None
97
65
98
66
99
67
if __name__ == "__main__" :
100
- # TODO: Use th.manual_seed to set the seed for the network initialization.
68
+ # TODO: Use th.manual_seed as 42 to set the seed for the network initialization
101
69
pass
102
- # TODO: Choose a step size.
103
- step_size = 0.00
104
- # TODO: Chose a suitable amount of iterations.
105
- iterations = 100
70
+ # TODO: Choose a suitable stepsize
71
+ step_size = 0.0
72
+ iterations = 150
106
73
input_neurons = output_neurons = 200
107
- # TODO: Choose a network size.
74
+ # TODO: Choose a proper network size.
108
75
hidden_neurons = 0
109
76
110
77
x = th .linspace (- 3 * th .pi , 3 * th .pi , 200 )
111
78
y = th .cos (x )
112
79
113
- # TODO: Instatiate our network using the `Net`-constructor.
114
- model = None
80
+ # TODO: Initialize the parameters
81
+ W1 = None
82
+ b = None
83
+ W2 = None
115
84
116
- for i in range (iterations ):
85
+ # TODO: Instantiate grad_and_value function
86
+ value_grad = None
87
+
88
+ for i in (pbar := tqdm (range (iterations ))):
117
89
th .manual_seed (i )
118
90
y_noise = y + th .randn ([200 ])
119
91
120
- # TODO: Compute the network output using your model.
121
- preds = 0.
122
-
123
- # TODO: Compute the loss value using your cost function.
124
- loss_val = 0.
92
+ # TODO: Compute loss and gradients
125
93
126
- #TODO: Compute the gradient by calling the backward() function of your loss Tensor.
127
- pass
94
+ # TODO: Update parameters using SGD
128
95
129
- # TODO: Use your sgd function to update your model.
130
- model = None
131
- # TODO: Use your zero grad function to reset your gradients
132
- model = None
133
- print (f"Iteration: { i } , Cost: { loss_val .item ()} " )
134
-
135
- y_hat = model (y_noise ).detach ().numpy ()
96
+ # TODO: Compute test y_hat using y_noise and converged parameters
97
+ y_hat = None
136
98
137
99
plt .title ("Denoising a cosine" )
138
100
plt .plot (x , y , label = "solution" )
@@ -143,26 +105,3 @@ def zero_grad(model: Net) -> Net:
143
105
plt .savefig ("./figures/Denoise.png" , dpi = 600 , bbox_inches = "tight" )
144
106
plt .show ()
145
107
print ("Done" )
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
- if __name__ == "__main__" :
154
- step_size = 0.01
155
- iterations = 100
156
- hidden_neurons = 10
157
-
158
- # generate cosine signal
159
- x = jnp .linspace (- 3 * jnp .pi , 3 * jnp .pi , 200 )
160
- y = jnp .cos (x )
161
-
162
- # TODO: Create W1, W2 and b using different random keys
163
-
164
- for i in range (iterations ):
165
- # add noise to cosine
166
- y_noise = y + jax .random .normal (jax .random .PRNGKey (i ), [200 ])
167
-
168
- # TODO: Implement a dense neural network to denoise the cosine.
0 commit comments