MLP Numpy Module


class mlp.MLP(seed=99, verbose=False, restore=False)[source]

Bases: object

Initialize MLP object

Parameters:
  • seed (int) – seed for random
  • verbose (bool) – If want to print help message during using MLP object
  • restore (bool) – Restore from checkpoint or not?
nn_architecture = [{'input_dim': 1296, 'output_dim': 100, 'activation': 'relu'}, {'input_dim': 100, 'output_dim': 10, 'activation': 'softmax'}]
static relu(z)[source]

Function which apply relu function to current z values

Parameters:z (np.array) – current value
Returns:current value after activation function a_curr
Return type:np.array
static relu_backward(d_a, z)[source]

Function which apply relu function to current error value for backpropagation process

Parameters:
  • d_a (np.array) – current error value
  • z (np.array) – current value before activation function
Returns:

error before activation function

Return type:

np.array

static softmax(z)[source]

Function which apply softmax function to current z values

Parameters:z (np.array) – current value
Returns:current value after activation function a_curr
Return type:np.array
softmax_backward(d_a, z)[source]

Softmax backward propagation

Parameters:
  • d_a (np.array) – current error value
  • z (np.array) – current value before activation function
Returns:

error before activation function

Return type:

np.array

single_layer_forward_propagation(a_prev, w_curr, b_curr, activation='relu')[source]

Function for make forward propagation through one layer in Neural Network

Parameters:
  • a_prev (np.array) – previous value after activation function (input)
  • w_curr (np.array) – current weights value
  • b_curr (np.array) – current bias value
  • activation (str) – type of activation function
Returns:

a_curr and value before activation function z_curr

Return type:

tuple

full_forward_propagation(x)[source]

Function for make full forward propagation through Neural Network

Parameters:x (np.array) – training data
Returns:None
Return type:None
get_cost_value(targets, epsilon=1e-10) → float[source]

Cross-entropy loss function

Parameters:
  • targets (np.array) – ground truth
  • epsilon (float) – hiperparater
Returns:

cross_entropy value

Return type:

float

get_accuracy_value(targets: numpy.array) → float[source]

Function to count accuracy value

Parameters:targets (np.array) – ground truth
Returns:accuracy value
Return type:float
single_layer_backward_propagation(d_a_curr, w_curr, z_curr, a_prev, activation='relu')[source]

Function for make single layer backward propagation

Parameters:
  • d_a_curr (np.array) – current error value
  • w_curr (np.array) – current weights value
  • z_curr (np.array) – current value before activation function
  • a_prev (np.array) – previous neuron autput value
  • activation (str) – type of activation function
Returns:

d_a_prev, d_w_curr, db_curr

Return type:

tuple

full_backward_propagation(targets: numpy.array) → dict[source]

Function for make full backward propagation through Neural Network

Parameters:targets (np.array) – ground truth
Returns:gradients values to update weights of NN
Return type:dict
update(grads_values, learning_rate)[source]

Update NN weights and bias

Parameters:
  • grads_values (np.array) – gradient value
  • learning_rate (float) – current learning_rate
train(train_data: numpy.array, train_labels: numpy.array, epochs: int = 1000, learning_rate: float = 0.001) → tuple[source]

Function which trains model

Parameters:
  • train_data (np.array) – x data for training
  • train_labels (np.array) – y data for training
  • epochs (int) – number of epochs for training
  • learning_rate (float) – hiperparameter of training
Returns:

(self.params_values, self.cost_history, self.accuracy_history)

Return type:

tuple

one_hot_encode(labels: numpy.array)[source]

Function which convert labels to vector

Parameters:labels (np.array) – np.array of labels to convert
Returns:converted labels in one_hot_encode
Return type:np.array
one_hot_decode(labels) → numpy.array[source]

Function which convert vector to labels

Parameters:labels (np.array) – np.array of predicted labels to convert
Returns:converted labels from one_hot_encode
Return type:np.array
test(x_input, ground_truth)[source]

Function for test model

Parameters:
  • x_input (np.array) – test data
  • ground_truth (np.array) – test labels
Returns:

test accuracy

Return type:

float

predict(x_input)[source]

Funtion to predict output for given input

Parameters:x_input (np.array) – image data
Returns:predition vector
Return type:np.array
save()[source]

Funtion for saving model current variables to files

load()[source]

Funtion for loading model current variables to files


Return Home