|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import numpy as np |
| 4 | +from sklearn import datasets |
| 5 | +from sklearn.preprocessing import StandardScaler |
| 6 | +from sklearn.model_selection import train_test_split |
| 7 | + |
| 8 | +# 0) Prepare data |
| 9 | +bc = datasets.load_breast_cancer() |
| 10 | +X, y = bc.data, bc.target |
| 11 | + |
| 12 | +n_samples, n_features = X.shape |
| 13 | + |
| 14 | +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234) |
| 15 | + |
| 16 | +# scale |
| 17 | +sc = StandardScaler() |
| 18 | +X_train = sc.fit_transform(X_train) |
| 19 | +X_test = sc.transform(X_test) |
| 20 | + |
| 21 | +X_train = torch.from_numpy(X_train.astype(np.float32)) |
| 22 | +X_test = torch.from_numpy(X_test.astype(np.float32)) |
| 23 | +y_train = torch.from_numpy(y_train.astype(np.float32)) |
| 24 | +y_test = torch.from_numpy(y_test.astype(np.float32)) |
| 25 | + |
| 26 | +y_train = y_train.view(y_train.shape[0], 1) |
| 27 | +y_test = y_test.view(y_test.shape[0], 1) |
| 28 | + |
| 29 | +# 1) Model |
| 30 | +# Linear model f = wx + b , sigmoid at the end |
| 31 | +class Model(nn.Module): |
| 32 | + def __init__(self, n_input_features): |
| 33 | + super(Model, self).__init__() |
| 34 | + self.linear = nn.Linear(n_input_features, 1) # One in and one out |
| 35 | + |
| 36 | + def forward(self, x): |
| 37 | + y_pred = torch.sigmoid(self.linear(x)) |
| 38 | + return y_pred |
| 39 | + |
| 40 | +model = Model(n_features) |
| 41 | + |
| 42 | +# 2) Loss and optimizer |
| 43 | +num_epochs = 100 |
| 44 | +learning_rate = 0.01 |
| 45 | +criterion = nn.BCELoss() |
| 46 | +optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) |
| 47 | + |
| 48 | +# 3) Training loop |
| 49 | +for epoch in range(num_epochs): |
| 50 | + # Forward pass and loss |
| 51 | + y_pred = model(X_train) |
| 52 | + loss = criterion(y_pred, y_train) |
| 53 | + |
| 54 | + # Backward pass and update |
| 55 | + loss.backward() |
| 56 | + optimizer.step() |
| 57 | + |
| 58 | + # zero grad before new step |
| 59 | + optimizer.zero_grad() |
| 60 | + |
| 61 | + if (epoch+1) % 10 == 0: |
| 62 | + print(f'epoch: {epoch+1}, loss = {loss.item():.4f}') |
| 63 | + |
| 64 | + |
| 65 | +with torch.no_grad(): |
| 66 | + y_predicted = model(X_test) |
| 67 | + y_predicted_cls = y_predicted.round() |
| 68 | + acc = y_predicted_cls.eq(y_test).sum() / float(y_test.shape[0]) |
| 69 | + print(f'accuracy: {acc.item():.4f}') |
0 commit comments