-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftmaxPredict.m
38 lines (25 loc) · 920 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
34
35
36
37
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));
%% disp(size(theta));
%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute pred using theta assuming that the labels start
% from 1.
M = theta * data;
M = bsxfun(@minus, M, max(M, [], 1));
M = exp(M);
h_x = bsxfun(@rdivide,M,sum(M));
[n,m] = size(data);
for i=1:m
[v,I] = max(h_x(:,i:i));
pred(i) = I;
end
% ---------------------------------------------------------------------
end