-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftmaxPredict.m
34 lines (26 loc) · 910 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 [prob, pred] = softmaxPredict(softmaxModel, data)
%% Function
% pred makes predictions on input data using the trained softmax
% model
%% Description of Inputs
% softmaxModel - model trained using softmaxTrain
% data - the N x M input matrix
% each column data(:, i) corresponds to a single
% test set
%% Description of Output
% pred - prediction matrix
% where pred(i) is argmax_c P(y(c) | x(i)).
%% Code
% Optimised parameters theta matrix - K x (N+1)?
theta = softmaxModel.optTheta;
% Initialise output
pred = zeros(1, size(data, 2));
% Easier if don't need probability
% p = theta*data;
% [~, pred] = max(p, [], 1);
TX = theta * data;
TX = bsxfun(@minus, TX, max(TX, [], 1)); % Prevents overflow
Exp_TX = exp(TX);
H = bsxfun(@rdivide, Exp_TX, sum(Exp_TX, 1));
[prob, pred] = max(H, [], 1);
end