|
23 | 23 | num_labels, (hidden_layer_size + 1));
|
24 | 24 | size(Theta1)
|
25 | 25 | size(Theta2)
|
| 26 | +size(X) |
26 | 27 |
|
27 | 28 | % Setup some useful variables
|
28 | 29 | m = size(X, 1);
|
|
64 | 65 | % and Theta2_grad from Part 2.
|
65 | 66 | %
|
66 | 67 |
|
67 |
| -% add bias column of 1's to Theta1 and Theta2 |
68 |
| -a1 = [ones(m,1), X]; |
69 |
| -a2 = [ones(m,1), sigmoid(a1*transpose(Theta1))]; |
70 |
| -a3 = sigmoid(a2*transpose(Theta2)); |
| 68 | +% add bias column of 1's to X and call it a1 |
| 69 | +% a1 = 5000 x 401 |
| 70 | +% Theta1 = 25 x 401 |
| 71 | +% Theta2 = 10 x 26 |
| 72 | + |
| 73 | +% Note: multiplication of the Theta matrices with a(n) matrices |
| 74 | +% can be done two ways, the one implemented below requiring |
| 75 | +% fewer transformation |
71 | 76 |
|
72 |
| -% compute the cost by looping over a3 values and accumulating the costs for each output node (10 in this case), for each y |
| 77 | +%a1 = [ones(m,1), X]; |
| 78 | +%a2 = [ones(m,1), transpose(sigmoid(Theta1*transpose(a1)))]; |
| 79 | +%a3 = transpose(sigmoid(Theta2*transpose(a2))); |
| 80 | + |
| 81 | +a1 = [ones(m,1), X]; |
| 82 | +a2 = [ones(m,1), sigmoid(a1 * transpose(Theta1))]; |
| 83 | +a3 = sigmoid(a2 * transpose(Theta2)); |
| 84 | +size(a2) |
| 85 | +size(a3) |
| 86 | +size(y) |
| 87 | + |
| 88 | +% compute the cost by looping over a3 values and accumulating the |
| 89 | +% costs for each output node (10 in this case), for each y |
73 | 90 | for i = 1:m
|
74 | 91 | % map y values to a row vector of num_labels length so that you can compare to output nodes while computing cost
|
75 | 92 | yvector = zeros(1,num_labels);
|
76 | 93 | yvector(1, y(i)) = 1;
|
77 | 94 | for j = 1:num_labels
|
78 |
| - % log(a3(j)) |
79 | 95 | % yvector and a3 should both be a row vector of size num_labels
|
80 |
| - J += -yvector(j)*log(a3(j)) - (1 - yvector(j))*log(1 - a3(j)); |
| 96 | + J += -yvector(j)*log(a3(i,j)) - (1 - yvector(j))*log(1 - a3(i,j)); |
81 | 97 | end
|
82 | 98 | end
|
83 | 99 |
|
84 | 100 | J = (1/m) * J;
|
85 |
| - |
86 | 101 |
|
87 | 102 |
|
88 | 103 |
|
|
0 commit comments