1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
from numpy import vstack from pandas import read_csv from sklearn.preprocessing import LabelEncoder from sklearn.metrics import accuracy_score from torch import Tensor from torch.optim import SGD from torch.utils.data import Dataset, DataLoader, random_split from torch.nn import Linear, ReLU, Sigmoid, Module, BCELoss from torch.nn.init import kaiming_uniform_, xavier_uniform_
class CSVDataset(Dataset): def __init__(self, path): df = read_csv(path, header=None) self.X = df.values[:, :-1] self.y = df.values[:, -1] self.X = self.X.astype('float32') self.y = LabelEncoder().fit_transform(self.y) self.y = self.y.astype('float32') self.y = self.y.reshape((len(self.y), 1))
def __len__(self): return len(self.X)
def __getitem__(self, idx): return [self.X[idx], self.y[idx]]
def get_splits(self, n_test=0.3): test_size = round(n_test * len(self.X)) train_size = len(self.X) - test_size return random_split(self, [train_size, test_size])
class MLP(Module): def __init__(self, n_inputs): super(MLP, self).__init__() self.hidden1 = Linear(n_inputs, 10) kaiming_uniform_(self.hidden1.weight, nonlinearity='relu') self.act1 = ReLU() self.hidden2 = Linear(10, 8) kaiming_uniform_(self.hidden2.weight, nonlinearity='relu') self.act2 = ReLU() self.hidden3 = Linear(8, 1) xavier_uniform_(self.hidden3.weight) self.act3 = Sigmoid()
def forward(self, X): X = self.hidden1(X) X = self.act1(X) X = self.hidden2(X) X = self.act2(X) X = self.hidden3(X) X = self.act3(X) return X
def prepare_data(path): dataset = CSVDataset(path) train, test = dataset.get_splits() train_dl = DataLoader(train, batch_size=32, shuffle=True) test_dl = DataLoader(test, batch_size=1024, shuffle=False) return train_dl, test_dl
def train_model(train_dl, model): criterion = BCELoss() optimizer = SGD(model.parameters(), lr=0.01, momentum=0.9) for epoch in range(100): for i, (inputs, targets) in enumerate(train_dl): optimizer.zero_grad() yhat = model(inputs) loss = criterion(yhat, targets) loss.backward() print("epoch: {}, batch: {}, loss: {}".format(epoch, i, loss.data)) optimizer.step()
def evaluate_model(test_dl, model): predictions, actuals = [], [] for i, (inputs, targets) in enumerate(test_dl): yhat = model(inputs) yhat = yhat.detach().numpy() actual = targets.numpy() actual = actual.reshape((len(actual), 1)) yhat = yhat.round() predictions.append(yhat) actuals.append(actual) predictions, actuals = vstack(predictions), vstack(actuals) acc = accuracy_score(actuals, predictions) return acc
def predict(row, model): row = Tensor([row]) yhat = model(row) yhat = yhat.detach().numpy() return yhat
path = './data/ionosphere.csv' train_dl, test_dl = prepare_data(path) print(len(train_dl.dataset), len(test_dl.dataset))
model = MLP(34) print(model)
train_model(train_dl, model)
acc = evaluate_model(test_dl, model) print('Accuracy: %.3f' % acc)
row = [1, 0, 0.99539, -0.05889, 0.85243, 0.02306, 0.83398, -0.37708, 1, 0.03760, 0.85243, -0.17755, 0.59755, -0.44945, 0.60536, -0.38223, 0.84356, -0.38542, 0.58212, -0.32192, 0.56971, -0.29674, 0.36946, -0.47357, 0.56811, -0.51171, 0.41078, -0.46168, 0.21266, -0.34090, 0.42267, -0.54487, 0.18641, -0.45300] yhat = predict(row, model) print('Predicted: %.3f (class=%d)' % (yhat, yhat.round()))
|