import minitorch import datasets import time import matplotlib.pyplot as plt PTS = 50 DATASET = datasets.Xor(PTS, vis=True) HIDDEN = 10 RATE = 0.5 def RParam(*shape): r = 2 * (minitorch.rand(shape) - 0.5) return minitorch.Parameter(r) class Network(minitorch.Module): def __init__(self): super().__init__() # Submodules self.layer1 = Linear(2, HIDDEN) self.layer2 = Linear(HIDDEN, HIDDEN) self.layer3 = Linear(HIDDEN, 1) def forward(self, x): # TODO: Implement for Task 2.5. out = self.layer1.forward(x).relu() out = self.layer2.forward(out).relu() return self.layer3.forward(out).sigmoid()
import torch import datasets import matplotlib.pyplot as plt PTS = 250 DATASET = datasets.Xor(PTS) HIDDEN = 10 RATE = 0.5 # Model with class Network(torch.nn.Module): def __init__(self): super().__init__() # Submodules self.layer1 = Linear(2, HIDDEN) self.layer2 = Linear(HIDDEN, HIDDEN) self.layer3 = Linear(HIDDEN, 1) def forward(self, x): h = self.layer1.forward(x).relu() h = self.layer2.forward(h).relu() return self.layer3.forward(h).sigmoid() class Linear(torch.nn.Module): def __init__(self, in_size, out_size): super().__init__() self.weight = torch.nn.Parameter(2 * (torch.rand(in_size, out_size) - 0.5))