<jupyter_start><jupyter_text>**3 layer NN**<jupyter_code># 3-layer NN
n, d_in, d_out, h = 64, 100000, 1000, 10
d_out1 = 50
lr = 1e-12
epochs = 200

x = torch.randn((n,d_in), device=device)
y = torch.randn((n, d_out), device=device)

w1 = torch.randn((d_in, h), device=device, requires_grad=True)
w2 = torch.randn((h, d_out1), device=device, requires_grad=True)
w3 = torch.randn((d_out1, d_out), device=device, requires_grad=True)

error = []
for num in range(epochs):

  y1 = x.mm(w1)
  y2 = y1.clamp(min=0)
  y3 = y2.mm(w2)
  y4 = y3.clamp(min=0)
  y5 = y4.mm(w3)

  loss = (y5 - y).pow(2).sum()
  print(loss.item())
  error.append(loss.item())

  loss.backward()

  with torch.no_grad():
    w1-= lr*w1.grad
    w2-= lr*w2.grad
    w3-= lr*w3.grad

    w1.grad.zero_()
    w2.grad.zero_()
    w3.grad.zero_()

plt.plot(error)<jupyter_output>806626852864.0
768034275328.0
732217344000.0
698882981888.0
667770945536.0
638712414208.0
611528933376.0
586044407808.0
562110267392.0
539610906624.0
518435176448.0
498487328768.0
479668174848.0
461891993600.0
445059104768.0
429121372160.0
414020894720.0
399717531648.0
386145320960.0
373254193152.0
360980480000.0
349282467840.0
338130960384.0
327493091328.0
317341433856.0
307640958976.0
298364370944.0
289480638464.0
280976261120.0
272827580416.0
265015787520.0
257530691584.0
250355007488.0
243468943360.0
236854296576.0
230496845824.0
224380895232.0
218497417216.0
212838858752.0
207392948224.0
202151837696.0
197106352128.0
192247709696.0
187556741120.0
183030693888.0
178660900864.0
174441168896.0
170366517248.0
166430261248.0
162624077824.0
158943346688.0
155383971840.0
151938842624.0
148603387904.0
145372856320.0
142243086336.0
139209113600.0
136269635584.0
133418500096.0
130652250112.0
127969468416.0
125366861824.0
122840334336.0
120386494464.0
118004514816.0
115690184704.0
1134398423[...]<jupyter_text>**Custom nn.Modules**<jupyter_code>n, d_in, d_out, h = 64, 10000, 100, 30
lr = 1e-3
epochs = 400
device = torch.device('cpu')

model = torch.nn.Sequential(
        torch.nn.Linear(d_in, h),
        torch.nn.ReLU(),
        torch.nn.Linear(h, d_out))

loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr= lr)
    
x = torch.rand((n, d_in), device=device, requires_grad=True)
y = torch.rand((n, d_out), device=device, requires_grad=True)

error = []
# for p in model.parameters():
#   print(p)
for num in range(epochs):
  
  y_pred = model(x) #pass input to the object as argument
  
  loss = loss_fn(y_pred, y)
  print(loss.item())
  error.append(loss.item())

  optimizer.zero_grad() # optimizer zero_grad usuallu used
  # backpropagate
  loss.backward()

  # optimiser step updates
  optimizer.step()

plt.plot(error)
n, d_in, d_out, h = 64, 10000, 100, 60
epochs = 800
lr = 1e-4

x = torch.randn((n, d_in), device=device)
y = torch.randn((n, d_out), device=device)

class TwoLayerNet(torch.nn.Module):
  def __init__(self, d_in, d_out, h):

    super(TwoLayerNet, self).__init__()
    
    self.linear1 = torch.nn.Linear(d_in,h)
    self.linear2 = torch.nn.Linear(h,d_out)

  def forward(self, x):
      y = self.linear1(x).clamp(min=0)
      out = self.linear2(y)
      return out

mnew = TwoLayerNet(d_in, d_out, h)
optimizer = torch.optim.SGD(mnew.parameters(), lr=lr)
loss_fn = torch.nn.MSELoss(reduction='sum')
error = []

for num in range(epochs):
    h = mnew(x)
    loss = loss_fn(h, y)
    print(loss.item())
    error.append(loss.item())

    mnew.zero_grad()
    loss.backward()

    with torch.no_grad():
      for param in mnew.parameters():
        param.data -= lr*param.grad

plt.plot(error)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14022038567493114, 'fraction_numerical': 0.2650137741046832, 'mean_word_length': 4.165007112375533, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '596713', 'license': 'MIT', 'n_tokens_mistral': 2187, 'n_tokens_neox': 1660, 'n_words': 287, 'path': 'Basic_Exercises.ipynb', 'repo_name': 'esha-singh/Pytorch-basics', 'size': '3630'}