-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathsoftmaxPredict.m
34 lines (22 loc) · 951 Bytes
/
softmaxPredict.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function [pred] = softmaxPredict(softmaxModel, data)
% softmaxModel - model trained using softmaxTrain
% data - the N x M input matrix, where each column data(:, i) corresponds to
% a single test set
%
% Your code should produce the prediction matrix
% pred, where pred(i) is argmax_c P(y(c) | x(i)).
% Unroll the parameters from theta
theta = softmaxModel.optTheta; % this provides a numClasses x inputSize matrix
pred = zeros(1, size(data, 2));
%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute pred using theta assuming that the labels start
% from 1.
numClasses = softmaxModel.numClasses;
inputSize = softmaxModel.inputSize;
theta = reshape(theta, numClasses, inputSize);
M = exp(theta * data);
%可以用这种方法替换我前面写的repmat
M = bsxfun(@rdivide, M, sum(M));
[p,pred] = max(M, [], 1);
% ---------------------------------------------------------------------
end