-
Notifications
You must be signed in to change notification settings - Fork 14
/
LogSoftMax.lua
83 lines (67 loc) · 2.41 KB
/
LogSoftMax.lua
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
require 'nn'
nn.LogSoftMax.baseUpdateOutput = nn.LogSoftMax.updateOutput
nn.LogSoftMax.baseUpdateGradInput = nn.LogSoftMax.updateGradInput
function nn.LogSoftMax:updateOutput(input)
if torch.type(input) ~= 'torch.ClTensor' then
return self:baseUpdateOutput(input)
end
if input:dim() == 1 then
if self.maxbuffer == nil then
self.maxbuffer, self.resind = input:max(1)
self.vec_size = input:size(1)
end
self.output:resize(input:size())
self.maxbuffer:max(self.resind, input, 1)
self.output:copy(input)
self.output:csub(self.maxbuffer:expand(input:size(1)))
self.output:exp()
self.maxbuffer:sum(self.output,1)
self.output:cdiv(self.maxbuffer:expand(input:size(1)))
self.output:log()
return self.output
elseif input:dim() == 2 then
if self.maxbuffer == nil then
self.maxbuffer, self.resind = input:max(2)
self.vec_size = input:size(2)
end
self.output:resize(input:size())
self.maxbuffer:max(self.resind, input, 2)
self.output:copy(input)
self.output:csub(self.maxbuffer:expand(input:size(1), input:size(2)))
self.output:exp()
self.maxbuffer:sum(self.output,2)
self.output:cdiv(self.maxbuffer:expand(input:size(1), input:size(2)))
self.output:log()
return self.output
else
error('LogSoftMax expects 1-d or 2-d tensor currently')
end
end
function nn.LogSoftMax:updateGradInput(input, gradOutput)
if torch.type(input) ~= 'torch.ClTensor' then
return self:baseUpdateGradInput(input, gradOutput)
end
local nElement = self.gradInput:nElement()
self.gradInput:resizeAs(input)
if self.gradInput:nElement() ~= nElement then
self.gradInput:zero()
end
if input:dim() == 1 then
self.maxbuffer:sum(gradOutput, 1)
self.gradInput:copy(self.output)
self.gradInput:exp()
self.gradInput:cmul(self.maxbuffer:expand(input:size(1)))
self.gradInput:neg()
self.gradInput:add(gradOutput)
elseif input:dim() == 2 then
self.maxbuffer:sum(gradOutput, 2)
self.gradInput:copy(self.output)
self.gradInput:exp()
self.gradInput:cmul(self.maxbuffer:expand(input:size(1), input:size(2)))
self.gradInput:neg()
self.gradInput:add(gradOutput)
else
error('LogSoftMax expects 1-d or 2-d tensor currently')
end
return self.gradInput
end