Replies: 1 comment
-
can you try without random state/seed and send |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
from torch.autograd.grad_mode import inference_mode
import torch
from torch import nn
import sklearn
from sklearn.datasets import make_circles
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
n_samples = 1000
x,y = make_circles(n_samples,
noise=0.03,
random_state=42)
X = torch.from_numpy(x).type(torch.float)
Y = torch.from_numpy(y).type(torch.float)
x_train,x_test,y_train,y_test = train_test_split(X,
Y,
test_size=0.2,
random_state=42)
len(x_train),len(x_test),len(y_train),len(y_test)
class CirclemodX0(nn.Module):
def init(self):
super().init()
self.linear1=nn.Linear(in_features=2,out_features=10)
self.linear2=nn.Linear(in_features=10,out_features=10)
self.linear3=nn.Linear(in_features=10,out_features=10)
self.linear4=nn.Linear(in_features=10,out_features=10)
self.linear5=nn.Linear(in_features=10,out_features=1)
self.relu = nn.ReLU()
def forward(self,x):
return self.linear5(self.relu(self.linear4(self.relu(self.linear3(self.relu(self.linear2(self.relu(self.linear1(x)))))))))
model_vx= CirclemodX0()
model_vx.state_dict()
losfn = nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(params=model_vx.parameters(),
lr=0.001)
def accuracy_fn(y_true,y_pred):
correct = torch.eq(y_true,y_pred).sum().item()
acc = correct/len(y_pred)*100
return acc
torch.manual_seed(42)
epochs =5000
for epoch in range(epochs):
model_vx.train()
y_logits = model_vx(x_train).squeeze()
y_pred =torch.round(torch.sigmoid(y_logits))
loss = losfn(y_logits,y_train)
acc = accuracy_fn(y_true=y_train,
y_pred = y_pred)
optimizer.zero_grad()
loss.backward()
optimizer.step()
model_vx.eval()
with inference_mode():
test_logits = model_vx(x_test).squeeze()
test_pred = torch.round(torch.sigmoid(test_logits))
test_loss = losfn(test_logits,y_test)
test_acc = accuracy_fn(y_true=y_test,
y_pred = test_pred)
if epoch % 100==0:
print(f"Epoch {epoch} | Loss {loss:.5f} | Accuracy {acc:.2f}% | Test Loss {test_loss:.5f} | test accuracy {test_acc:.2f}%")
Beta Was this translation helpful? Give feedback.
All reactions