Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 15th, 2018 81 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. import strformat
  2. import torch
  3. import torch/[nn, optim]
  4.  
  5. let inputs = torch.tensor([
  6.   [0.0, 0.0],
  7.   [0.0, 1.0],
  8.   [1.0, 0.0],
  9.   [1.0, 1.0],
  10. ])
  11.  
  12. let targets = torch.tensor([
  13.   [0.0],
  14.   [1.0],
  15.   [1.0],
  16.   [0.0],
  17. ])
  18.  
  19. proc xorTraining() {.exportc.} =
  20.   let
  21.     fc1 = nn.Linear(2, 4)
  22.     fc2 = nn.Linear(4, 1)
  23.     loss_fn = nn.MSELoss()
  24.     optimizer = optim.SGD(fc1.parameters & fc2.parameters , lr = 0.01, momentum = 0.1)
  25.  
  26.   for i in 0 ..< 50000:
  27.     optimizer.zero_grad()
  28.  
  29.     let predictions = inputs.fc1.relu.fc2.sigmoid
  30.  
  31.     let loss = loss_fn(predictions, targets)
  32.     loss.backward()
  33.     optimizer.step()
  34.  
  35.     if i mod 5000 == 0:
  36.       echo fmt"Episode {i}, Loss: {loss.toFloat32()}"
  37.  
  38.   echo ""
  39.   echo "Layer 1:"
  40.   echo fc1.weight
  41.   echo ""
  42.   echo "Layer 2:"
  43.   echo fc2.weight
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top