# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="B6Zcl1xDtKY3" colab_type="code" colab={}
# This notebook implements a neural net to apprximate XOR function using PyTorch
from __future__ import print_function
import numpy as np
import torch
from torch.autograd import Variable

# + id="gfA-FfFG1792" colab_type="code" outputId="a412e635-f39d-4495-a574-e5a0d01fba46" executionInfo={"status": "ok", "timestamp": 1562991254444, "user_tz": 360, "elapsed": 1564, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13266317426275772701"}} colab={"base_uri": "https://localhost:8080/", "height": 153}
X = torch.tensor([[1.0,0.0,0.0,1.0],[0.0,0.0,1.0,1.0]],dtype=torch.float32) # 2x4 matrix
X = torch.transpose(X,0,1)
Y = torch.tensor([[1.0,0.0,1.0,0.0]],dtype=torch.float32)                   # 1x4 vector
Y = torch.transpose(Y,0,1)
print("input: ", X)
print("output: ", Y)


# + id="gHOSWcDL1_vh" colab_type="code" outputId="4f279a0c-1af4-4e19-f9bb-bf4d6d8bf65b" executionInfo={"status": "ok", "timestamp": 1562991489203, "user_tz": 360, "elapsed": 4630, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "13266317426275772701"}} colab={"base_uri": "https://localhost:8080/", "height": 323}
# parameters of neural net
W1 = Variable(torch.torch.FloatTensor(2, 8).uniform_(-1, 1), requires_grad=True) # 2x8 matrix
b1 = Variable(torch.zeros((1,8)), requires_grad=True)                            # 1x8 matrix
W2 = Variable(torch.torch.FloatTensor(8, 1).uniform_(-1, 1), requires_grad=True) # 8x1 matrix
b2 = Variable(torch.zeros([1]), requires_grad=True)                              # scalar

learning_rate = 0.05
optimizer = torch.optim.SGD([W1, b1, W2, b2], lr=learning_rate, momentum=0.9)    # Torch optimizer

loss_fn = torch.nn.MSELoss() # Eclidean loss function

for step in range(10000):

  # forward pass
  Z1 = torch.mm(X,W1)    # 4x8 matrix
  Z2 = Z1 + b1           # 4x8 matrix
  Z3 = torch.sigmoid(Z2) # 4x8 matrix
  Z4 = torch.mm(Z3,W2)   # 4x1 vector
  Z5 = Z4 + b2           # 4x1 vector
  Yp = torch.sigmoid(Z5) # 4x1 vector

  # backward pass
  optimizer.zero_grad()  # zero out previous gradients
  loss = loss_fn(Yp,Y)   # compute loss
  loss.backward()        # calculate gradients
  #Yp.backward(Yp-Y)     # or, apply gradient of loss at Yp!
  #Z5.backward(Yp*(1.0-Yp)*(Yp-Y)) # or, apply gradient of Yp at Z5!
  optimizer.step()       # apply new gradients

  if step%1000 == 0: 
    print("loss:",loss.item())

print(Yp)
print(Y)

StarcoderdataJupyterScriptsDedupFiltered