Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e79c4e4c
提交
e79c4e4c
authored
4月 11, 2017
作者:
Frédéric Bastien
提交者:
GitHub
4月 11, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5835 from Amrithasuresh/master
Updated numpy as np #4218
上级
d0524fe5
7168c81b
隐藏空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
110 行增加
和
111 行删除
+110
-111
Conv3D.py
theano/tensor/nnet/Conv3D.py
+4
-4
ConvGrad3D.py
theano/tensor/nnet/ConvGrad3D.py
+2
-2
ConvTransp3D.py
theano/tensor/nnet/ConvTransp3D.py
+7
-7
abstract_conv.py
theano/tensor/nnet/abstract_conv.py
+22
-23
blocksparse.py
theano/tensor/nnet/blocksparse.py
+5
-5
bn.py
theano/tensor/nnet/bn.py
+11
-11
conv.py
theano/tensor/nnet/conv.py
+19
-19
neighbours.py
theano/tensor/nnet/neighbours.py
+2
-2
nnet.py
theano/tensor/nnet/nnet.py
+26
-26
sigm.py
theano/tensor/nnet/sigm.py
+12
-12
没有找到文件。
theano/tensor/nnet/Conv3D.py
浏览文件 @
e79c4e4c
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
as
N
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
theano
import
theano
...
@@ -407,7 +407,7 @@ class Conv3D(theano.Op):
...
@@ -407,7 +407,7 @@ class Conv3D(theano.Op):
long long Vposl = Vpos;
long long Vposl = Vpos;
for (int m = 0; m < filterDur; m++) {
for (int m = 0; m < filterDur; m++) {
//H[i,r,c,t,:] +=
N
.dot(W[:,k,l,m,:],V[i,dr*r+k,dc*c+l,dt*t+m,:])
//H[i,r,c,t,:] +=
np
.dot(W[:,k,l,m,:],V[i,dr*r+k,dc*c+l,dt*t+m,:])
//note: changing the weights so that outputChannels and inputChannels were the last two rather than
//note: changing the weights so that outputChannels and inputChannels were the last two rather than
...
@@ -619,8 +619,8 @@ def computeH(V, W, b, d):
...
@@ -619,8 +619,8 @@ def computeH(V, W, b, d):
outputWidth
=
int
((
vidWidth
-
filterWidth
)
/
dy
)
+
1
outputWidth
=
int
((
vidWidth
-
filterWidth
)
/
dy
)
+
1
outputDur
=
int
((
vidDur
-
filterDur
)
/
dt
)
+
1
outputDur
=
int
((
vidDur
-
filterDur
)
/
dt
)
+
1
H
=
N
.
zeros
((
batchSize
,
outputHeight
,
H
=
np
.
zeros
((
batchSize
,
outputHeight
,
outputWidth
,
outputDur
,
outputChannels
),
dtype
=
V
.
dtype
)
outputWidth
,
outputDur
,
outputChannels
),
dtype
=
V
.
dtype
)
# H[i,j,x,y,t] = b_j + sum_k sum_l sum_m sum_z W[j,z,k,l,m] V[i,z, dx*x+k,dy*y+l,dt*t+m]
# H[i,j,x,y,t] = b_j + sum_k sum_l sum_m sum_z W[j,z,k,l,m] V[i,z, dx*x+k,dy*y+l,dt*t+m]
for
i
in
xrange
(
0
,
H
.
shape
[
0
]):
for
i
in
xrange
(
0
,
H
.
shape
[
0
]):
...
...
theano/tensor/nnet/ConvGrad3D.py
浏览文件 @
e79c4e4c
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
from
six.moves
import
xrange
from
six.moves
import
xrange
import
numpy
as
N
import
numpy
as
np
import
theano
import
theano
from
theano.tensor
import
basic
as
T
from
theano.tensor
import
basic
as
T
...
@@ -71,7 +71,7 @@ class ConvGrad3D(theano.Op):
...
@@ -71,7 +71,7 @@ class ConvGrad3D(theano.Op):
assert
V
.
shape
[
0
]
==
batchSize
assert
V
.
shape
[
0
]
==
batchSize
dr
,
dc
,
dt
=
d
dr
,
dc
,
dt
=
d
dCdW
=
N
.
zeros
(
WShape
,
dtype
=
V
.
dtype
)
dCdW
=
np
.
zeros
(
WShape
,
dtype
=
V
.
dtype
)
# print 'computing output of shape '+str(WShape)
# print 'computing output of shape '+str(WShape)
...
...
theano/tensor/nnet/ConvTransp3D.py
浏览文件 @
e79c4e4c
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
as
N
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
theano
import
theano
...
@@ -385,8 +385,8 @@ def computeR(W, b, d, H, Rshape=None):
...
@@ -385,8 +385,8 @@ def computeR(W, b, d, H, Rshape=None):
# print "video size: "+str((videoHeight, videoWidth, videoDur))
# print "video size: "+str((videoHeight, videoWidth, videoDur))
R
=
N
.
zeros
((
batchSize
,
videoHeight
,
R
=
np
.
zeros
((
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
dtype
=
H
.
dtype
)
videoWidth
,
videoDur
,
inputChannels
),
dtype
=
H
.
dtype
)
# R[i,j,r,c,t] = b_j + sum_{rc,rk | d \circ rc + rk = r} sum_{cc,ck | ...} sum_{tc,tk | ...} sum_k W[k, j, rk, ck, tk] * H[i,k,rc,cc,tc]
# R[i,j,r,c,t] = b_j + sum_{rc,rk | d \circ rc + rk = r} sum_{cc,ck | ...} sum_{tc,tk | ...} sum_k W[k, j, rk, ck, tk] * H[i,k,rc,cc,tc]
for
i
in
xrange
(
0
,
batchSize
):
for
i
in
xrange
(
0
,
batchSize
):
...
@@ -399,12 +399,12 @@ def computeR(W, b, d, H, Rshape=None):
...
@@ -399,12 +399,12 @@ def computeR(W, b, d, H, Rshape=None):
for
t
in
xrange
(
0
,
videoDur
):
for
t
in
xrange
(
0
,
videoDur
):
R
[
i
,
r
,
c
,
t
,
j
]
=
b
[
j
]
R
[
i
,
r
,
c
,
t
,
j
]
=
b
[
j
]
ftc
=
max
([
0
,
int
(
N
.
ceil
(
ftc
=
max
([
0
,
int
(
np
.
ceil
(
float
(
t
-
filterDur
+
1
)
/
float
(
dt
)))])
float
(
t
-
filterDur
+
1
)
/
float
(
dt
)))])
fcc
=
max
([
0
,
int
(
N
.
ceil
(
fcc
=
max
([
0
,
int
(
np
.
ceil
(
float
(
c
-
filterWidth
+
1
)
/
float
(
dc
)))])
float
(
c
-
filterWidth
+
1
)
/
float
(
dc
)))])
rc
=
max
([
0
,
int
(
N
.
ceil
(
rc
=
max
([
0
,
int
(
np
.
ceil
(
float
(
r
-
filterHeight
+
1
)
/
float
(
dr
)))])
float
(
r
-
filterHeight
+
1
)
/
float
(
dr
)))])
while
rc
<
outputHeight
:
while
rc
<
outputHeight
:
rk
=
r
-
rc
*
dr
rk
=
r
-
rc
*
dr
...
@@ -423,7 +423,7 @@ def computeR(W, b, d, H, Rshape=None):
...
@@ -423,7 +423,7 @@ def computeR(W, b, d, H, Rshape=None):
if
tk
<
0
:
if
tk
<
0
:
break
break
R
[
i
,
r
,
c
,
t
,
j
]
+=
N
.
dot
(
R
[
i
,
r
,
c
,
t
,
j
]
+=
np
.
dot
(
W
[:,
rk
,
ck
,
tk
,
j
],
H
[
i
,
rc
,
cc
,
tc
,
:])
W
[:,
rk
,
ck
,
tk
,
j
],
H
[
i
,
rc
,
cc
,
tc
,
:])
tc
+=
1
tc
+=
1
...
...
theano/tensor/nnet/abstract_conv.py
浏览文件 @
e79c4e4c
...
@@ -17,7 +17,6 @@ from theano.gof import Apply, Op
...
@@ -17,7 +17,6 @@ from theano.gof import Apply, Op
from
six.moves
import
xrange
from
six.moves
import
xrange
import
warnings
import
warnings
import
numpy
import
numpy
as
np
import
numpy
as
np
try
:
try
:
...
@@ -69,7 +68,7 @@ def get_conv_output_shape(image_shape, kernel_shape,
...
@@ -69,7 +68,7 @@ def get_conv_output_shape(image_shape, kernel_shape,
nkern
,
kshp
=
kernel_shape
[
0
],
kernel_shape
[
2
:]
nkern
,
kshp
=
kernel_shape
[
0
],
kernel_shape
[
2
:]
if
filter_dilation
is
None
:
if
filter_dilation
is
None
:
filter_dilation
=
n
umpy
.
ones
(
len
(
subsample
),
dtype
=
'int'
)
filter_dilation
=
n
p
.
ones
(
len
(
subsample
),
dtype
=
'int'
)
if
isinstance
(
border_mode
,
tuple
):
if
isinstance
(
border_mode
,
tuple
):
out_shp
=
tuple
(
get_conv_shape_1axis
(
out_shp
=
tuple
(
get_conv_shape_1axis
(
...
@@ -181,7 +180,7 @@ def get_conv_gradweights_shape(image_shape, top_shape,
...
@@ -181,7 +180,7 @@ def get_conv_gradweights_shape(image_shape, top_shape,
nchan
,
topshp
=
top_shape
[
1
],
top_shape
[
2
:]
nchan
,
topshp
=
top_shape
[
1
],
top_shape
[
2
:]
if
filter_dilation
is
None
:
if
filter_dilation
is
None
:
filter_dilation
=
n
umpy
.
ones
(
len
(
subsample
),
dtype
=
'int'
)
filter_dilation
=
n
p
.
ones
(
len
(
subsample
),
dtype
=
'int'
)
if
isinstance
(
border_mode
,
tuple
):
if
isinstance
(
border_mode
,
tuple
):
out_shp
=
tuple
(
get_conv_gradweights_shape_1axis
(
out_shp
=
tuple
(
get_conv_gradweights_shape_1axis
(
...
@@ -286,7 +285,7 @@ def get_conv_gradinputs_shape(kernel_shape, top_shape,
...
@@ -286,7 +285,7 @@ def get_conv_gradinputs_shape(kernel_shape, top_shape,
nkern
,
kshp
=
kernel_shape
[
1
],
kernel_shape
[
2
:]
nkern
,
kshp
=
kernel_shape
[
1
],
kernel_shape
[
2
:]
if
filter_dilation
is
None
:
if
filter_dilation
is
None
:
filter_dilation
=
n
umpy
.
ones
(
len
(
subsample
),
dtype
=
'int'
)
filter_dilation
=
n
p
.
ones
(
len
(
subsample
),
dtype
=
'int'
)
if
isinstance
(
border_mode
,
tuple
):
if
isinstance
(
border_mode
,
tuple
):
out_shp
=
tuple
(
get_conv_gradinputs_shape_1axis
(
out_shp
=
tuple
(
get_conv_gradinputs_shape_1axis
(
...
@@ -1508,11 +1507,11 @@ class BaseAbstractConv(Op):
...
@@ -1508,11 +1507,11 @@ class BaseAbstractConv(Op):
out_shape
=
get_conv_output_shape
(
img
.
shape
,
kern
.
shape
,
out_shape
=
get_conv_output_shape
(
img
.
shape
,
kern
.
shape
,
mode
,
[
1
]
*
self
.
convdim
,
dilation
)
mode
,
[
1
]
*
self
.
convdim
,
dilation
)
out
=
n
umpy
.
zeros
(
out_shape
,
dtype
=
img
.
dtype
)
out
=
n
p
.
zeros
(
out_shape
,
dtype
=
img
.
dtype
)
dil_kern_shp
=
kern
.
shape
[:
-
self
.
convdim
]
+
tuple
(
dil_kern_shp
=
kern
.
shape
[:
-
self
.
convdim
]
+
tuple
(
(
kern
.
shape
[
-
self
.
convdim
+
i
]
-
1
)
*
dilation
[
i
]
+
1
(
kern
.
shape
[
-
self
.
convdim
+
i
]
-
1
)
*
dilation
[
i
]
+
1
for
i
in
range
(
self
.
convdim
))
for
i
in
range
(
self
.
convdim
))
dilated_kern
=
n
umpy
.
zeros
(
dil_kern_shp
,
dtype
=
kern
.
dtype
)
dilated_kern
=
n
p
.
zeros
(
dil_kern_shp
,
dtype
=
kern
.
dtype
)
dilated_kern
[(
slice
(
None
),
slice
(
None
))
+
dilated_kern
[(
slice
(
None
),
slice
(
None
))
+
tuple
(
slice
(
None
,
None
,
dilation
[
i
])
for
i
in
range
(
self
.
convdim
))
tuple
(
slice
(
None
,
None
,
dilation
[
i
])
for
i
in
range
(
self
.
convdim
))
]
=
kern
]
=
kern
...
@@ -1522,7 +1521,7 @@ class BaseAbstractConv(Op):
...
@@ -1522,7 +1521,7 @@ class BaseAbstractConv(Op):
bval
=
_bvalfromboundary
(
'fill'
)
bval
=
_bvalfromboundary
(
'fill'
)
with
warnings
.
catch_warnings
():
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
'ignore'
,
n
umpy
.
ComplexWarning
)
warnings
.
simplefilter
(
'ignore'
,
n
p
.
ComplexWarning
)
for
b
in
xrange
(
img
.
shape
[
0
]):
for
b
in
xrange
(
img
.
shape
[
0
]):
for
n
in
xrange
(
kern
.
shape
[
0
]):
for
n
in
xrange
(
kern
.
shape
[
0
]):
for
im0
in
xrange
(
img
.
shape
[
1
]):
for
im0
in
xrange
(
img
.
shape
[
1
]):
...
@@ -1592,8 +1591,8 @@ class AbstractConv(BaseAbstractConv):
...
@@ -1592,8 +1591,8 @@ class AbstractConv(BaseAbstractConv):
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
img
,
kern
=
inp
img
,
kern
=
inp
img
=
n
umpy
.
asarray
(
img
)
img
=
n
p
.
asarray
(
img
)
kern
=
n
umpy
.
asarray
(
kern
)
kern
=
n
p
.
asarray
(
kern
)
dil_kernshp
=
tuple
((
kern
.
shape
[
2
+
i
]
-
1
)
*
self
.
filter_dilation
[
i
]
+
1
dil_kernshp
=
tuple
((
kern
.
shape
[
2
+
i
]
-
1
)
*
self
.
filter_dilation
[
i
]
+
1
for
i
in
range
(
self
.
convdim
))
for
i
in
range
(
self
.
convdim
))
o
,
=
out_
o
,
=
out_
...
@@ -1613,10 +1612,10 @@ class AbstractConv(BaseAbstractConv):
...
@@ -1613,10 +1612,10 @@ class AbstractConv(BaseAbstractConv):
if
isinstance
(
mode
,
tuple
):
if
isinstance
(
mode
,
tuple
):
pad
=
tuple
(
int
(
mode
[
i
])
for
i
in
range
(
self
.
convdim
))
pad
=
tuple
(
int
(
mode
[
i
])
for
i
in
range
(
self
.
convdim
))
mode
=
"valid"
mode
=
"valid"
new_img
=
n
umpy
.
zeros
((
img
.
shape
[
0
],
img
.
shape
[
1
])
+
new_img
=
n
p
.
zeros
((
img
.
shape
[
0
],
img
.
shape
[
1
])
+
tuple
(
img
.
shape
[
i
+
2
]
+
2
*
pad
[
i
]
tuple
(
img
.
shape
[
i
+
2
]
+
2
*
pad
[
i
]
for
i
in
range
(
self
.
convdim
)),
for
i
in
range
(
self
.
convdim
)),
dtype
=
img
.
dtype
)
dtype
=
img
.
dtype
)
new_img
[(
slice
(
None
),
slice
(
None
))
+
new_img
[(
slice
(
None
),
slice
(
None
))
+
tuple
(
slice
(
pad
[
i
],
img
.
shape
[
i
+
2
]
+
pad
[
i
])
tuple
(
slice
(
pad
[
i
],
img
.
shape
[
i
+
2
]
+
pad
[
i
])
for
i
in
range
(
self
.
convdim
))]
=
img
for
i
in
range
(
self
.
convdim
))]
=
img
...
@@ -1809,8 +1808,8 @@ class AbstractConv_gradWeights(BaseAbstractConv):
...
@@ -1809,8 +1808,8 @@ class AbstractConv_gradWeights(BaseAbstractConv):
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
img
,
topgrad
,
shape
=
inp
img
,
topgrad
,
shape
=
inp
img
=
n
umpy
.
asarray
(
img
)
img
=
n
p
.
asarray
(
img
)
topgrad
=
n
umpy
.
asarray
(
topgrad
)
topgrad
=
n
p
.
asarray
(
topgrad
)
o
,
=
out_
o
,
=
out_
...
@@ -1833,10 +1832,10 @@ class AbstractConv_gradWeights(BaseAbstractConv):
...
@@ -1833,10 +1832,10 @@ class AbstractConv_gradWeights(BaseAbstractConv):
pad
=
tuple
(
int
(
mode
[
i
])
for
i
in
range
(
self
.
convdim
))
pad
=
tuple
(
int
(
mode
[
i
])
for
i
in
range
(
self
.
convdim
))
mode
=
"valid"
mode
=
"valid"
new_img
=
n
umpy
.
zeros
((
img
.
shape
[
0
],
img
.
shape
[
1
])
+
new_img
=
n
p
.
zeros
((
img
.
shape
[
0
],
img
.
shape
[
1
])
+
tuple
(
img
.
shape
[
i
+
2
]
+
2
*
pad
[
i
]
tuple
(
img
.
shape
[
i
+
2
]
+
2
*
pad
[
i
]
for
i
in
range
(
self
.
convdim
)),
for
i
in
range
(
self
.
convdim
)),
dtype
=
img
.
dtype
)
dtype
=
img
.
dtype
)
new_img
[(
slice
(
None
),
slice
(
None
))
+
new_img
[(
slice
(
None
),
slice
(
None
))
+
tuple
(
slice
(
pad
[
i
],
img
.
shape
[
i
+
2
]
+
pad
[
i
])
tuple
(
slice
(
pad
[
i
],
img
.
shape
[
i
+
2
]
+
pad
[
i
])
for
i
in
range
(
self
.
convdim
))]
=
img
for
i
in
range
(
self
.
convdim
))]
=
img
...
@@ -1846,7 +1845,7 @@ class AbstractConv_gradWeights(BaseAbstractConv):
...
@@ -1846,7 +1845,7 @@ class AbstractConv_gradWeights(BaseAbstractConv):
new_shape
=
((
topgrad
.
shape
[
0
],
topgrad
.
shape
[
1
])
+
new_shape
=
((
topgrad
.
shape
[
0
],
topgrad
.
shape
[
1
])
+
tuple
(
img
.
shape
[
i
+
2
]
-
dil_shape
[
i
]
+
1
tuple
(
img
.
shape
[
i
+
2
]
-
dil_shape
[
i
]
+
1
for
i
in
range
(
self
.
convdim
)))
for
i
in
range
(
self
.
convdim
)))
new_topgrad
=
n
umpy
.
zeros
((
new_shape
),
dtype
=
topgrad
.
dtype
)
new_topgrad
=
n
p
.
zeros
((
new_shape
),
dtype
=
topgrad
.
dtype
)
new_topgrad
[(
slice
(
None
),
slice
(
None
))
+
new_topgrad
[(
slice
(
None
),
slice
(
None
))
+
tuple
(
slice
(
None
,
None
,
self
.
subsample
[
i
])
tuple
(
slice
(
None
,
None
,
self
.
subsample
[
i
])
for
i
in
range
(
self
.
convdim
))]
=
topgrad
for
i
in
range
(
self
.
convdim
))]
=
topgrad
...
@@ -2049,8 +2048,8 @@ class AbstractConv_gradInputs(BaseAbstractConv):
...
@@ -2049,8 +2048,8 @@ class AbstractConv_gradInputs(BaseAbstractConv):
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
kern
,
topgrad
,
shape
=
inp
kern
,
topgrad
,
shape
=
inp
kern
=
n
umpy
.
asarray
(
kern
)
kern
=
n
p
.
asarray
(
kern
)
topgrad
=
n
umpy
.
asarray
(
topgrad
)
topgrad
=
n
p
.
asarray
(
topgrad
)
o
,
=
out_
o
,
=
out_
mode
=
self
.
border_mode
mode
=
self
.
border_mode
...
@@ -2089,7 +2088,7 @@ class AbstractConv_gradInputs(BaseAbstractConv):
...
@@ -2089,7 +2088,7 @@ class AbstractConv_gradInputs(BaseAbstractConv):
new_shape
=
((
topgrad
.
shape
[
0
],
topgrad
.
shape
[
1
])
+
new_shape
=
((
topgrad
.
shape
[
0
],
topgrad
.
shape
[
1
])
+
tuple
(
shape
[
i
]
+
2
*
pad
[
i
]
-
dil_kernshp
[
i
]
+
1
tuple
(
shape
[
i
]
+
2
*
pad
[
i
]
-
dil_kernshp
[
i
]
+
1
for
i
in
range
(
self
.
convdim
)))
for
i
in
range
(
self
.
convdim
)))
new_topgrad
=
n
umpy
.
zeros
((
new_shape
),
dtype
=
topgrad
.
dtype
)
new_topgrad
=
n
p
.
zeros
((
new_shape
),
dtype
=
topgrad
.
dtype
)
new_topgrad
[(
slice
(
None
),
slice
(
None
))
+
new_topgrad
[(
slice
(
None
),
slice
(
None
))
+
tuple
(
slice
(
None
,
None
,
self
.
subsample
[
i
])
tuple
(
slice
(
None
,
None
,
self
.
subsample
[
i
])
for
i
in
range
(
self
.
convdim
))]
=
topgrad
for
i
in
range
(
self
.
convdim
))]
=
topgrad
...
...
theano/tensor/nnet/blocksparse.py
浏览文件 @
e79c4e4c
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
Op
,
Apply
from
theano
import
Op
,
Apply
...
@@ -106,7 +106,7 @@ class SparseBlockGemv(Op):
...
@@ -106,7 +106,7 @@ class SparseBlockGemv(Op):
for
i
in
range
(
h
.
shape
[
1
]):
for
i
in
range
(
h
.
shape
[
1
]):
inputIdx
=
iIdx
[
b
,
i
]
inputIdx
=
iIdx
[
b
,
i
]
w
=
W
[
inputIdx
,
outputIdx
]
w
=
W
[
inputIdx
,
outputIdx
]
o
[
b
,
j
,
:]
+=
n
umpy
.
dot
(
h
[
b
,
i
],
w
)
o
[
b
,
j
,
:]
+=
n
p
.
dot
(
h
[
b
,
i
],
w
)
out_
[
0
][
0
]
=
o
out_
[
0
][
0
]
=
o
def
infer_shape
(
self
,
node
,
input_shapes
):
def
infer_shape
(
self
,
node
,
input_shapes
):
...
@@ -185,7 +185,7 @@ class SparseBlockOuter(Op):
...
@@ -185,7 +185,7 @@ class SparseBlockOuter(Op):
Which blocks will be computed is specified in `yIdx`.
Which blocks will be computed is specified in `yIdx`.
"""
"""
one
=
theano
.
tensor
.
constant
(
n
umpy
.
asarray
(
1.0
,
dtype
=
'float32'
))
one
=
theano
.
tensor
.
constant
(
n
p
.
asarray
(
1.0
,
dtype
=
'float32'
))
o
=
theano
.
tensor
.
as_tensor_variable
(
o
)
o
=
theano
.
tensor
.
as_tensor_variable
(
o
)
x
=
theano
.
tensor
.
as_tensor_variable
(
x
)
x
=
theano
.
tensor
.
as_tensor_variable
(
x
)
y
=
theano
.
tensor
.
as_tensor_variable
(
y
)
y
=
theano
.
tensor
.
as_tensor_variable
(
y
)
...
@@ -208,8 +208,8 @@ class SparseBlockOuter(Op):
...
@@ -208,8 +208,8 @@ class SparseBlockOuter(Op):
for
b
in
range
(
x
.
shape
[
0
]):
for
b
in
range
(
x
.
shape
[
0
]):
for
i
in
range
(
xIdx
.
shape
[
1
]):
for
i
in
range
(
xIdx
.
shape
[
1
]):
for
j
in
range
(
yIdx
.
shape
[
1
]):
for
j
in
range
(
yIdx
.
shape
[
1
]):
o
[
xIdx
[
b
,
i
],
yIdx
[
b
,
j
]]
+=
n
umpy
.
outer
(
x
[
b
,
i
],
o
[
xIdx
[
b
,
i
],
yIdx
[
b
,
j
]]
+=
n
p
.
outer
(
x
[
b
,
i
],
y
[
b
,
j
,
:])
y
[
b
,
j
,
:])
out_
[
0
][
0
]
=
o
out_
[
0
][
0
]
=
o
...
...
theano/tensor/nnet/bn.py
浏览文件 @
e79c4e4c
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
Apply
,
Op
from
theano
import
Apply
,
Op
from
theano.gof
import
local_optimizer
from
theano.gof
import
local_optimizer
...
@@ -89,7 +89,7 @@ def _prepare_batch_normalization_axes(axes, ndim):
...
@@ -89,7 +89,7 @@ def _prepare_batch_normalization_axes(axes, ndim):
axes
=
(
0
,)
axes
=
(
0
,)
elif
axes
==
'spatial'
:
elif
axes
==
'spatial'
:
axes
=
(
0
,)
+
tuple
(
range
(
2
,
ndim
))
axes
=
(
0
,)
+
tuple
(
range
(
2
,
ndim
))
elif
isinstance
(
axes
,
(
tuple
,
list
,
n
umpy
.
ndarray
)):
elif
isinstance
(
axes
,
(
tuple
,
list
,
n
p
.
ndarray
)):
axes
=
tuple
(
int
(
a
)
for
a
in
axes
)
axes
=
tuple
(
int
(
a
)
for
a
in
axes
)
else
:
else
:
raise
ValueError
(
'invalid axes:
%
s'
,
str
(
axes
))
raise
ValueError
(
'invalid axes:
%
s'
,
str
(
axes
))
...
@@ -215,7 +215,7 @@ def batch_normalization_train(inputs, gamma, beta, axes='per-activation',
...
@@ -215,7 +215,7 @@ def batch_normalization_train(inputs, gamma, beta, axes='per-activation',
# epsilon will be converted to floatX later. we need to check
# epsilon will be converted to floatX later. we need to check
# for rounding errors now, since numpy.float32(1e-5) < 1e-5.
# for rounding errors now, since numpy.float32(1e-5) < 1e-5.
epsilon
=
n
umpy
.
cast
[
theano
.
config
.
floatX
](
epsilon
)
epsilon
=
n
p
.
cast
[
theano
.
config
.
floatX
](
epsilon
)
if
epsilon
<
1e-5
:
if
epsilon
<
1e-5
:
raise
ValueError
(
"epsilon must be at least 1e-5, got
%
s"
%
str
(
epsilon
))
raise
ValueError
(
"epsilon must be at least 1e-5, got
%
s"
%
str
(
epsilon
))
...
@@ -337,7 +337,7 @@ def batch_normalization_test(inputs, gamma, beta, mean, var,
...
@@ -337,7 +337,7 @@ def batch_normalization_test(inputs, gamma, beta, mean, var,
# epsilon will be converted to floatX later. we need to check
# epsilon will be converted to floatX later. we need to check
# for rounding errors now, since numpy.float32(1e-5) < 1e-5.
# for rounding errors now, since numpy.float32(1e-5) < 1e-5.
epsilon
=
n
umpy
.
cast
[
theano
.
config
.
floatX
](
epsilon
)
epsilon
=
n
p
.
cast
[
theano
.
config
.
floatX
](
epsilon
)
if
epsilon
<
1e-5
:
if
epsilon
<
1e-5
:
raise
ValueError
(
"epsilon must be at least 1e-5, got
%
s"
%
str
(
epsilon
))
raise
ValueError
(
"epsilon must be at least 1e-5, got
%
s"
%
str
(
epsilon
))
...
@@ -480,7 +480,7 @@ class AbstractBatchNormTrain(Op):
...
@@ -480,7 +480,7 @@ class AbstractBatchNormTrain(Op):
mean
=
x
.
mean
(
axes
,
keepdims
=
True
)
mean
=
x
.
mean
(
axes
,
keepdims
=
True
)
var
=
x
.
var
(
axes
,
keepdims
=
True
)
var
=
x
.
var
(
axes
,
keepdims
=
True
)
invstd
=
1.0
/
n
umpy
.
sqrt
(
var
+
epsilon
)
invstd
=
1.0
/
n
p
.
sqrt
(
var
+
epsilon
)
out
=
(
x
-
mean
)
*
(
scale
*
invstd
)
+
bias
out
=
(
x
-
mean
)
*
(
scale
*
invstd
)
+
bias
output_storage
[
0
][
0
]
=
out
output_storage
[
0
][
0
]
=
out
...
@@ -493,7 +493,7 @@ class AbstractBatchNormTrain(Op):
...
@@ -493,7 +493,7 @@ class AbstractBatchNormTrain(Op):
mean
*
running_average_factor
mean
*
running_average_factor
output_storage
[
3
][
0
]
=
running_mean
output_storage
[
3
][
0
]
=
running_mean
if
len
(
inputs
)
>
6
:
if
len
(
inputs
)
>
6
:
m
=
float
(
n
umpy
.
prod
(
x
.
shape
)
/
numpy
.
prod
(
scale
.
shape
))
m
=
float
(
n
p
.
prod
(
x
.
shape
)
/
np
.
prod
(
scale
.
shape
))
running_var
=
inputs
[
6
]
running_var
=
inputs
[
6
]
running_var
=
running_var
*
(
1.0
-
running_average_factor
)
+
\
running_var
=
running_var
*
(
1.0
-
running_average_factor
)
+
\
(
m
/
(
m
-
1
))
*
var
*
running_average_factor
(
m
/
(
m
-
1
))
*
var
*
running_average_factor
...
@@ -568,7 +568,7 @@ class AbstractBatchNormInference(Op):
...
@@ -568,7 +568,7 @@ class AbstractBatchNormInference(Op):
def
perform
(
self
,
node
,
inputs
,
output_storage
):
def
perform
(
self
,
node
,
inputs
,
output_storage
):
x
,
scale
,
bias
,
estimated_mean
,
estimated_variance
,
epsilon
=
inputs
x
,
scale
,
bias
,
estimated_mean
,
estimated_variance
,
epsilon
=
inputs
out
=
(
x
-
estimated_mean
)
*
(
scale
/
n
umpy
.
sqrt
(
estimated_variance
+
epsilon
))
+
bias
out
=
(
x
-
estimated_mean
)
*
(
scale
/
n
p
.
sqrt
(
estimated_variance
+
epsilon
))
+
bias
output_storage
[
0
][
0
]
=
out
output_storage
[
0
][
0
]
=
out
...
@@ -607,12 +607,12 @@ class AbstractBatchNormTrainGrad(Op):
...
@@ -607,12 +607,12 @@ class AbstractBatchNormTrainGrad(Op):
raise
ValueError
(
'axes should be less than ndim (<
%
d), but
%
s given'
%
(
x
.
ndim
,
str
(
axes
)))
raise
ValueError
(
'axes should be less than ndim (<
%
d), but
%
s given'
%
(
x
.
ndim
,
str
(
axes
)))
x_diff
=
x
-
x_mean
x_diff
=
x
-
x_mean
mean_dy_x_diff
=
n
umpy
.
mean
(
dy
*
x_diff
,
axis
=
axes
,
keepdims
=
True
)
mean_dy_x_diff
=
n
p
.
mean
(
dy
*
x_diff
,
axis
=
axes
,
keepdims
=
True
)
c
=
(
dy
*
x_invstd
)
-
(
x_diff
*
mean_dy_x_diff
*
(
x_invstd
**
3
))
c
=
(
dy
*
x_invstd
)
-
(
x_diff
*
mean_dy_x_diff
*
(
x_invstd
**
3
))
g_wrt_inputs
=
scale
*
(
c
-
n
umpy
.
mean
(
c
,
axis
=
axes
,
keepdims
=
True
))
g_wrt_inputs
=
scale
*
(
c
-
n
p
.
mean
(
c
,
axis
=
axes
,
keepdims
=
True
))
g_wrt_scale
=
n
umpy
.
sum
(
dy
*
x_invstd
*
x_diff
,
axis
=
axes
,
keepdims
=
True
)
g_wrt_scale
=
n
p
.
sum
(
dy
*
x_invstd
*
x_diff
,
axis
=
axes
,
keepdims
=
True
)
g_wrt_bias
=
n
umpy
.
sum
(
dy
,
axis
=
axes
,
keepdims
=
True
)
g_wrt_bias
=
n
p
.
sum
(
dy
,
axis
=
axes
,
keepdims
=
True
)
output_storage
[
0
][
0
]
=
g_wrt_inputs
output_storage
[
0
][
0
]
=
g_wrt_inputs
output_storage
[
1
][
0
]
=
g_wrt_scale
output_storage
[
1
][
0
]
=
g_wrt_scale
...
...
theano/tensor/nnet/conv.py
浏览文件 @
e79c4e4c
...
@@ -12,7 +12,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -12,7 +12,7 @@ from __future__ import absolute_import, print_function, division
import
logging
import
logging
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
warnings
import
warnings
...
@@ -756,8 +756,8 @@ class ConvOp(OpenMPOp):
...
@@ -756,8 +756,8 @@ class ConvOp(OpenMPOp):
(
1
,
1
))[
2
:]
(
1
,
1
))[
2
:]
if
z
[
0
]
is
None
or
z
[
0
]
.
shape
!=
(
bsize
,
nkern
,)
+
fulloutshp
:
if
z
[
0
]
is
None
or
z
[
0
]
.
shape
!=
(
bsize
,
nkern
,)
+
fulloutshp
:
z
[
0
]
=
n
umpy
.
zeros
((
bsize
,
nkern
,)
+
fulloutshp
,
z
[
0
]
=
n
p
.
zeros
((
bsize
,
nkern
,)
+
fulloutshp
,
dtype
=
img2d
.
dtype
)
dtype
=
img2d
.
dtype
)
zz
=
z
[
0
]
zz
=
z
[
0
]
stacklen
=
imshp
[
0
]
stacklen
=
imshp
[
0
]
...
@@ -767,18 +767,18 @@ class ConvOp(OpenMPOp):
...
@@ -767,18 +767,18 @@ class ConvOp(OpenMPOp):
if
self
.
imshp
!=
self
.
imshp_logical
:
if
self
.
imshp
!=
self
.
imshp_logical
:
# assuming that to get from imshp to imshp logical we insert zeros in missing spots
# assuming that to get from imshp to imshp logical we insert zeros in missing spots
rstride
=
int
(
n
umpy
.
ceil
(
imshp_logical
[
1
]
/
float
(
imshp
[
1
])))
rstride
=
int
(
n
p
.
ceil
(
imshp_logical
[
1
]
/
float
(
imshp
[
1
])))
cstride
=
int
(
n
umpy
.
ceil
(
imshp_logical
[
2
]
/
float
(
imshp
[
2
])))
cstride
=
int
(
n
p
.
ceil
(
imshp_logical
[
2
]
/
float
(
imshp
[
2
])))
buf
=
n
umpy
.
zeros
((
bsize
,)
+
imshp_logical
,
dtype
=
img2d
.
dtype
)
buf
=
n
p
.
zeros
((
bsize
,)
+
imshp_logical
,
dtype
=
img2d
.
dtype
)
buf
[:,
:,
::
rstride
,
::
cstride
]
=
img2d
buf
[:,
:,
::
rstride
,
::
cstride
]
=
img2d
img2d
=
buf
img2d
=
buf
del
buf
,
rstride
,
cstride
del
buf
,
rstride
,
cstride
if
kshp
!=
kshp_logical
:
if
kshp
!=
kshp_logical
:
rstride
=
int
(
n
umpy
.
ceil
(
kshp_logical
[
0
]
/
float
(
kshp
[
0
])))
rstride
=
int
(
n
p
.
ceil
(
kshp_logical
[
0
]
/
float
(
kshp
[
0
])))
cstride
=
int
(
n
umpy
.
ceil
(
kshp_logical
[
1
]
/
float
(
kshp
[
1
])))
cstride
=
int
(
n
p
.
ceil
(
kshp_logical
[
1
]
/
float
(
kshp
[
1
])))
buf
=
n
umpy
.
zeros
((
nkern
,
stacklen
)
+
buf
=
n
p
.
zeros
((
nkern
,
stacklen
)
+
self
.
kshp_logical
,
dtype
=
filtersflipped
.
dtype
)
self
.
kshp_logical
,
dtype
=
filtersflipped
.
dtype
)
if
self
.
kshp_logical_top_aligned
:
if
self
.
kshp_logical_top_aligned
:
roffset
=
coffset
=
0
roffset
=
coffset
=
0
else
:
else
:
...
@@ -796,7 +796,7 @@ class ConvOp(OpenMPOp):
...
@@ -796,7 +796,7 @@ class ConvOp(OpenMPOp):
bval
=
_bvalfromboundary
(
'fill'
)
bval
=
_bvalfromboundary
(
'fill'
)
with
warnings
.
catch_warnings
():
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
'ignore'
,
n
umpy
.
ComplexWarning
)
warnings
.
simplefilter
(
'ignore'
,
n
p
.
ComplexWarning
)
for
b
in
xrange
(
bsize
):
for
b
in
xrange
(
bsize
):
for
n
in
xrange
(
nkern
):
for
n
in
xrange
(
nkern
):
zz
[
b
,
n
,
...
]
.
fill
(
0
)
zz
[
b
,
n
,
...
]
.
fill
(
0
)
...
@@ -808,9 +808,9 @@ class ConvOp(OpenMPOp):
...
@@ -808,9 +808,9 @@ class ConvOp(OpenMPOp):
if
False
:
if
False
:
if
False
and
self
.
out_mode
==
"full"
:
if
False
and
self
.
out_mode
==
"full"
:
img2d2
=
n
umpy
.
zeros
((
bsize
,
stacklen
,
img2d2
=
n
p
.
zeros
((
bsize
,
stacklen
,
imshp
[
1
]
+
2
*
kshp
[
0
]
-
2
,
imshp
[
1
]
+
2
*
kshp
[
0
]
-
2
,
imshp
[
2
]
+
2
*
kshp
[
1
]
-
2
))
imshp
[
2
]
+
2
*
kshp
[
1
]
-
2
))
img2d2
[:,
:,
kshp
[
0
]
-
1
:
kshp
[
0
]
-
1
+
imshp
[
1
],
img2d2
[:,
:,
kshp
[
0
]
-
1
:
kshp
[
0
]
-
1
+
imshp
[
1
],
kshp
[
1
]
-
1
:
kshp
[
1
]
-
1
+
imshp
[
2
]]
=
img2d
kshp
[
1
]
-
1
:
kshp
[
1
]
-
1
+
imshp
[
2
]]
=
img2d
img2d
=
img2d2
img2d
=
img2d2
...
@@ -873,7 +873,7 @@ class ConvOp(OpenMPOp):
...
@@ -873,7 +873,7 @@ class ConvOp(OpenMPOp):
tmp_node
=
theano
.
tensor
.
nnet
.
conv3D
(
tmp_node
=
theano
.
tensor
.
nnet
.
conv3D
(
V
=
shuffled_inputs
,
V
=
shuffled_inputs
,
W
=
shuffled_kerns
,
W
=
shuffled_kerns
,
b
=
theano
.
tensor
.
alloc
(
n
umpy
.
asarray
(
0
,
dtype
=
kerns
.
dtype
),
b
=
theano
.
tensor
.
alloc
(
n
p
.
asarray
(
0
,
dtype
=
kerns
.
dtype
),
kerns
.
shape
[
0
]),
kerns
.
shape
[
0
]),
d
=
(
self
.
dx
,
self
.
dy
,
1
))
d
=
(
self
.
dx
,
self
.
dy
,
1
))
node
=
theano
.
tensor
.
addbroadcast
(
node
=
theano
.
tensor
.
addbroadcast
(
...
@@ -1260,17 +1260,17 @@ if(%(value)s != %(expected)s){
...
@@ -1260,17 +1260,17 @@ if(%(value)s != %(expected)s){
if
all_shape
:
if
all_shape
:
d
[
"self_kshp_logical_r"
]
=
self
.
kshp_logical
[
0
]
d
[
"self_kshp_logical_r"
]
=
self
.
kshp_logical
[
0
]
d
[
"self_kshp_logical_c"
]
=
self
.
kshp_logical
[
1
]
d
[
"self_kshp_logical_c"
]
=
self
.
kshp_logical
[
1
]
d
[
"self_kshp_logical_stride_r"
]
=
int
(
n
umpy
.
ceil
(
d
[
"self_kshp_logical_stride_r"
]
=
int
(
n
p
.
ceil
(
self
.
kshp_logical
[
0
]
/
float
(
self
.
kshp
[
0
])))
self
.
kshp_logical
[
0
]
/
float
(
self
.
kshp
[
0
])))
d
[
"self_kshp_logical_stride_c"
]
=
int
(
n
umpy
.
ceil
(
d
[
"self_kshp_logical_stride_c"
]
=
int
(
n
p
.
ceil
(
self
.
kshp_logical
[
1
]
/
float
(
self
.
kshp
[
1
])))
self
.
kshp_logical
[
1
]
/
float
(
self
.
kshp
[
1
])))
d
[
"self_imshp_logical_r"
]
=
self
.
imshp_logical
[
1
]
d
[
"self_imshp_logical_r"
]
=
self
.
imshp_logical
[
1
]
# numpy.B. 1 not 0
# numpy.B. 1 not 0
d
[
"self_imshp_logical_c"
]
=
self
.
imshp_logical
[
2
]
d
[
"self_imshp_logical_c"
]
=
self
.
imshp_logical
[
2
]
# numpy.B. 2 not 1
# numpy.B. 2 not 1
d
[
"self_imshp_logical_stride_r"
]
=
int
(
n
umpy
.
ceil
(
d
[
"self_imshp_logical_stride_r"
]
=
int
(
n
p
.
ceil
(
self
.
imshp_logical
[
1
]
/
float
(
self
.
imshp
[
1
])))
self
.
imshp_logical
[
1
]
/
float
(
self
.
imshp
[
1
])))
d
[
"self_imshp_logical_stride_c"
]
=
int
(
n
umpy
.
ceil
(
d
[
"self_imshp_logical_stride_c"
]
=
int
(
n
p
.
ceil
(
self
.
imshp_logical
[
2
]
/
float
(
self
.
imshp
[
2
])))
self
.
imshp_logical
[
2
]
/
float
(
self
.
imshp
[
2
])))
if
not
self
.
imshp
[
0
]
==
1
:
if
not
self
.
imshp
[
0
]
==
1
:
d
[
"affectation"
]
=
"+="
d
[
"affectation"
]
=
"+="
...
...
theano/tensor/nnet/neighbours.py
浏览文件 @
e79c4e4c
...
@@ -4,7 +4,7 @@ TODO: implement Images2Neibs.infer_shape() methods
...
@@ -4,7 +4,7 @@ TODO: implement Images2Neibs.infer_shape() methods
"""
"""
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
Op
,
Apply
from
theano
import
Op
,
Apply
...
@@ -224,7 +224,7 @@ class Images2Neibs(Op):
...
@@ -224,7 +224,7 @@ class Images2Neibs(Op):
z_dim0
=
grid_c
*
grid_d
*
ten4
.
shape
[
1
]
*
ten4
.
shape
[
0
]
z_dim0
=
grid_c
*
grid_d
*
ten4
.
shape
[
1
]
*
ten4
.
shape
[
0
]
z_dim1
=
c
*
d
z_dim1
=
c
*
d
z
[
0
]
=
n
umpy
.
empty
((
z_dim0
,
z_dim1
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
z
[
0
]
=
n
p
.
empty
((
z_dim0
,
z_dim1
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
nb_batch
=
ten4
.
shape
[
0
]
nb_batch
=
ten4
.
shape
[
0
]
nb_stack
=
ten4
.
shape
[
1
]
nb_stack
=
ten4
.
shape
[
1
]
...
...
theano/tensor/nnet/nnet.py
浏览文件 @
e79c4e4c
...
@@ -15,7 +15,7 @@ revisited later when all the intermediate part are on the GPU.
...
@@ -15,7 +15,7 @@ revisited later when all the intermediate part are on the GPU.
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
logging
import
logging
import
warnings
import
warnings
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
theano
import
theano
...
@@ -85,7 +85,7 @@ class SoftmaxWithBias(gof.Op):
...
@@ -85,7 +85,7 @@ class SoftmaxWithBias(gof.Op):
if
x
.
size
==
0
:
if
x
.
size
==
0
:
# Numpy doesn't like the max of a zero-sized object.
# Numpy doesn't like the max of a zero-sized object.
output_storage
[
0
][
0
]
=
n
umpy
.
zeros
(
x
.
shape
,
dtype
=
x
.
dtype
)
output_storage
[
0
][
0
]
=
n
p
.
zeros
(
x
.
shape
,
dtype
=
x
.
dtype
)
return
return
x_dtype
=
x
.
dtype
x_dtype
=
x
.
dtype
...
@@ -94,7 +94,7 @@ class SoftmaxWithBias(gof.Op):
...
@@ -94,7 +94,7 @@ class SoftmaxWithBias(gof.Op):
x
=
x
.
astype
(
'float32'
)
x
=
x
.
astype
(
'float32'
)
x_plus_b
=
x
+
b
[
None
,
:]
x_plus_b
=
x
+
b
[
None
,
:]
e_x
=
n
umpy
.
exp
(
x_plus_b
-
x_plus_b
.
max
(
axis
=
1
)[:,
None
])
e_x
=
n
p
.
exp
(
x_plus_b
-
x_plus_b
.
max
(
axis
=
1
)[:,
None
])
e_x
*=
1.0
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
e_x
*=
1.0
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
# default for copy is True and we don't need a copy if the
# default for copy is True and we don't need a copy if the
# data type matches.
# data type matches.
...
@@ -314,7 +314,7 @@ class SoftmaxGrad(gof.Op):
...
@@ -314,7 +314,7 @@ class SoftmaxGrad(gof.Op):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
dy
,
sm
=
input_storage
dy
,
sm
=
input_storage
dx
=
n
umpy
.
zeros_like
(
sm
)
dx
=
n
p
.
zeros_like
(
sm
)
# dx[i,j] = - (\sum_k dy[i,k] sm[i,k]) sm[i,j] + dy[i,j] sm[i,j]
# dx[i,j] = - (\sum_k dy[i,k] sm[i,k]) sm[i,j] + dy[i,j] sm[i,j]
for
i
in
xrange
(
sm
.
shape
[
0
]):
for
i
in
xrange
(
sm
.
shape
[
0
]):
dy_times_sm_i
=
dy
[
i
]
*
sm
[
i
]
dy_times_sm_i
=
dy
[
i
]
*
sm
[
i
]
...
@@ -435,7 +435,7 @@ class Softmax(gof.Op):
...
@@ -435,7 +435,7 @@ class Softmax(gof.Op):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
x
,
=
input_storage
x
,
=
input_storage
e_x
=
n
umpy
.
exp
(
x
-
x
.
max
(
axis
=
1
)[:,
None
])
e_x
=
n
p
.
exp
(
x
-
x
.
max
(
axis
=
1
)[:,
None
])
sm
=
e_x
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
sm
=
e_x
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
output_storage
[
0
][
0
]
=
sm
output_storage
[
0
][
0
]
=
sm
...
@@ -620,8 +620,8 @@ class LogSoftmax(gof.Op):
...
@@ -620,8 +620,8 @@ class LogSoftmax(gof.Op):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
x
,
=
input_storage
x
,
=
input_storage
xdev
=
x
-
x
.
max
(
axis
=
1
)[:,
None
]
xdev
=
x
-
x
.
max
(
axis
=
1
)[:,
None
]
lsm
=
xdev
-
n
umpy
.
log
(
numpy
.
sum
(
numpy
.
exp
(
xdev
),
axis
=
1
,
lsm
=
xdev
-
n
p
.
log
(
np
.
sum
(
np
.
exp
(
xdev
),
axis
=
1
,
keepdims
=
True
))
keepdims
=
True
))
output_storage
[
0
][
0
]
=
lsm
output_storage
[
0
][
0
]
=
lsm
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
...
@@ -1003,27 +1003,27 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
...
@@ -1003,27 +1003,27 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
raise
ValueError
(
'y_idx must have same number of rows as x'
)
raise
ValueError
(
'y_idx must have same number of rows as x'
)
if
any
(
y_idx
<
0
):
if
any
(
y_idx
<
0
):
raise
ValueError
(
"y_i value out of bounds"
)
raise
ValueError
(
"y_i value out of bounds"
)
sm
=
n
umpy
.
zeros_like
(
x
)
# softmax
sm
=
n
p
.
zeros_like
(
x
)
# softmax
nll
=
n
umpy
.
zeros
(
x
.
shape
[
0
],
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
# nll(y | softmax(x))
nll
=
n
p
.
zeros
(
x
.
shape
[
0
],
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
# nll(y | softmax(x))
am
=
n
umpy
.
zeros_like
(
y_idx
)
am
=
n
p
.
zeros_like
(
y_idx
)
for
i
in
xrange
(
sm
.
shape
[
0
]):
for
i
in
xrange
(
sm
.
shape
[
0
]):
# add the bias vector to the i'th row of x
# add the bias vector to the i'th row of x
row
=
x
[
i
]
+
b
row
=
x
[
i
]
+
b
# get the maximum value of i'th row for numerically safe
# get the maximum value of i'th row for numerically safe
# softmax / nll
# softmax / nll
am
[
i
]
=
n
umpy
.
argmax
(
row
)
am
[
i
]
=
n
p
.
argmax
(
row
)
m
=
row
[
am
[
i
]]
m
=
row
[
am
[
i
]]
# compute the unnormalized softmax, and normalization constant
# compute the unnormalized softmax, and normalization constant
sm
[
i
]
=
n
umpy
.
exp
(
row
-
m
)
sm
[
i
]
=
n
p
.
exp
(
row
-
m
)
sum_j
=
n
umpy
.
sum
(
sm
[
i
])
# sum_j(exp(x[j] - m))
sum_j
=
n
p
.
sum
(
sm
[
i
])
# sum_j(exp(x[j] - m))
# normalized our softmax
# normalized our softmax
sm
[
i
]
*=
1.0
/
sum_j
sm
[
i
]
*=
1.0
/
sum_j
# store the nll
# store the nll
nll
[
i
]
=
-
row
[
y_idx
[
i
]]
+
m
+
n
umpy
.
log
(
sum_j
)
nll
[
i
]
=
-
row
[
y_idx
[
i
]]
+
m
+
n
p
.
log
(
sum_j
)
output_storage
[
0
][
0
]
=
nll
output_storage
[
0
][
0
]
=
nll
output_storage
[
1
][
0
]
=
sm
output_storage
[
1
][
0
]
=
sm
...
@@ -1200,7 +1200,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
...
@@ -1200,7 +1200,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
dy
,
sm
,
y_idx
=
input_storage
dy
,
sm
,
y_idx
=
input_storage
if
any
(
y_idx
<
0
):
if
any
(
y_idx
<
0
):
raise
ValueError
(
"y_i value out of bounds"
)
raise
ValueError
(
"y_i value out of bounds"
)
dx
=
n
umpy
.
zeros_like
(
sm
)
dx
=
n
p
.
zeros_like
(
sm
)
if
dy
.
ndim
==
0
:
if
dy
.
ndim
==
0
:
dy
=
dy
[
None
]
dy
=
dy
[
None
]
incr
=
int
(
dy
.
shape
[
0
]
>
1
)
incr
=
int
(
dy
.
shape
[
0
]
>
1
)
...
@@ -1391,7 +1391,7 @@ class CrossentropyCategorical1HotGrad(gof.Op):
...
@@ -1391,7 +1391,7 @@ class CrossentropyCategorical1HotGrad(gof.Op):
def
perform
(
self
,
node
,
inp
,
out
):
def
perform
(
self
,
node
,
inp
,
out
):
g_y
,
coding_dist
,
true_one_of_n
=
inp
g_y
,
coding_dist
,
true_one_of_n
=
inp
g_coding_strg
,
=
out
g_coding_strg
,
=
out
g_coding
=
n
umpy
.
zeros_like
(
coding_dist
)
g_coding
=
n
p
.
zeros_like
(
coding_dist
)
for
i
in
xrange
(
len
(
g_y
)):
for
i
in
xrange
(
len
(
g_y
)):
g_coding
[
i
,
true_one_of_n
[
i
]]
=
(
-
g_y
[
i
]
/
g_coding
[
i
,
true_one_of_n
[
i
]]
=
(
-
g_y
[
i
]
/
coding_dist
[
i
,
true_one_of_n
[
i
]])
coding_dist
[
i
,
true_one_of_n
[
i
]])
...
@@ -1450,9 +1450,9 @@ class CrossentropyCategorical1Hot(gof.Op):
...
@@ -1450,9 +1450,9 @@ class CrossentropyCategorical1Hot(gof.Op):
def
perform
(
self
,
node
,
inp
,
out
):
def
perform
(
self
,
node
,
inp
,
out
):
coding
,
one_of_n
=
inp
coding
,
one_of_n
=
inp
y_out
,
=
out
y_out
,
=
out
y
=
n
umpy
.
zeros_like
(
coding
[:,
0
])
y
=
n
p
.
zeros_like
(
coding
[:,
0
])
for
i
in
xrange
(
len
(
y
)):
for
i
in
xrange
(
len
(
y
)):
y
[
i
]
=
-
n
umpy
.
log
(
coding
[
i
,
one_of_n
[
i
]])
y
[
i
]
=
-
n
p
.
log
(
coding
[
i
,
one_of_n
[
i
]])
y_out
[
0
]
=
y
y_out
[
0
]
=
y
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
...
@@ -1659,9 +1659,9 @@ def _is_const(z, val, approx=False):
...
@@ -1659,9 +1659,9 @@ def _is_const(z, val, approx=False):
except
tensor
.
NotScalarConstantError
:
except
tensor
.
NotScalarConstantError
:
return
False
return
False
if
approx
:
if
approx
:
return
n
umpy
.
allclose
(
maybe
,
val
)
return
n
p
.
allclose
(
maybe
,
val
)
else
:
else
:
return
n
umpy
.
all
(
maybe
==
val
)
return
n
p
.
all
(
maybe
==
val
)
@opt.register_specialize
(
'fast_compile_gpu'
)
@opt.register_specialize
(
'fast_compile_gpu'
)
...
@@ -1792,7 +1792,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1792,7 +1792,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
# set out_grad according to the numerator, it may be divided later
# set out_grad according to the numerator, it may be divided later
# num should be a vector or a scalar
# num should be a vector or a scalar
if
num
.
ndim
==
1
or
n
umpy
.
all
(
num
.
broadcastable
):
if
num
.
ndim
==
1
or
n
p
.
all
(
num
.
broadcastable
):
out_grad
*=
-
num
out_grad
*=
-
num
else
:
else
:
return
return
...
@@ -1818,7 +1818,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1818,7 +1818,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
rest
=
tensor
.
mul
(
*
[
other_inputs
])
rest
=
tensor
.
mul
(
*
[
other_inputs
])
# Check that rest is a vector or a scalar
# Check that rest is a vector or a scalar
if
rest
.
ndim
==
1
or
n
umpy
.
all
(
rest
.
broadcastable
):
if
rest
.
ndim
==
1
or
n
p
.
all
(
rest
.
broadcastable
):
adv_subtensor
=
input
adv_subtensor
=
input
out_grad
/=
rest
out_grad
/=
rest
break
break
...
@@ -2099,14 +2099,14 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
...
@@ -2099,14 +2099,14 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
output
,
=
out
output
,
=
out
new_shape
=
(
mat
.
shape
[
0
],
mat
.
shape
[
1
]
+
1
)
new_shape
=
(
mat
.
shape
[
0
],
mat
.
shape
[
1
]
+
1
)
if
output
[
0
]
is
None
:
if
output
[
0
]
is
None
:
output
[
0
]
=
n
umpy
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
output
[
0
]
=
n
p
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
out
=
output
[
0
]
out
=
output
[
0
]
else
:
else
:
if
output
[
0
]
.
shape
!=
new_shape
:
if
output
[
0
]
.
shape
!=
new_shape
:
try
:
try
:
output
[
0
]
.
resize
(
new_shape
)
output
[
0
]
.
resize
(
new_shape
)
except
Exception
:
except
Exception
:
output
[
0
]
=
n
umpy
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
output
[
0
]
=
n
p
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
out
=
output
[
0
]
out
=
output
[
0
]
out
[:,
0
]
.
fill
(
self
.
val
.
data
)
out
[:,
0
]
.
fill
(
self
.
val
.
data
)
...
@@ -2147,14 +2147,14 @@ class Prepend_scalar_to_each_row(gof.Op):
...
@@ -2147,14 +2147,14 @@ class Prepend_scalar_to_each_row(gof.Op):
output
,
=
out
output
,
=
out
new_shape
=
(
mat
.
shape
[
0
],
mat
.
shape
[
1
]
+
1
)
new_shape
=
(
mat
.
shape
[
0
],
mat
.
shape
[
1
]
+
1
)
if
output
[
0
]
is
None
:
if
output
[
0
]
is
None
:
output
[
0
]
=
n
umpy
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
output
[
0
]
=
n
p
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
out
=
output
[
0
]
out
=
output
[
0
]
else
:
else
:
if
output
[
0
]
.
shape
!=
new_shape
:
if
output
[
0
]
.
shape
!=
new_shape
:
try
:
try
:
output
[
0
]
.
resize
(
new_shape
)
output
[
0
]
.
resize
(
new_shape
)
except
Exception
:
except
Exception
:
output
[
0
]
=
n
umpy
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
output
[
0
]
=
n
p
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
out
=
output
[
0
]
out
=
output
[
0
]
out
[:,
0
]
.
fill
(
val
)
out
[:,
0
]
.
fill
(
val
)
out
[:,
1
:]
=
mat
out
[:,
1
:]
=
mat
...
...
theano/tensor/nnet/sigm.py
浏览文件 @
e79c4e4c
...
@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
import
warnings
import
warnings
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
config
,
gof
,
printing
,
scalar
from
theano
import
config
,
gof
,
printing
,
scalar
...
@@ -41,8 +41,8 @@ class ScalarSigmoid(scalar.UnaryScalarOp):
...
@@ -41,8 +41,8 @@ class ScalarSigmoid(scalar.UnaryScalarOp):
# half-precision (float16), where we want float32.
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
1.0
/
(
1.0
+
n
umpy
.
exp
(
-
x
,
sig
=
'f'
))
return
1.0
/
(
1.0
+
n
p
.
exp
(
-
x
,
sig
=
'f'
))
return
1.0
/
(
1.0
+
n
umpy
.
exp
(
-
x
))
return
1.0
/
(
1.0
+
n
p
.
exp
(
-
x
))
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
return
ScalarSigmoid
.
st_impl
(
x
)
return
ScalarSigmoid
.
st_impl
(
x
)
...
@@ -134,8 +134,8 @@ class ScalarSigmoid(scalar.UnaryScalarOp):
...
@@ -134,8 +134,8 @@ class ScalarSigmoid(scalar.UnaryScalarOp):
This method was used to generate the graph: sigmoid_prec.png in the doc.
This method was used to generate the graph: sigmoid_prec.png in the doc.
"""
"""
data
=
n
umpy
.
arange
(
-
15
,
15
,
.
1
)
data
=
n
p
.
arange
(
-
15
,
15
,
.
1
)
val
=
1
/
(
1
+
n
umpy
.
exp
(
-
data
))
val
=
1
/
(
1
+
n
p
.
exp
(
-
data
))
def
hard_sigmoid
(
x
):
def
hard_sigmoid
(
x
):
return
theano
.
tensor
.
nnet
.
hard_sigmoid
(
x
)
return
theano
.
tensor
.
nnet
.
hard_sigmoid
(
x
)
...
@@ -330,8 +330,8 @@ class ScalarSoftplus(scalar.UnaryScalarOp):
...
@@ -330,8 +330,8 @@ class ScalarSoftplus(scalar.UnaryScalarOp):
# half-precision (float16), where we want float32.
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
log1p
(
numpy
.
exp
(
x
,
sig
=
'f'
))
return
n
p
.
log1p
(
np
.
exp
(
x
,
sig
=
'f'
))
return
n
umpy
.
log1p
(
numpy
.
exp
(
x
))
return
n
p
.
log1p
(
np
.
exp
(
x
))
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
return
ScalarSoftplus
.
static_impl
(
x
)
return
ScalarSoftplus
.
static_impl
(
x
)
...
@@ -399,7 +399,7 @@ def _is_1(expr):
...
@@ -399,7 +399,7 @@ def _is_1(expr):
"""
"""
try
:
try
:
v
=
opt
.
get_scalar_constant_value
(
expr
)
v
=
opt
.
get_scalar_constant_value
(
expr
)
return
n
umpy
.
allclose
(
v
,
1
)
return
n
p
.
allclose
(
v
,
1
)
except
tensor
.
NotScalarConstantError
:
except
tensor
.
NotScalarConstantError
:
return
False
return
False
...
@@ -457,7 +457,7 @@ def is_1pexp(t, only_process_constants=True):
...
@@ -457,7 +457,7 @@ def is_1pexp(t, only_process_constants=True):
scal_sum
=
scalars
[
0
]
scal_sum
=
scalars
[
0
]
for
s
in
scalars
[
1
:]:
for
s
in
scalars
[
1
:]:
scal_sum
=
scal_sum
+
s
scal_sum
=
scal_sum
+
s
if
n
umpy
.
allclose
(
scal_sum
,
1
):
if
n
p
.
allclose
(
scal_sum
,
1
):
return
False
,
maybe_exp
.
owner
.
inputs
[
0
]
return
False
,
maybe_exp
.
owner
.
inputs
[
0
]
# Before 7987b51 there used to be a bug where *any* constant
# Before 7987b51 there used to be a bug where *any* constant
# was considered as if it was equal to 1, and thus this
# was considered as if it was equal to 1, and thus this
...
@@ -569,7 +569,7 @@ def is_neg(var):
...
@@ -569,7 +569,7 @@ def is_neg(var):
for
idx
,
mul_input
in
enumerate
(
apply
.
inputs
):
for
idx
,
mul_input
in
enumerate
(
apply
.
inputs
):
try
:
try
:
constant
=
opt
.
get_scalar_constant_value
(
mul_input
)
constant
=
opt
.
get_scalar_constant_value
(
mul_input
)
is_minus_1
=
n
umpy
.
allclose
(
constant
,
-
1
)
is_minus_1
=
n
p
.
allclose
(
constant
,
-
1
)
except
NotScalarConstantError
:
except
NotScalarConstantError
:
is_minus_1
=
False
is_minus_1
=
False
if
is_minus_1
:
if
is_minus_1
:
...
@@ -968,7 +968,7 @@ def local_inv_1_plus_exp(node):
...
@@ -968,7 +968,7 @@ def local_inv_1_plus_exp(node):
# scalar_inputs are potentially dimshuffled and fill'd scalars
# scalar_inputs are potentially dimshuffled and fill'd scalars
if
len
(
nonconsts
)
==
1
:
if
len
(
nonconsts
)
==
1
:
if
nonconsts
[
0
]
.
owner
and
nonconsts
[
0
]
.
owner
.
op
==
tensor
.
exp
:
if
nonconsts
[
0
]
.
owner
and
nonconsts
[
0
]
.
owner
.
op
==
tensor
.
exp
:
if
scalars
and
n
umpy
.
allclose
(
numpy
.
sum
(
scalars
),
1
):
if
scalars
and
n
p
.
allclose
(
np
.
sum
(
scalars
),
1
):
out
=
opt
.
_fill_chain
(
out
=
opt
.
_fill_chain
(
sigmoid
(
sigmoid
(
tensor
.
neg
(
nonconsts
[
0
]
.
owner
.
inputs
[
0
])),
tensor
.
neg
(
nonconsts
[
0
]
.
owner
.
inputs
[
0
])),
...
@@ -999,7 +999,7 @@ def local_1msigmoid(node):
...
@@ -999,7 +999,7 @@ def local_1msigmoid(node):
val_l
=
opt
.
get_scalar_constant_value
(
sub_l
)
val_l
=
opt
.
get_scalar_constant_value
(
sub_l
)
except
Exception
:
except
Exception
:
return
return
if
n
umpy
.
allclose
(
numpy
.
sum
(
val_l
),
1
):
if
n
p
.
allclose
(
np
.
sum
(
val_l
),
1
):
out
=
sigmoid
(
-
sub_r
.
owner
.
inputs
[
0
])
out
=
sigmoid
(
-
sub_r
.
owner
.
inputs
[
0
])
copy_stack_trace
([
sub_r
,
node
.
outputs
[
0
]],
out
)
copy_stack_trace
([
sub_r
,
node
.
outputs
[
0
]],
out
)
return
[
out
]
return
[
out
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论