predict.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. function p = predict(Theta1, Theta2, X)
  2. %PREDICT Predict the label of an input given a trained neural network
  3. % p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
  4. % trained weights of a neural network (Theta1, Theta2)
  5. % Useful values
  6. m = size(X, 1);
  7. num_labels = size(Theta2, 1);
  8. % You need to return the following variables correctly
  9. p = zeros(size(X, 1), 1);
  10. % ====================== YOUR CODE HERE ======================
  11. % Instructions: Complete the following code to make predictions using
  12. % your learned neural network. You should set p to a
  13. % vector containing labels between 1 to num_labels.
  14. %
  15. % Hint: The max function might come in useful. In particular, the max
  16. % function can also return the index of the max element, for more
  17. % information see 'help max'. If your examples are in rows, then, you
  18. % can use max(A, [], 2) to obtain the max for each row.
  19. %
  20. X1 = [ones(rows(X), 1) X];
  21. a2 = sigmoid(X1 * Theta1');
  22. a2_1 = [ones(rows(a2), 1) a2];
  23. % size = 5000 * 26
  24. %size(a2_1)
  25. a3 = sigmoid(a2_1 * Theta2');
  26. % size = 5000 * 10
  27. %size(a3)
  28. # pick the max value at each row
  29. [v,p] = max(a3, [], 2);
  30. # check to see that the array contain values that are mapped to the 10 classes
  31. #unique(p)
  32. % =========================================================================
  33. end