project-bikesharing/my_answers.py
def backpropagation(self, final_outputs, hidden_outputs, X, y, delta_weights_i_h, delta_weights_h_o):
        ''' Implement backpropagation

            Arguments
            ---------
            final_outputs: output from forward pass
            y: target (i.e. label) batch
            delta_weights_i_h: change in weights from input to hidden layers
            delta_weights_h_o: change in weights from hidden to output layers

        '''
        # print("Starting backpropagation: ")
        # print("    final_outputs:     {}".format(final_outputs))
        # print("    hidden_outputs:    {}".format(hidden_outputs))
        # print("    X (input):         {}".format(X))
        # print("    y (labels):        {}".format(y))
        # print("    delta_weights_i_h: {}".format(delta_weights_i_h))
        # print("    delta_weights_h_o: {}".format(delta_weights_h_o))

        #### Implement the backward pass here ####
        ### Backward pass ###

        # DONE: Output error - Replace this value with your calculations.
        error = y - final_outputs  # Output layer error is the difference between desired target and actual output.

        # DONE: Backpropagated error terms - Replace these values with your calculations.
        output_error_term = error #not using activation function for error, so no derivative needed here.

        # DONE: Calculate the hidden layer's contribution to the error

        # Weight step (hidden to output)
        delta_weights_h_o += (output_error_term * hidden_outputs.T)[:, None]

        hidden_error = np.matmul(output_error_term, self.weights_hidden_to_output.T)  # hidden error shape: (2, 1), hidden_outputs shape: (1, 2)
        hidden_error_term = hidden_error.T * hidden_outputs.T * (1 - hidden_outputs.T)

        # Weight step (input to hidden)
        delta_weights_i_h += np.matmul(hidden_error_term[:, None], X[:, None].T).T

        return delta_weights_i_h, delta_weights_h_o
 Implement backpropagation

            Arguments
            ---------
            final_outputs: output from forward pass
            y: target (i.e. label) batch
            delta_weights_i_h: change in weights from input to hidden layers
            delta_weights_h_o: change in weights from hidden to output layers

        
Implement backpropagation
Arguments

output from forward pass
y: target  batch
delta_weights_i_h: change in weights from input to hidden layers
delta_weights_h_o: change in weights from hidden to output layers
Implement backpropagation Arguments output from forward pass y : target batch delta_weights_i_h : change in weights from input to hidden layers delta_weights_h_o : change in weights from hidden to output layers
def backpropagation(self, final_outputs, hidden_outputs, X, y, delta_weights_i_h, delta_weights_h_o):
        error = y - final_outputs  
        output_error_term = error 
        delta_weights_h_o += (output_error_term * hidden_outputs.T)[:, None]
        hidden_error = np.matmul(output_error_term, self.weights_hidden_to_output.T)  
        hidden_error_term = hidden_error.T * hidden_outputs.T * (1 - hidden_outputs.T)
        delta_weights_i_h += np.matmul(hidden_error_term[:, None], X[:, None].T).T
        return delta_weights_i_h, delta_weights_h_o
def backpropagation ( self , final_outputs , hidden_outputs , X , y , delta_weights_i_h , delta_weights_h_o ) : error = y - final_outputs output_error_term = error delta_weights_h_o += ( output_error_term * hidden_outputs . T ) [ : , None ] hidden_error = np . matmul ( output_error_term , self . weights_hidden_to_output . T ) hidden_error_term = hidden_error . T * hidden_outputs . T * ( 1 - hidden_outputs . T ) delta_weights_i_h += np . matmul ( hidden_error_term [ : , None ] , X [ : , None ] . T ) . T return delta_weights_i_h , delta_weights_h_o
Implement backpropagation
Arguments
Implement backpropagation Arguments
''' Implement backpropagation

            Arguments
            ---------
            final_outputs: output from forward pass
            y: target (i.e. label) batch
            delta_weights_i_h: change in weights from input to hidden layers
            delta_weights_h_o: change in weights from hidden to output layers

        ''' # print("Starting backpropagation: ") # print("    final_outputs:     {}".format(final_outputs)) # print("    hidden_outputs:    {}".format(hidden_outputs)) # print("    X (input):         {}".format(X)) # print("    y (labels):        {}".format(y)) # print("    delta_weights_i_h: {}".format(delta_weights_i_h)) # print("    delta_weights_h_o: {}".format(delta_weights_h_o)) #### Implement the backward pass here #### ### Backward pass ### # DONE: Output error - Replace this value with your calculations. # Output layer error is the difference between desired target and actual output. # DONE: Backpropagated error terms - Replace these values with your calculations. #not using activation function for error, so no derivative needed here. # DONE: Calculate the hidden layer's contribution to the error # Weight step (hidden to output) # hidden error shape: (2, 1), hidden_outputs shape: (1, 2) # Weight step (input to hidden)
self
final_outputs
hidden_outputs
X
y
delta_weights_i_h
delta_weights_h_o