In [2]:
X, y = make_circles(n_samples=500, noise=0.1, factor = 0.2)
In [3]:
def scatter_plot():
plt.scatter(X[y==0][:,0], X[y==0][:,1], label = "Class 0")
plt.scatter(X[y==1][:,0], X[y==1][:,1], label = "Class 1")
plt.legend()
scatter_plot()
In [4]:
X_data = torch.Tensor(X)
y_data = torch.Tensor(y.reshape(500,1))
Data is ready
In [5]:
class LR(nn.Module):
def __init__(self,inp, H1, op):
super().__init__()
self.linear = nn.Linear(inp, H1)
self.linear2 = nn.Linear(H1,op)
def forward(self,x):
x = torch.sigmoid(self.linear(x))
x = torch.sigmoid(self.linear2(x))
return x
Experiment 1: Two neurons in the hidden layer
In [6]:
torch.manual_seed(2)
model = LR(2,2,1)
In [7]:
list(model.parameters())
Out[7]:
In [8]:
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.05)
In [9]:
losses = []
for epoch in range(1000):
y_pred = model.forward(X_data)
loss = criterion(y_pred, y_data)
losses.append(loss.item())
loss.backward()
optimizer.step()
optimizer.zero_grad()
In [10]:
plt.plot(range(1000),losses)
Out[10]:
In [11]:
list(model.parameters())
Out[11]:
In [12]:
optimizer.state
Out[12]:
In [13]:
optimizer.defaults
Out[13]:
In [14]:
optimizer.defaults
Out[14]:
In [15]:
optimizer.param_groups
Out[15]:
Testing
In [16]:
def plot_decision_boundary(X, y):
x_span = np.linspace(min(X[:, 0]) -0.25, max(X[:, 0])+0.25)
y_span = np.linspace(min(X[:, 1]) -0.25, max(X[:, 1])+0.25)
xx, yy = np.meshgrid(x_span, y_span)
grid = torch.Tensor(np.c_[xx.ravel(), yy.ravel()])
pred_func = model.forward(grid)
z = pred_func.view(xx.shape).detach().numpy()
plt.contourf(xx, yy, z)
In [17]:
plot_decision_boundary(X, y)
scatter_plot()
In [18]:
x = 0.025
y = 0.025
point = torch.Tensor([x, y])
prediction = model.forward(point)
prediction = 1 if prediction >0.5 else 0
plt.plot([x], [y], marker='o', markersize=10, color="red")
print("Prediction is", prediction)
plot_decision_boundary(X, y)
In [19]:
x = -1
y = 1
point = torch.Tensor([x, y])
prediction = model.forward(point)
prediction = 1 if prediction >0.5 else 0
plt.plot([x], [y], marker='o', markersize=10, color="red")
print("Prediction is", prediction)
plot_decision_boundary(X, y)
Two neurons in the hidden layer is not enough to classify our datasets. By seeing the data, we need at least three neurons(triangle) to classify our data correctly. Four neurons will form a square shapes to classify the points correctly
Three neurons in the hidden layer¶
In [20]:
X, y = make_circles(n_samples=500, noise=0.1, factor = 0.2)
X_data = torch.Tensor(X)
y_data = torch.Tensor(y.reshape(500,1))
In [21]:
torch.manual_seed(2)
model = LR(2,3,1)
In [22]:
list(model.parameters())
Out[22]:
In [23]:
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.05)
In [24]:
losses = []
for epoch in range(1000):
y_pred = model.forward(X_data)
loss = criterion(y_pred, y_data)
losses.append(loss.item())
loss.backward()
optimizer.step()
optimizer.zero_grad()
In [25]:
plt.plot(range(1000),losses)
Out[25]:
In [26]:
list(model.parameters())
Out[26]:
In [ ]:
In [27]:
def plot_decision_boundary(X, y):
x_span = np.linspace(min(X[:, 0]) -0.25, max(X[:, 0])+0.25)
y_span = np.linspace(min(X[:, 1]) -0.25, max(X[:, 1])+0.25)
xx, yy = np.meshgrid(x_span, y_span)
grid = torch.Tensor(np.c_[xx.ravel(), yy.ravel()])
pred_func = model.forward(grid)
z = pred_func.view(xx.shape).detach().numpy()
plt.contourf(xx, yy, z)
In [28]:
plot_decision_boundary(X, y)
scatter_plot()
Four units in the hidden layer¶
In [29]:
X, y = make_circles(n_samples=500, noise=0.1, factor = 0.2)
X_data = torch.Tensor(X)
y_data = torch.Tensor(y.reshape(500,1))
In [30]:
torch.manual_seed(2)
model = LR(2,4,1)
In [31]:
list(model.parameters())
Out[31]:
In [32]:
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.05)
In [33]:
losses = []
for epoch in range(1000):
y_pred = model.forward(X_data)
loss = criterion(y_pred, y_data)
losses.append(loss.item())
loss.backward()
optimizer.step()
optimizer.zero_grad()
In [34]:
plt.plot(range(1000),losses)
Out[34]:
In [35]:
list(model.parameters())
Out[35]:
In [36]:
def plot_decision_boundary(X, y):
x_span = np.linspace(min(X[:, 0]) -0.25, max(X[:, 0])+0.25)
y_span = np.linspace(min(X[:, 1]) -0.25, max(X[:, 1])+0.25)
xx, yy = np.meshgrid(x_span, y_span)
grid = torch.Tensor(np.c_[xx.ravel(), yy.ravel()])
pred_func = model.forward(grid)
z = pred_func.view(xx.shape).detach().numpy()
plt.contourf(xx, yy, z)
In [37]:
plot_decision_boundary(X, y)
scatter_plot()
More units in the hidden layer¶
Let's increase the hidden unit to 10. It should form circular shapes that fits our training data very well (overfitting)
In [38]:
X, y = make_circles(n_samples=500, noise=0.1, factor = 0.2)
X_data = torch.Tensor(X)
y_data = torch.Tensor(y.reshape(500,1))
In [39]:
torch.manual_seed(2)
model = LR(2,10,1)
In [40]:
list(model.parameters())
Out[40]:
In [41]:
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.05)
In [42]:
losses = []
for epoch in range(1000):
y_pred = model.forward(X_data)
loss = criterion(y_pred, y_data)
losses.append(loss.item())
loss.backward()
optimizer.step()
optimizer.zero_grad()
In [43]:
plt.plot(range(1000),losses)
Out[43]:
In [44]:
list(model.parameters())
Out[44]:
In [45]:
def plot_decision_boundary(X, y):
x_span = np.linspace(min(X[:, 0]) -0.25, max(X[:, 0])+0.25)
y_span = np.linspace(min(X[:, 1]) -0.25, max(X[:, 1])+0.25)
xx, yy = np.meshgrid(x_span, y_span)
grid = torch.Tensor(np.c_[xx.ravel(), yy.ravel()])
pred_func = model.forward(grid)
z = pred_func.view(xx.shape).detach().numpy()
plt.contourf(xx, yy, z)
In [46]:
plot_decision_boundary(X, y)
scatter_plot()
No comments :
Post a Comment