-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBPMLLCost.m
73 lines (58 loc) · 1.76 KB
/
BPMLLCost.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function [ cost, grad ] = BPMLLCost(theta, X, T, visibleSize, hiddenSize, labelSize, options)
l2_lambda = options.l2_lambda;
useGPU = false;
debug = false;
if isfield(options,'useGPU')
useGPU = options.useGPU;
end
if isfield(options,'debug')
debug = options.debug;
end
%% Unroll parameter
% Extract out the "stack"
W1 = reshape(theta(1:visibleSize*hiddenSize), hiddenSize, visibleSize);
b1 = theta(visibleSize*hiddenSize+1:visibleSize*hiddenSize+hiddenSize);
W2 = reshape(theta(visibleSize*hiddenSize+hiddenSize+1:visibleSize*hiddenSize+hiddenSize+hiddenSize*labelSize), labelSize, hiddenSize);
b2 = theta(visibleSize*hiddenSize+hiddenSize+hiddenSize*labelSize+1:end);
[D, M] = size(X);
% forward pass for autoencoder
[H, dH] = tanh_act(bsxfun(@plus, W1*X, b1));
[O, dO] = tanh_act(bsxfun(@plus, W2*H, b2));
if useGPU
[delta] = eval_bpmll_loss_gpumex(T,O);
if debug
[err,~] = computePW(gather(T),gather(O));
err = gpuArray(err);
end
else
[delta] = eval_bpmll_loss_cpumex(T,O);
if debug
[err,~] = computePW(T,O);
end
end
if debug
cost = 1/M*sum(err) + .5*l2_lambda*sum(sum(W1(:).^2) + sum(W2(:).^2));
else
cost = 0;
end
if nargout > 1
delta = (1/M)*delta .* dO;
W2grad = delta*H' + l2_lambda*W2;
b2grad = sum(delta,2);
delta = (W2'*delta) .* dH;
W1grad = delta*X' + l2_lambda*W1;
b1grad = sum(delta,2);
%% Roll gradient vector
grad = [W1grad(:) ; b1grad(:) ; W2grad(:) ; b2grad(:)];
else
grad = zeros(size(theta));
end
end
function [F,dF] = sigm_act(X)
F = 1./(1+exp(-X));
dF = F.*(1-F);
end
function [F, dF] = tanh_act(X)
F = tanh(X);
dF = 1-F.^2;
end