# Backpropagation Exercise

# Goal: implement the code to calculate one Backpropagation update step
# for two sets of weights (weights from input layer to hidden layer and
# weights from hidden layer to output layer)

# TODOs in backward pass:
## Calculate the network's output error (error).
## Calculate the output layer's error term (output_error_term).
## Use backpropagation to calculate the hidden layer's error term (hidden_error_term).
## Calculate the change in weights (delta_w_h_o and delta_w_i_h) that result from
### propagating the erroes back through the neural network.


import numpy as np


def sigmoid(x):
    """
    Calculate sigmoid
    """
    return 1 / (1 + np.exp(-x))


x = np.array([0.5, 0.1, -0.2])
target = 0.6 # the value of y
learnrate = 0.5

weights_input_hidden = np.array([[0.5, -0.6],
                                 [0.1, -0.2],
                                 [0.1, 0.7]])

weights_hidden_output = np.array([0.1, -0.3])

## Forward pass
hidden_layer_input = np.dot(x, weights_input_hidden)
hidden_layer_output = sigmoid(hidden_layer_input)

output_layer_in = np.dot(hidden_layer_output, weights_hidden_output)
output = sigmoid(output_layer_in)

## Backwards pass
## TODO: Calculate output error
error = target - output   # y - y_hat

# TODO: Calculate error term for output layer
output_error_term = error * output * (1 - output)

# TODO: Calculate error term for hidden layer
hidden_error_term = np.dot(weights_hidden_output, output_error_term) * hidden_layer_output * (1 - hidden_layer_output)

# TODO: Calculate change in weights for hidden layer to output layer
delta_w_h_o = learnrate * output_error_term * hidden_layer_output

# TODO: Calculate change in weights for input layer to hidden layer
delta_w_i_h = learnrate * hidden_error_term * x[:, None]

print('Change in weights for hidden layer to output layer:')
print(delta_w_h_o)
print('Change in weights for input layer to hidden layer:')
print(delta_w_i_h)

# Backpropagation Exercise

# Goal: implement the code to calculate one Backpropagation update step
# for two sets of weights (weights from input layer to hidden layer and
# weights from hidden layer to output layer)

# TODOs in backward pass:
## Calculate the network's output error (error).
## Calculate the output layer's error term (output_error_term).
## Use backpropagation to calculate the hidden layer's error term (hidden_error_term).
## Calculate the change in weights (delta_w_h_o and delta_w_i_h) that result from
### propagating the erroes back through the neural network.


import numpy as np


def sigmoid(x):
    """
    Calculate sigmoid
    """
    return 1 / (1 + np.exp(-x))


x = np.array([0.5, 0.1, -0.2])
target = 0.6 # the value of y
learnrate = 0.5

weights_input_hidden = np.array([[0.5, -0.6],
                                 [0.1, -0.2],
                                 [0.1, 0.7]])

weights_hidden_output = np.array([0.1, -0.3])

## Forward pass
hidden_layer_input = np.dot(x, weights_input_hidden)
hidden_layer_output = sigmoid(hidden_layer_input)

output_layer_in = np.dot(hidden_layer_output, weights_hidden_output)
output = sigmoid(output_layer_in)

## Backwards pass
## TODO: Calculate output error
error = target - output   # y - y_hat

# TODO: Calculate error term for output layer
output_error_term = error * output * (1 - output)

# TODO: Calculate error term for hidden layer
hidden_error_term = np.dot(weights_hidden_output, output_error_term) * hidden_layer_output * (1 - hidden_layer_output)

# TODO: Calculate change in weights for hidden layer to output layer
delta_w_h_o = learnrate * output_error_term * hidden_layer_output

# TODO: Calculate change in weights for input layer to hidden layer
delta_w_i_h = learnrate * hidden_error_term * x[:, None]

print('Change in weights for hidden layer to output layer:')
print(delta_w_h_o)
print('Change in weights for input layer to hidden layer:')
print(delta_w_i_h)

# Backpropagation Exercise # Goal: implement the code to calculate one Backpropagation update step # for two sets of weights (weights from input layer to hidden layer and # weights from hidden layer to output layer) # TODOs in backward pass: ## Calculate the network's output error (error). ## Calculate the output layer's error term (output_error_term). ## Use backpropagation to calculate the hidden layer's error term (hidden_error_term). ## Calculate the change in weights (delta_w_h_o and delta_w_i_h) that result from ### propagating the erroes back through the neural network. Calculate sigmoid # the value of y ## Forward pass ## Backwards pass ## TODO: Calculate output error # y - y_hat # TODO: Calculate error term for output layer # TODO: Calculate error term for hidden layer # TODO: Calculate change in weights for hidden layer to output layer # TODO: Calculate change in weights for input layer to hidden layer