02 Neural Network Classification -tried using circle equation instead of ReLU but didn't work #1378
|
Hello guys, I am a beginner in ml and have a small doubt `#Neural Network classification #let's display our data #let's draw our data #let's turn our data into tensors #let's split our data using train_test_split function from sklearn.model_selection #let's make device agnostic code import torch def forward(self,x:torch.Tensor): model_0=classification().to(device) #let's enter data into our untrained model #let's setup loss function and optimizer #let's create an evaluation metric or function to evaluate the performance of our model #let's create our training and testing loop for epoch in range(epochs): loss = loss_fn(y_logits,y_train) #testing part #printing what's happening This is the notebook - |
Replies: 1 comment 1 reply
|
The circle equation is fine. The sign is backwards. In X, y = make_circles(1000, noise=0.03, random_state=42)
d = np.sqrt((X**2).sum(1))
d[y == 0].mean() # 0.998
d[y == 1].mean() # 0.801Your forward returns I ran your code as posted to be sure. After 500 epochs I get 19% test accuracy. Anything well below 50% is the giveaway that a binary model is inverted rather than just failing to learn. Flip the subtraction: def forward(self, x):
return self.rad**2 - ((x[:, 0] - self.h)**2 + (x[:, 1] - self.k)**2)That took me to 79%. It stalls there for a second reason that's easier to miss. The two radii are roughly 0.8 and 1.0, so class classification(nn.Module):
def __init__(self):
super().__init__()
self.h = nn.Parameter(torch.randn(1))
self.k = nn.Parameter(torch.randn(1))
self.rad = nn.Parameter(torch.randn(1))
self.scale = nn.Parameter(torch.tensor([1.0]))
def forward(self, x):
d2 = (x[:, 0] - self.h)**2 + (x[:, 1] - self.k)**2
return self.scale * (self.rad**2 - d2)
What I measured, same seed, 500 epochs each:
One caveat on the whole approach: this works because you already know the data is two rings, so you hardcoded the shape of the boundary and left the model three numbers to fit. The ReLU stack in the course is doing something different. It has no idea the data is circular and has to build that boundary out of straight pieces. Yours will beat it on |

The circle equation is fine. The sign is backwards.
In
make_circles, label 1 is the inner ring and label 0 is the outer one. Quick check:Your forward returns
d**2 - r**2, which gets larger the further a point sits from the center.BCEWithLogitsLossreads a larger logit as "more likely to be class 1", so you're telling it the outer ring is class 1. No value of(h, k, rad)can satisfy that, so gradient descent keeps pushing the boundary further into the wrong place. That's why accuracy falls off instead of climbing.I ran your code as posted to be sure. Af…