Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6c87a2e9
提交
6c87a2e9
authored
4月 11, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
1a7851d7
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
11 行增加
和
11 行删除
+11
-11
bn.py
theano/tensor/nnet/bn.py
+11
-11
没有找到文件。
theano/tensor/nnet/bn.py
浏览文件 @
6c87a2e9
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
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论