Tuesday, November 19, 2019

Opti 2


In [0]:
import numpy as np
import matplotlib.pyplot as plt
import torch
In [0]:
import torch.nn as nn
In [0]:
class LogisticRegression(nn.Module):
    def __init__(self):
        super().__init__()
        self.l1 = nn.Linear(8,6)
        self.l2 = nn.Linear(6,1)
    def forward(self,x):
        x = torch.relu(self.l1(x))
        x = torch.sigmoid(self.l2(x))
        return x
In [0]:
import pandas as pd
In [0]:
df = pd.read_csv('diabetes.csv.gz', compression='gzip', header=None, dtype=np.float32)
In [131]:
df
Out[131]:
0 1 2 3 4 5 6 7 8
0 -0.294118 0.487437 0.180328 -0.292929 0.000000 0.001490 -0.531170 -0.033333 0.0
1 -0.882353 -0.145729 0.081967 -0.414141 0.000000 -0.207153 -0.766866 -0.666667 1.0
2 -0.058823 0.839196 0.049180 0.000000 0.000000 -0.305514 -0.492741 -0.633333 0.0
3 -0.882353 -0.105528 0.081967 -0.535354 -0.777778 -0.162444 -0.923997 0.000000 1.0
4 0.000000 0.376884 -0.344262 -0.292929 -0.602837 0.284650 0.887276 -0.600000 0.0
... ... ... ... ... ... ... ... ... ...
754 0.176471 0.015075 0.245902 -0.030303 -0.574468 -0.019374 -0.920581 0.400000 1.0
755 -0.764706 0.226131 0.147541 -0.454545 0.000000 0.096870 -0.776260 -0.800000 1.0
756 -0.411765 0.216080 0.180328 -0.535354 -0.735225 -0.219076 -0.857387 -0.700000 1.0
757 -0.882353 0.266332 -0.016393 0.000000 0.000000 -0.102832 -0.768574 -0.133333 0.0
758 -0.882353 -0.065327 0.147541 -0.373737 0.000000 -0.093890 -0.797609 -0.933333 1.0
759 rows × 9 columns
In [0]:
from sklearn.model_selection import train_test_split
In [0]:
X_train, X_test, y_train, y_test = train_test_split(df.iloc[:,:8], df.iloc[:,8], random_state=8)
In [148]:
X_train.shape, X_test.shape
Out[148]:
((569, 8), (190, 8))
In [0]:
from sklearn.preprocessing import StandardScaler
In [0]:
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
In [0]:
X_train = torch.from_numpy(X_train).float()
X_test = torch.from_numpy(X_test).float()
y_train = torch.from_numpy(y_train.values)
y_test = torch.from_numpy(y_test.values)
In [0]:
def accuracy(truth,preds):
    return (truth==preds).float().mean()

SGD

In [0]:
model = LogisticRegression()
criterion = nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.001)
In [154]:
for epoch in range(501):
    pred = model.forward(X_train)
    loss = criterion(pred, y_train)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    with torch.no_grad():
        test_preds = model.forward(X_test)
        test_preds = test_preds>0.5
        acc = accuracy(y_test, test_preds.view(y_test.shape))
        if epoch%50==0:print(epoch, acc)
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py:498: UserWarning: Using a target size (torch.Size([569])) that is different to the input size (torch.Size([569, 1])) is deprecated. Please ensure they have the same size.
  return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
0 tensor(0.6421)
50 tensor(0.6368)
100 tensor(0.6368)
150 tensor(0.6421)
200 tensor(0.6474)
250 tensor(0.6474)
300 tensor(0.6526)
350 tensor(0.6579)
400 tensor(0.6737)
450 tensor(0.6842)
500 tensor(0.6842)
In [0]:
model = LogisticRegression()
criterion = nn.BCELoss()
optimizer = torch.optim.RMSprop(model.parameters(), lr = 0.001)
In [156]:
for epoch in range(501):
    pred = model.forward(X_train)
    loss = criterion(pred, y_train)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    with torch.no_grad():
        test_preds = model.forward(X_test)
        test_preds = test_preds>0.5
        acc = accuracy(y_test, test_preds.view(y_test.shape))
        if epoch%50==0:print(epoch, acc)
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py:498: UserWarning: Using a target size (torch.Size([569])) that is different to the input size (torch.Size([569, 1])) is deprecated. Please ensure they have the same size.
  return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
0 tensor(0.5368)
50 tensor(0.7526)
100 tensor(0.8000)
150 tensor(0.7895)
200 tensor(0.7842)
250 tensor(0.7737)
300 tensor(0.7684)
350 tensor(0.7684)
400 tensor(0.7737)
450 tensor(0.7737)
500 tensor(0.7895)
In [0]:
model = LogisticRegression()
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.001)
In [158]:
for epoch in range(501):
    pred = model.forward(X_train)
    loss = criterion(pred, y_train)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    with torch.no_grad():
        test_preds = model.forward(X_test)
        test_preds = test_preds>0.5
        acc = accuracy(y_test, test_preds.view(y_test.shape))
        if epoch%50==0:print(epoch, acc)
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py:498: UserWarning: Using a target size (torch.Size([569])) that is different to the input size (torch.Size([569, 1])) is deprecated. Please ensure they have the same size.
  return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
0 tensor(0.6158)
50 tensor(0.6842)
100 tensor(0.6842)
150 tensor(0.6842)
200 tensor(0.7211)
250 tensor(0.7632)
300 tensor(0.7789)
350 tensor(0.7842)
400 tensor(0.7842)
450 tensor(0.7789)
500 tensor(0.7842)
In [0]:
 
In [0]:
 
In [0]:
 
In [0]:
arr = np.arange(16).reshape((2,2,4))
In [119]:
arr
Out[119]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])
In [120]:
arr.shape
Out[120]:
(2, 2, 4)
In [121]:
arr[0]
Out[121]:
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
In [122]:
arr[1]
Out[122]:
array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])
In [124]:
arr.transpose(1,0,2).shape
Out[124]:
(2, 2, 4)
In [125]:
arr.transpose(1,0,2)
Out[125]:
array([[[ 0,  1,  2,  3],
        [ 8,  9, 10, 11]],

       [[ 4,  5,  6,  7],
        [12, 13, 14, 15]]])
In [0]:
 

No comments :

Post a Comment