<jupyter_start><jupyter_text><jupyter_code>!pip3 install torch
import torch
import numpy as np
import matplotlib.pyplot as plt
import torch.nn as nn
from sklearn import datasets
  n_pts = 500
  X, y = datasets.make_circles(n_samples=n_pts, random_state=123, noise=0.1, factor=0.2)
  
  x_data = torch.Tensor(X)
  y_data = torch.Tensor(y.reshape(500, 1))
  print(y.shape)<jupyter_output>(500,)<jupyter_text><jupyter_code>def scatter_plot():
  plt.scatter(X[y==0, 0], X[y==0, 1])
  plt.scatter(X[y==1, 0], X[y==1, 1])
scatter_plot()
class Model(nn.Module):
    def __init__(self, input_size, H1, output_size):
      super().__init__() 
      self.linear = nn.Linear(input_size, H1)
      self.linear2 = nn.Linear(H1, output_size)
    def forward(self, x):
      x = torch.sigmoid(self.linear(x))
      x = torch.sigmoid(self.linear2(x))
      return x
    def predict(self, x):
      pred = self.forward(x)
      if pred >= 0.5:
        return 1
      else:
        return 0
torch.manual_seed(2)
model = Model(2, 4, 1)
print(list(model.parameters()))
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
epochs = 1000
losses = []

for i in range(epochs):
  y_pred = model.forward(x_data)
  loss = criterion(y_pred, y_data)
  print("epoch:", i, "loss:", loss.item())
  losses.append(loss.item())
  optimizer.zero_grad()
  loss.backward()
  optimizer.step()
plt.plot(range(epochs), losses)
plt.ylabel('Loss')
plt.xlabel('epoch')
plt.grid()
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)
plot_decision_boundary(X,y)
scatter_plot()
x = 0.25
y = 0.25
point = torch.Tensor ([x, y])
prediction = model.predict(point)
plt.plot([x], [y], marker='o', markersize=10, color="red")
print("Prediction is" , prediction)
plot_decision_boundary(X,y)
point1 = torch.Tensor([1.0, -1.0])
point2 = torch.Tensor([-1.0, 1.0])
plt.plot(point1.numpy()[0], point1.numpy()[1], 'ro')
plt.plot(point2.numpy()[0], point2.numpy()[1], 'ko')
plot_fit("Trained Model")
print("Red point positive probability = {}".format(model.forward(point1).item())) 
print("Black point positive probability = {}".format(model.forward(point2).item())) 
print("Red point belongs in class {}".format(model.predict(point1))) 
print("Black point belongs in class = {}".format(model.predict(point2)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18484500574052812, 'fraction_numerical': 0.03482587064676617, 'mean_word_length': 4.707423580786026, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '5313', 'license': 'MIT', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1033, 'n_words': 203, 'path': 'deep_neaural_network.ipynb', 'repo_name': 'cervantes-loves-ai/100-Days-Of-ML-Code', 'size': '2613'}