2
$\begingroup$

I'm giving my first steps in really learning machine learning. As an exercise in my online course, it was asked for me to code the Cost function of some neural network that should resolve the handwritten problem with digits between 1 to 10.

As most of you know, the cost function of NN is given by:

$$ J(\theta)=\frac{1}{m} \sum_{i=1}^{m} \sum_{k=1}^{K}\left[-y_{k}^{(i)} \log \left(\left(h_{\theta}\left(x^{(i)}\right)\right)_{k}\right)-\left(1-y_{k}^{(i)}\right) \log \left(1-\left(h_{\theta}\left(x^{(i)}\right)\right)_{k}\right)\right] $$

so I tried to code it considering the following information:


where $\mathrm{h_{\theta}}(\mathrm{x^{i}})$ is computed as shown in Figure 2 and $K=10$ is the total number of possible labels. Note that $h_{\theta}\left(x^{(i)}\right)_{k}= a_{k}^{(3)}$ is the activation (output value) of the $k$ -th output unit Also, recall that whereas the original labels (In the variable $y$) were $1,2, \ldots, 10$, for the purpose of training a neural network, we need to recode the labels as vectors containing only values 0 or 1, so that

$$ y=\left[\begin{array}{c} 1 \\ 0 \\ 0 \\ \vdots \\ 0 \end{array}\right],\left[\begin{array}{l} 0 \\ 1 \\ 0 \\ \vdots \\ 0 \end{array}\right], \ldots . \text { or }\left[\begin{array}{c} 0 \\ 0 \\ 0 \\ \vdots \\ 1 \end{array}\right] $$

For example, if $x^{i}$ is an image of the digit $5,$ then the corresponding $y^{i n}$ (that you should use with the cost function) should be a $10-$ dimensional vector with $y_{5}=1,$ and the other elements equal to $0$. You should implement the feedforward computation that computes $\mathrm{h_{\theta}}(\mathrm{x^{i}})$ for every example $i$ and sum the cost overall examples. Your code should also work for a dataset of any size, with any number of labels (you can assume that there are always at least $K \geq 3$ labels)


plotting the graph, I got: enter image description here

here's my error cost function code:

function [J grad] = nnCostFunction(nn_params, ...
                                   input_layer_size, ...
                                   hidden_layer_size, ...
                                   num_labels, ...
                                   X, y, lambda)

% Setup some useful variables
 m = size(X, 1)
% bias  = ones(m,1)';
Theta1 = [ ones(401,1)  reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
                 hidden_layer_size, (input_layer_size + 1))']';

Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
                 num_labels, (hidden_layer_size + 1));

J =0;
error_history = zeros(1,m);
y_coded = zeros(1,num_labels);
for i = 1:m
    y_coded(y) = 1;
    X_in = [1 X(i,:)]';
    hypotesis_array = sigmoid(Theta2*sigmoid(Theta1*X_in));
    
    for k =1:num_labels
        J = J  -(y_coded(k)*log10(hypotesis_array(k)) -  (1- y_coded(k))*log10(hypotesis_array(k)));
    end
    J =J/m;
    error_history(i) = J;
    
    
end
plot(1:5000, error_history);
ylabel("Error_Value");
xlabel("training iteration");

I did that considering that the weights were previously given. Is it normal getting this noise error history value or I did something wrong with my code?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.