-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathComputeEntailmentExampleCostAndGrad.m
More file actions
188 lines (162 loc) · 7.31 KB
/
ComputeEntailmentExampleCostAndGrad.m
File metadata and controls
188 lines (162 loc) · 7.31 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
% Want to distribute this code? Have other questions? -> sbowman@stanford.edu
function [ cost, grad, embGrad, pred ] = ComputeEntailmentExampleCostAndGrad(theta, thetaDecoder, dataPoint, separateWordFeatures, hyperParams, computeGradient)
% Compute cost, gradient, and predicted label for one example.
grad = [];
embGrad = [];
assert(~hyperParams.useLattices, 'Use batched computation for the LatticeNN.');
assert(~hyperParams.gpu, 'Use batching for GPU computation.');
% Unpack theta
[ mergeMatrices, mergeMatrix ...
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;
% Set the number of mposition functions
if hyperParams.useSumming
NUMCOMP = 0;
elseif ~hyperParams.untied
NUMCOMP = 1;
else
NUMCOMP = 3;
end
NUMTRANS = size(embeddingTransformMatrix, 3);
left = dataPoint.left;
right = dataPoint.right;
trueLabel = dataPoint.label;
% Run the trees/sequences forward
left.updateFeatures(wordFeatures, compositionMatrices, ...
compositionMatrix, embeddingTransformMatrix, hyperParams.compNL, computeGradient, hyperParams);
right.updateFeatures(wordFeatures, compositionMatrices, ...
compositionMatrix, embeddingTransformMatrix, hyperParams.compNL, computeGradient, hyperParams);
leftFeatures = left.getFeatures();
rightFeatures = right.getFeatures();
[ leftFeatures, leftMask ] = Dropout(leftFeatures, hyperParams.topDropout, computeGradient, hyperParams.gpu);
[ rightFeatures, rightMask ] = Dropout(rightFeatures, hyperParams.topDropout, computeGradient, hyperParams.gpu);
% Compute classification tensor layer (or plain RNN layer)
if hyperParams.useThirdOrderMerge
mergeOutput = ComputeTensorLayer(leftFeatures, ...
rightFeatures, mergeMatrices, mergeMatrix, hyperParams.classNL);
else
mergeOutput = ComputeRNNLayer(leftFeatures, rightFeatures, ...
mergeMatrix, hyperParams.classNL);
end
% Run layers forward
extraInputs = zeros(hyperParams.penultDim, 1, hyperParams.topDepth);
extraInnerOutputs = zeros(hyperParams.penultDim, 1, hyperParams.topDepth - 1);
extraInputs(:, 1, 1) = mergeOutput;
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(trueLabel(1));
[ labelProbs, cost ] = ComputeSoftmaxLayer( ...
extraInputs(:,hyperParams.topDepth), softmaxMatrix, hyperParams, trueLabel', multiplier);
else
[ labelProbs, cost ] = ComputeSoftmaxLayer( ...
extraInputs(:,hyperParams.topDepth), softmaxMatrix, hyperParams, trueLabel');
end
% Produce gradient
if nargout > 1 && (nargin < 6 || computeGradient)
if ~isempty(hyperParams.labelCostMultipliers)
[ localSoftmaxGradient, softmaxDelta ] = ...
ComputeSoftmaxClassificationGradients( ...
softmaxMatrix, labelProbs, trueLabel', ...
extraInputs(:,hyperParams.topDepth), hyperParams, multiplier);
else
[ localSoftmaxGradient, softmaxDelta ] = ...
ComputeSoftmaxClassificationGradients( ...
softmaxMatrix, labelProbs, trueLabel', ...
extraInputs(:,hyperParams.topDepth), hyperParams);
end
% Compute gradients for extra top layers
[ localExtraMatrixGradients, extraDelta ] = ...
ComputeExtraClassifierGradients(classifierExtraMatrix,...
softmaxDelta, extraInputs, hyperParams.classNLDeriv);
if hyperParams.useThirdOrderMerge
% Compute gradients for classification tensor layer
[ localMergeMatricesGradients, localMergeMatrixGradients, ...
MergeDeltaLeft, MergeDeltaRight ] = ...
ComputeTensorLayerGradients(leftFeatures, rightFeatures, ...
mergeMatrices, mergeMatrix, ...
extraDelta, hyperParams.classNLDeriv, mergeOutput);
else
% Compute gradients for classification first layer
localMergeMatricesGradients = zeros(0, 0, 0);
[ localMergeMatrixGradients, MergeDeltaLeft, ...
MergeDeltaRight ] = ...
ComputeRNNLayerGradients(leftFeatures, rightFeatures, ...
mergeMatrix, ...
extraDelta, hyperParams.classNLDeriv, mergeOutput);
end
MergeDeltaLeft = MergeDeltaLeft .* leftMask;
MergeDeltaRight = MergeDeltaRight .* rightMask;
[ localWordFeatureGradients, ...
localCompositionMatricesGradients, ...
localCompositionMatrixGradients, ...
localEmbeddingTransformMatrixGradients ] = ...
left.getGradient(MergeDeltaLeft, [], wordFeatures, ...
compositionMatrices, compositionMatrix, ...
embeddingTransformMatrix, ...
hyperParams.compNLDeriv, hyperParams);
[ rightWordGradients, ...
rightCompositionMatricesGradients, ...
rightCompositionMatrixGradients, ...
rightEmbeddingTransformMatrixGradients ] = ...
right.getGradient(MergeDeltaRight, [], wordFeatures, ...
compositionMatrices, compositionMatrix, ...
embeddingTransformMatrix, ...
hyperParams.compNLDeriv, hyperParams);
if hyperParams.trainWords
localWordFeatureGradients = localWordFeatureGradients ...
+ rightWordGradients;
end
localCompositionMatricesGradients = localCompositionMatricesGradients...
+ rightCompositionMatricesGradients;
localCompositionMatrixGradients = localCompositionMatrixGradients...
+ rightCompositionMatrixGradients;
localEmbeddingTransformMatrixGradients = localEmbeddingTransformMatrixGradients...
+ rightEmbeddingTransformMatrixGradients;
% Pack up gradients
if hyperParams.largeVocabMode
grad = param2stack(localMergeMatricesGradients, ...
localMergeMatrixGradients, ...
localSoftmaxGradient, ...
[], localCompositionMatricesGradients, ...
localCompositionMatrixGradients, ...
[], ...
localExtraMatrixGradients, ...
localEmbeddingTransformMatrixGradients);
embGrad = localWordFeatureGradients;
else
[ grad, dec ] = param2stack(localMergeMatricesGradients, ...
localMergeMatrixGradients, ...
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