<jupyter_start><jupyter_text>Make Random Circle<jupyter_code>def MakeCircle(center,radius):
    c=torch.zeros(100,2)
    i=0
    while i<100:
        point=torch.randint(-10,10,(1,2)).squeeze().float()+torch.rand(2)
        
        distance=(point[0]-center[0])**2+(point[1]-center[1])**2
        if distance<radius:
            c[i]=point
            i+=1
    return c
    

c1=MakeCircle([0,0],3)
c2=MakeCircle([3,3],3)

plt.scatter(np.array([x[0] for x in c1]),np.array([x[1] for x in c1]))
plt.scatter(np.array([x[0] for x in c2]),np.array([x[1] for x in c2]))
plt.show()<jupyter_output><empty_output><jupyter_text>Binary Cross Entropy<jupyter_code>x_train=torch.cat((c1,c2))
y_train=torch.cat((torch.zeros(100,1),torch.ones(100,1)))

layers=[nn.Linear(2,32,bias=True),
        nn.Sigmoid(),
        nn.Linear(32,32,bias=True),
        nn.Sigmoid(),
        nn.Linear(32,1,bias=True),
        nn.Sigmoid()
       ]

model=nn.Sequential(*layers)

optimizer=optim.Adam(model.parameters(),lr=0.001)
criterion=nn.BCELoss()

XX=np.arange(-6.5,7.8,1.3)
YY=np.arange(-6.5,7.8,1.3)
ZZ=np.zeros((11,11))

XXX,YYY=np.meshgrid(XX,YY)
for epoch in range(1001):
    
    hypothesis=model(x_train)
    
    optimizer.zero_grad()
    
    cost=criterion(hypothesis,y_train)
    cost.backward()
    
    optimizer.step()
    
    if epoch % 100 == 0:
        print('Epoch {}, cost = {:.9f}'.format(epoch,cost.item()))
        
        with torch.no_grad():
            for X_ in range(11):
                for Y_ in range(11):
                    predict=model(torch.FloatTensor([XXX[X_,Y_],YYY[X_,Y_]]))
                    ZZ[X_,Y_]=torch.Tensor.numpy(predict)

            plt.contour(XX,YY,ZZ,colors='black',linestyles='dashed')
            plt.contourf(XX,YY,ZZ)
            plt.scatter(np.array([x[0] for x in c1]),np.array([x[1] for x in c1]))
            plt.scatter(np.array([x[0] for x in c2]),np.array([x[1] for x in c2]))
            plt.show()<jupyter_output>Epoch 0, cost = 0.721215725<jupyter_text>Multi Class Classification<jupyter_code>c1=MakeCircle([0,0],3)
c2=MakeCircle([3,3],3)
c3=MakeCircle([-3,3],3)

plt.scatter(np.array([x[0] for x in c1]),np.array([x[1] for x in c1]))
plt.scatter(np.array([x[0] for x in c2]),np.array([x[1] for x in c2]))
plt.scatter(np.array([x[0] for x in c3]),np.array([x[1] for x in c3]))
plt.show()

x_train=torch.cat((c1,c2,c3))
y_train=torch.cat((torch.LongTensor([[0]]*100),torch.LongTensor([[1]]*100),torch.LongTensor([[2]]*100))).squeeze()

layers=[nn.Linear(2,16,bias=True),
        nn.Sigmoid(),
        nn.Linear(16,16,bias=True),
        nn.Sigmoid(),
        nn.Linear(16,3,bias=True),
        nn.Sigmoid()
       ]
model=nn.Sequential(*layers)

optimizer=optim.Adam(model.parameters(),lr=0.001)
criterion=nn.CrossEntropyLoss()

XX=np.arange(-6.5,7.8,1.3)
YY=np.arange(-6.5,7.8,1.3)
ZZ=np.zeros((11,11))

XXX,YYY=np.meshgrid(XX,YY)

for epoch in range(1001):
    
    hypothesis=model(x_train)
    
    optimizer.zero_grad()
    cost=criterion(hypothesis,y_train)
    cost.backward()
    
    optimizer.step()
    
    if epoch % 100 == 0:
        print('Epoch {}, cost = {:.9f}'.format(epoch,cost.item()))
        
        with torch.no_grad():
            for X_ in range(11):
                for Y_ in range(11):
                    predict=model(torch.FloatTensor([XXX[X_,Y_],YYY[X_,Y_]]))
                    ZZ[X_,Y_]=torch.Tensor.numpy(predict.max(dim=0)[1])

            plt.contour(XX,YY,ZZ,colors='black',linestyles='dashed')
            plt.contourf(XX,YY,ZZ)
            plt.scatter(np.array([x[0] for x in c1]),np.array([x[1] for x in c1]))
            plt.scatter(np.array([x[0] for x in c2]),np.array([x[1] for x in c2]))
            plt.scatter(np.array([x[0] for x in c3]),np.array([x[1] for x in c3]))
            plt.show()<jupyter_output>Epoch 0, cost = 1.103102207
{'fraction_non_alphanumeric': 0.1946256196190973, 'fraction_numerical': 0.05661361857552831, 'mean_word_length': 3.0357894736842104, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '279691', 'license': 'MIT', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1587, 'n_words': 213, 'path': '03_Pytorch_Classification.ipynb', 'repo_name': 'wow1548/Pytorch-Notebook', 'size': '3833'}