|
| 1 | +import warnings |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from sklearn.datasets import load_breast_cancer |
| 5 | +from sklearn.metrics import accuracy_score |
| 6 | +from sklearn.preprocessing import MinMaxScaler |
| 7 | + |
| 8 | +warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 9 | + |
| 10 | + |
| 11 | +def train_network(neurons, x_train, y_train, epochs): |
| 12 | + """ |
| 13 | + Code the backpropagation algorithm with the technique of regularization |
| 14 | + weight decay. |
| 15 | + The chosen network architecture consists of 3 layers |
| 16 | + (the input layer, the hidden layer and the output layer). |
| 17 | +
|
| 18 | + Explanation here (Available just in Spanish): |
| 19 | + https://drive.google.com/file/d/1QTEbRVgevfK8QJ30tWcEbaNbBaKnvGWv/view?usp=sharing |
| 20 | + """ |
| 21 | + mu = 0.2 |
| 22 | + lambda_ = 1e-4 |
| 23 | + factor_scale = 0.001 |
| 24 | + inputs = np.shape(x_train)[1] |
| 25 | + outputs = np.shape(y_train)[1] |
| 26 | + # initialization of weights and bias randomly in very small values |
| 27 | + rng = np.random.default_rng(seed=42) |
| 28 | + w_co = rng.random((int(inputs), int(neurons))) * factor_scale |
| 29 | + bias_co = rng.random((1, int(neurons))) * factor_scale |
| 30 | + w_cs = rng.random((int(neurons), int(outputs))) * factor_scale |
| 31 | + bias_cs = rng.random((1, int(outputs))) * factor_scale |
| 32 | + error = np.zeros(epochs) |
| 33 | + # iterative process |
| 34 | + k = 0 |
| 35 | + while k < epochs: |
| 36 | + y = np.zeros(np.shape(y_train)) |
| 37 | + for j in np.arange(0, len(x_train), 1): |
| 38 | + x = x_train[j] |
| 39 | + t = y_train[j] |
| 40 | + # forward step: calcul of aj, ak ,zj y zk |
| 41 | + aj = np.dot(x, w_co) + bias_co |
| 42 | + zj = relu(aj) |
| 43 | + ak = np.dot(zj, w_cs) + bias_cs |
| 44 | + zk = sigmoid(ak) |
| 45 | + y[j] = np.round(zk) |
| 46 | + |
| 47 | + # backward step: Error gradient estimation |
| 48 | + g2p = d_sigmoid(ak) # for the weights and bias of the output layer neuron |
| 49 | + d_w_cs = g2p * zj.T |
| 50 | + d_bias_cs = g2p * 1 |
| 51 | + grad_w_cs = (zk - t) * d_w_cs + lambda_ * w_cs |
| 52 | + grad_bias_cs = (zk - t) * d_bias_cs + lambda_ * bias_cs |
| 53 | + |
| 54 | + g1p = d_relu(aj) # for the weights and bias of occult layer neurons |
| 55 | + d_w_co = np.zeros(np.shape(w_co)) |
| 56 | + d_bias_co = np.zeros(np.shape(bias_co)) |
| 57 | + for i in np.arange(0, np.shape(d_w_co)[1], 1): |
| 58 | + d_w_co[:, i] = g2p * w_cs[i] * g1p.T[i] * x.T |
| 59 | + d_bias_co[0, i] = g2p * w_cs[i] * g1p.T[i] * 1 |
| 60 | + grad_w_co = (zk - t) * d_w_co + lambda_ * w_co |
| 61 | + grad_bias_co = (zk - t) * d_bias_co + lambda_ * bias_co |
| 62 | + |
| 63 | + # Weight and bias update with regularization weight decay |
| 64 | + w_cs = (1 - mu * lambda_) * w_cs - mu * grad_w_cs |
| 65 | + bias_cs = (1 - mu * lambda_) * bias_cs - mu * grad_bias_cs |
| 66 | + w_co = (1 - mu * lambda_) * w_co - mu * grad_w_co |
| 67 | + bias_co = (1 - mu * lambda_) * bias_co - mu * grad_bias_co |
| 68 | + error[k] = 0.5 * np.sum((y - y_train) ** 2) |
| 69 | + k += 1 |
| 70 | + return w_co, bias_co, w_cs, bias_cs, error |
| 71 | + |
| 72 | + |
| 73 | +def relu(x): |
| 74 | + """ |
| 75 | + Relu activation function |
| 76 | + Hidden Layer due to it is less susceptible to vanish gradient |
| 77 | + """ |
| 78 | + for i in np.arange(0, len(x)): |
| 79 | + x[i, 0] = max(x[i, 0], 0) |
| 80 | + return x |
| 81 | + |
| 82 | + |
| 83 | +def d_relu(x): |
| 84 | + """ |
| 85 | + Relu Activation derivate function |
| 86 | + """ |
| 87 | + for i in np.arange(0, len(x)): |
| 88 | + if x[i, 0] >= 0: |
| 89 | + x[i, 0] = 1 |
| 90 | + else: |
| 91 | + x[i, 0] = 0 |
| 92 | + return x |
| 93 | + |
| 94 | + |
| 95 | +def sigmoid(x): |
| 96 | + """ |
| 97 | + Sigmoid activation function |
| 98 | + Output layer |
| 99 | + """ |
| 100 | + return 1 / (1 + np.exp(-x)) |
| 101 | + |
| 102 | + |
| 103 | +def d_sigmoid(x): |
| 104 | + """ |
| 105 | + Sigmoid activation derivate |
| 106 | + """ |
| 107 | + return sigmoid(x) ** 2 * np.exp(-x) |
| 108 | + |
| 109 | + |
| 110 | +def main(): |
| 111 | + """ |
| 112 | + Import load_breast_cancer dataset |
| 113 | + It is a binary classification problem with 569 samples and 30 attributes |
| 114 | + Categorical value output [0 1] |
| 115 | +
|
| 116 | + The date is split 70% / 30% in train and test sets |
| 117 | +
|
| 118 | + Before train the neural network, the data is normalized to [0 1] interval |
| 119 | +
|
| 120 | + The function trainNetwork() returns the weight and bias matrix to apply the |
| 121 | + transfer function to predict the output |
| 122 | + """ |
| 123 | + |
| 124 | + inputs = load_breast_cancer()["data"] |
| 125 | + target = load_breast_cancer()["target"] |
| 126 | + target = target.reshape(np.shape(target)[0], 1) |
| 127 | + |
| 128 | + scaler = MinMaxScaler() |
| 129 | + normalized_data = scaler.fit_transform(inputs) |
| 130 | + |
| 131 | + train = int(np.round(np.shape(normalized_data)[0] * 0.7)) |
| 132 | + x_train = normalized_data[0:train, :] |
| 133 | + x_test = normalized_data[train:, :] |
| 134 | + |
| 135 | + y_train = target[0:train] |
| 136 | + y_test = target[train:] |
| 137 | + |
| 138 | + epochs = 50 |
| 139 | + neurons = 5 |
| 140 | + w_co, bias_co, w_cs, bias_cs, error = train_network( |
| 141 | + neurons, x_train, y_train, epochs |
| 142 | + ) |
| 143 | + |
| 144 | + # find the labels with the weights obtained ( apply network transfer function ) |
| 145 | + yp_test = np.round( |
| 146 | + sigmoid(np.dot(relu(np.dot(x_test, w_co) + bias_co), w_cs) + bias_cs) |
| 147 | + ) |
| 148 | + |
| 149 | + print(f"accuracy: {accuracy_score(y_test, yp_test)}") |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments