-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathComputeSentenceClassificationExampleCostAndGrad.m
More file actions
155 lines (130 loc) · 5.56 KB
/
ComputeSentenceClassificationExampleCostAndGrad.m
File metadata and controls
155 lines (130 loc) · 5.56 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
% Want to distribute this code? Have other questions? -> sbowman@stanford.edu
function [ cost, grad, embGrad, pred ] = ComputeSentenceClassificationExampleCostAndGrad(theta, thetaDecoder, dataPoint, separateWordFeatures, hyperParams, computeGradient)
% Compute cost, gradient, and predicted label for one example.
grad = [];
embGrad = [];
assert(~hyperParams.useLattices, 'Unbatched computation is not available for the LatticeNN. Use batching.');
assert(~hyperParams.gpu, 'Use batching for GPU computation.');
% Unpack theta
[ ~, ~, ...
softmaxMatrix, trainedWordFeatures, compositionMatrices,...
compositionMatrix, ~, classifierExtraMatrix, ...
embeddingTransformMatrix ] ...
= stack2param(theta, thetaDecoder);
if hyperParams.trainWords && ~hyperParams.largeVocabMode
wordFeatures = trainedWordFeatures;
else
wordFeatures = separateWordFeatures;
end
DIM = hyperParams.dim;
EMBDIM = hyperParams.embeddingDim;
if isfield(hyperParams, 'smallVecs') && hyperParams.smallVecs
sigma = 0.25;
else
sigma = 1;
end
if length(hyperParams.randomEmbeddingIndices) > 0
wordFeatures(:, hyperParams.randomEmbeddingIndices) = ...
fNormrnd(0, sigma, [EMBDIM, length(hyperParams.randomEmbeddingIndices)], ...
hyperParams.gpu, hyperParams.gpu && hyperParams.largeVocabMode);
wordFeatures(1, hyperParams.randomEmbeddingIndices) = 1;
end
% Set the number of mposition functions
if hyperParams.useSumming
NUMCOMP = 0;
elseif ~hyperParams.untied
NUMCOMP = 1;
else
NUMCOMP = 3;
end
NUMTRANS = size(embeddingTransformMatrix, 3);
% Run the trees/sequences forward
dataPoint.sentence.updateFeatures(wordFeatures, compositionMatrices, ...
compositionMatrix, embeddingTransformMatrix, hyperParams.compNL, computeGradient, hyperParams);
[ features, mask ] = Dropout(dataPoint.sentence.getFeatures(), hyperParams.topDropout, computeGradient, hyperParams.gpu);
% Run layers forward
if hyperParams.penultDim == 2 * hyperParams.dim
% Hack for transfer learning: pad with zeros.
features = [zeros(size(features), 'like', features); features];
end
extraInputs = zeros(hyperParams.penultDim, 1, hyperParams.topDepth);
extraInnerOutputs = zeros(hyperParams.penultDim, 1, hyperParams.topDepth - 1);
extraInputs(:, 1, 1) = features;
for layer = 1:(hyperParams.topDepth - 1)
extraInnerOutputs(:, 1, layer) = classifierExtraMatrix(:, :, layer) * [1; extraInputs(:, 1, layer)];
extraInputs(:, 1, layer + 1) = hyperParams.classNL(extraInnerOutputs(:, 1, layer));
end
if ~isempty(hyperParams.labelCostMultipliers)
multiplier = hyperParams.labelCostMultipliers(dataPoint.label(1));
[ labelProbs, cost ] = ComputeSoftmaxLayer( ...
extraInputs(:,hyperParams.topDepth), softmaxMatrix, hyperParams, dataPoint.label', multiplier);
else
[ labelProbs, cost ] = ComputeSoftmaxLayer( ...
extraInputs(:,hyperParams.topDepth), softmaxMatrix, hyperParams, dataPoint.label');
end
% Produce gradient
if nargout > 1 && (nargin < 6 || computeGradient)
if ~isempty(hyperParams.labelCostMultipliers)
[ localSoftmaxGradient, softmaxDelta ] = ...
ComputeSoftmaxClassificationGradients( ...
softmaxMatrix, labelProbs, dataPoint.label', ...
extraInputs(:,hyperParams.topDepth), hyperParams, multiplier);
else
[ localSoftmaxGradient, softmaxDelta ] = ...
ComputeSoftmaxClassificationGradients( ...
softmaxMatrix, labelProbs, dataPoint.label', ...
extraInputs(:,hyperParams.topDepth), hyperParams);
end
% Compute gradients for extra top layers
[ localExtraMatrixGradients, extraDelta ] = ...
ComputeExtraClassifierGradients(classifierExtraMatrix,...
softmaxDelta, extraInputs, hyperParams.classNLDeriv);
if hyperParams.penultDim == 2 * hyperParams.dim
% Hack for transfer learning: pad with zeros.
extraDelta = extraDelta(hyperParams.dim + 1:end, :);
end
extraDelta = extraDelta .* mask;
[ localWordFeatureGradients, ...
localCompositionMatricesGradients, ...
localCompositionMatrixGradients, ...
localEmbeddingTransformMatrixGradients ] = ...
dataPoint.sentence.getGradient(extraDelta, [], wordFeatures, ...
compositionMatrices, compositionMatrix, ...
embeddingTransformMatrix, ...
hyperParams.compNLDeriv, hyperParams);
% Pack up gradients
if hyperParams.largeVocabMode
grad = param2stack([], ...
[], ...
localSoftmaxGradient, ...
[], localCompositionMatricesGradients, ...
localCompositionMatrixGradients, ...
[], ...
localExtraMatrixGradients, ...
localEmbeddingTransformMatrixGradients);
embGrad = localWordFeatureGradients;
else
[ grad, dec ] = param2stack([], ...
[], ...
localSoftmaxGradient, ...
localWordFeatureGradients, localCompositionMatricesGradients, ...
localCompositionMatrixGradients, ...
[], ...
localExtraMatrixGradients, ...
localEmbeddingTransformMatrixGradients);
embGrad = [];
end
% Clip the gradient.
if hyperParams.clipGradients
gradNorm = norm(grad);
if gradNorm > hyperParams.maxGradNorm
grad = grad .* (hyperParams.maxGradNorm ./ gradNorm);
end
end
end
% Compute prediction. Note: This will be in integer, indexing into whichever class set was used
% for this example.
if nargout > 3
[ ~, pred ] = max(labelProbs);
end
end