human
6. Flatten
Convolutional neural networks allow one to perform better image processing than fully connected neural networks. We will combine convolutional layers, wich deal with 4-dimensional tensors, with dense layers, wich work with matrices. To make it work, we should implement Flatten layer. 
Flatten layer takes a 4-dimensional tensor of size (n_obj, n_channels, h, w) as an input and reshaes it to a 2-dimensional tensor (matrix) of size (n_obj, n_channels * h * w).
The backward pass of this layer is not complicated. Think about it. 
Please implement flatten_forward and flatten_grad_input functions using np.reshape. Here is the description of np.reshape.
 Write me a the Python program you requested?
assistant
#Here is the Python program you requested.
def flatten_forward(x_input):
    """Perform the reshaping of the tensor of size `(K, L, M, N)` 
        to the tensor of size `(K, L*M*N)`
    # Arguments
        x_input: np.array of size `(K, L, M, N)`
    # Output
        output: np.array of size `(K, L*M*N)`
    """
    #################
    ### YOUR CODE ###
    #################
    return output

am.test_student_function(username, flatten_forward, ['x_input'])

def flatten_grad_input(x_input, grad_output):
    """Calculate partial derivative of the loss with respect to the input
    # Arguments
        x_input: partial derivative of the loss 
            with respect to the output
            np.array of size `(K, L*M*N)`
    # Output
        output: partial derivative of the loss 
            with respect to the input
            np.array of size `(K, L, M, N)`
    """
    #################
    ### YOUR CODE ###
    #################
    return grad_input

am.test_student_function(username, flatten_grad_input, ['x_input', 'grad_output'])

class FlattenLayer(Layer):
    
    def forward(self, x_input):
        self.output = flatten_forward(x_input)
        return self.output
    
    def backward(self, x_input, grad_output):
        output = flatten_grad_input(x_input, grad_output)
        return output