predict.m 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. a1 = [ones(size(X, 1),1) X]';
  9. z2 = Theta1 * a1;
  10. a2 = [ones(1, size(z2,2)); sigmoid(z2)];
  11. z3 = Theta2 * a2;
  12. a3 = sigmoid(z3);
  13. [~, p] = max(a3, [], 1);
  14. % You need to return the following variables correctly
  15. % ====================== YOUR CODE HERE ======================
  16. % Instructions: Complete the following code to make predictions using
  17. % your learned neural network. You should set p to a
  18. % vector containing labels between 1 to num_labels.
  19. %
  20. % Hint: The max function might come in useful. In particular, the max
  21. % function can also return the index of the max element, for more
  22. % information see 'help max'. If your examples are in rows, then, you
  23. % can use max(A, [], 2) to obtain the max for each row.
  24. %
  25. % =========================================================================
  26. end