Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
13cb7ed2
提交
13cb7ed2
authored
4月 12, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
c918af6e
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
123 行增加
和
123 行删除
+123
-123
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+123
-123
没有找到文件。
theano/tensor/nnet/tests/test_nnet.py
浏览文件 @
13cb7ed2
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
unittest
import
unittest
import
numpy
import
numpy
as
np
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -47,7 +47,7 @@ class T_sigmoid(unittest.TestCase):
...
@@ -47,7 +47,7 @@ class T_sigmoid(unittest.TestCase):
utt
.
seed_rng
()
utt
.
seed_rng
()
def
test_elemwise
(
self
):
def
test_elemwise
(
self
):
utt
.
verify_grad
(
sigmoid
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
sigmoid
,
[
n
p
.
random
.
rand
(
3
,
4
)])
class
T_softplus
(
unittest
.
TestCase
):
class
T_softplus
(
unittest
.
TestCase
):
...
@@ -56,7 +56,7 @@ class T_softplus(unittest.TestCase):
...
@@ -56,7 +56,7 @@ class T_softplus(unittest.TestCase):
utt
.
seed_rng
()
utt
.
seed_rng
()
def
test_elemwise
(
self
):
def
test_elemwise
(
self
):
utt
.
verify_grad
(
softplus
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
softplus
,
[
n
p
.
random
.
rand
(
3
,
4
)])
class
T_Softmax
(
utt
.
InferShapeTester
):
class
T_Softmax
(
utt
.
InferShapeTester
):
...
@@ -64,26 +64,26 @@ class T_Softmax(utt.InferShapeTester):
...
@@ -64,26 +64,26 @@ class T_Softmax(utt.InferShapeTester):
def
test0
(
self
):
def
test0
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)[:,
0
]
return
softmax_op
(
a
)[:,
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test1
(
self
):
def
test1
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)[:,
1
]
return
softmax_op
(
a
)[:,
1
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test2
(
self
):
def
test2
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)[:,
2
]
return
softmax_op
(
a
)[:,
2
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test3
(
self
):
def
test3
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)[:,
3
]
return
softmax_op
(
a
)[:,
3
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
admat_val
=
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
admat_val
=
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
([
admat
],
[
Softmax
()(
admat
)],
self
.
_compile_and_check
([
admat
],
[
Softmax
()(
admat
)],
[
admat_val
],
Softmax
)
[
admat_val
],
Softmax
)
...
@@ -91,13 +91,13 @@ class T_Softmax(utt.InferShapeTester):
...
@@ -91,13 +91,13 @@ class T_Softmax(utt.InferShapeTester):
x
=
T
.
vector
()
x
=
T
.
vector
()
f
=
theano
.
function
([
x
],
softmax_op
(
x
))
f
=
theano
.
function
([
x
],
softmax_op
(
x
))
xv
=
n
umpy
.
random
.
randn
(
6
)
.
astype
(
config
.
floatX
)
xv
=
n
p
.
random
.
randn
(
6
)
.
astype
(
config
.
floatX
)
assert
n
umpy
.
allclose
(
f
(
xv
),
numpy
.
exp
(
xv
)
/
numpy
.
exp
(
xv
)
.
sum
())
assert
n
p
.
allclose
(
f
(
xv
),
np
.
exp
(
xv
)
/
np
.
exp
(
xv
)
.
sum
())
def
test_vector_grad
(
self
):
def
test_vector_grad
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)
return
softmax_op
(
a
)
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
4
)])
class
T_SoftmaxWithBias
(
utt
.
InferShapeTester
):
class
T_SoftmaxWithBias
(
utt
.
InferShapeTester
):
...
@@ -105,32 +105,32 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
...
@@ -105,32 +105,32 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
def
test0
(
self
):
def
test0
(
self
):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
softmax_with_bias
(
a
,
b
)[:,
0
]
return
softmax_with_bias
(
a
,
b
)[:,
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test1
(
self
):
def
test1
(
self
):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
softmax_with_bias
(
a
,
b
)[:,
1
]
return
softmax_with_bias
(
a
,
b
)[:,
1
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test2
(
self
):
def
test2
(
self
):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
softmax_with_bias
(
a
,
b
)[:,
2
]
return
softmax_with_bias
(
a
,
b
)[:,
2
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test3
(
self
):
def
test3
(
self
):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
softmax_with_bias
(
a
,
b
)[:,
3
]
return
softmax_with_bias
(
a
,
b
)[:,
3
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test_broadcast
(
self
):
def
test_broadcast
(
self
):
# test that we don't raise an error during optimization for no good
# test that we don't raise an error during optimization for no good
# reason as softmax_with_bias don't support correctly some/all
# reason as softmax_with_bias don't support correctly some/all
# broadcasted inputs pattern
# broadcasted inputs pattern
initial_W
=
n
umpy
.
asarray
([[
0.1
,
0.1
,
0.1
],
initial_W
=
n
p
.
asarray
([[
0.1
,
0.1
,
0.1
],
[
0.1
,
0.1
,
0.1
],
[
0.1
,
0.1
,
0.1
],
[
0.1
,
0.1
,
0.1
]],
[
0.1
,
0.1
,
0.1
]],
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
...
@@ -148,8 +148,8 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
...
@@ -148,8 +148,8 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
def
test_softmax_with_bias_trace
(
self
):
def
test_softmax_with_bias_trace
(
self
):
a
=
theano
.
shared
(
a
=
theano
.
shared
(
n
umpy
.
random
.
randn
(
3
)
.
astype
(
config
.
floatX
))
n
p
.
random
.
randn
(
3
)
.
astype
(
config
.
floatX
))
b
=
theano
.
shared
(
n
umpy
.
float32
(
numpy
.
random
.
randn
()))
b
=
theano
.
shared
(
n
p
.
float32
(
np
.
random
.
randn
()))
sm
=
T
.
nnet
.
softmax
(
a
+
b
)
sm
=
T
.
nnet
.
softmax
(
a
+
b
)
f
=
theano
.
function
([],
sm
)
f
=
theano
.
function
([],
sm
)
assert
check_stack_trace
(
f
,
ops_to_check
=
'last'
)
assert
check_stack_trace
(
f
,
ops_to_check
=
'last'
)
...
@@ -157,8 +157,8 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
...
@@ -157,8 +157,8 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
admat_val
=
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
admat_val
=
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
advec_val
=
n
umpy
.
random
.
rand
(
4
)
.
astype
(
config
.
floatX
)
advec_val
=
n
p
.
random
.
rand
(
4
)
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
([
admat
,
advec
],
self
.
_compile_and_check
([
admat
,
advec
],
[
SoftmaxWithBias
()(
admat
,
advec
)],
[
SoftmaxWithBias
()(
admat
,
advec
)],
[
admat_val
,
advec_val
],
SoftmaxWithBias
)
[
admat_val
,
advec_val
],
SoftmaxWithBias
)
...
@@ -169,40 +169,40 @@ class T_LogSoftmax(utt.InferShapeTester):
...
@@ -169,40 +169,40 @@ class T_LogSoftmax(utt.InferShapeTester):
def
test0
(
self
):
def
test0
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)[:,
0
]
return
logsoftmax_op
(
a
)[:,
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test1
(
self
):
def
test1
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)[:,
1
]
return
logsoftmax_op
(
a
)[:,
1
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test2
(
self
):
def
test2
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)[:,
2
]
return
logsoftmax_op
(
a
)[:,
2
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test3
(
self
):
def
test3
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)[:,
3
]
return
logsoftmax_op
(
a
)[:,
3
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test_matrix
(
self
):
def
test_matrix
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)
return
logsoftmax_op
(
a
)
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test_vector
(
self
):
def
test_vector
(
self
):
x
=
T
.
vector
()
x
=
T
.
vector
()
f
=
theano
.
function
([
x
],
logsoftmax_op
(
x
))
f
=
theano
.
function
([
x
],
logsoftmax_op
(
x
))
xv
=
n
umpy
.
random
.
randn
(
6
)
.
astype
(
config
.
floatX
)
xv
=
n
p
.
random
.
randn
(
6
)
.
astype
(
config
.
floatX
)
assert
n
umpy
.
allclose
(
f
(
xv
),
assert
n
p
.
allclose
(
f
(
xv
),
n
umpy
.
log
(
numpy
.
exp
(
xv
)
/
numpy
.
exp
(
xv
)
.
sum
()))
n
p
.
log
(
np
.
exp
(
xv
)
/
np
.
exp
(
xv
)
.
sum
()))
def
test_vector_grad
(
self
):
def
test_vector_grad
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)
return
logsoftmax_op
(
a
)
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
4
)])
def
test_allclose
(
self
):
def
test_allclose
(
self
):
m
=
theano
.
config
.
mode
m
=
theano
.
config
.
mode
...
@@ -220,9 +220,9 @@ class T_LogSoftmax(utt.InferShapeTester):
...
@@ -220,9 +220,9 @@ class T_LogSoftmax(utt.InferShapeTester):
grad
=
tensor
.
grad
(
cm2
.
mean
(),
x
)
grad
=
tensor
.
grad
(
cm2
.
mean
(),
x
)
# create some inputs into a softmax that are large and labels
# create some inputs into a softmax that are large and labels
a
=
n
umpy
.
exp
(
10
*
numpy
.
random
.
rand
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
))
a
=
n
p
.
exp
(
10
*
np
.
random
.
rand
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
))
# create some one-hot coded labels
# create some one-hot coded labels
b
=
n
umpy
.
eye
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
)
b
=
n
p
.
eye
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
)
# show equivalence of softmax and exponentiated numerically stable
# show equivalence of softmax and exponentiated numerically stable
# log-softmax
# log-softmax
...
@@ -241,7 +241,7 @@ class T_LogSoftmax(utt.InferShapeTester):
...
@@ -241,7 +241,7 @@ class T_LogSoftmax(utt.InferShapeTester):
# while in the log-softmax case they don't
# while in the log-softmax case they don't
f3
=
theano
.
function
([
x
,
y
],
[
grad
])
f3
=
theano
.
function
([
x
,
y
],
[
grad
])
grad_
=
f3
(
a
,
b
)
grad_
=
f3
(
a
,
b
)
assert
not
n
umpy
.
any
(
numpy
.
isnan
(
grad_
))
assert
not
n
p
.
any
(
np
.
isnan
(
grad_
))
def
test_isclose
(
self
):
def
test_isclose
(
self
):
def
f
(
a
):
def
f
(
a
):
...
@@ -274,8 +274,8 @@ class T_LogSoftmax(utt.InferShapeTester):
...
@@ -274,8 +274,8 @@ class T_LogSoftmax(utt.InferShapeTester):
m
.
check_isfinite
=
False
m
.
check_isfinite
=
False
# some inputs that are large to make the gradient explode in the non
# some inputs that are large to make the gradient explode in the non
# optimized case
# optimized case
a
=
n
umpy
.
exp
(
a
=
n
p
.
exp
(
10
*
n
umpy
.
random
.
rand
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
))
10
*
n
p
.
random
.
rand
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
))
def
myfunc
(
x
):
def
myfunc
(
x
):
sm
=
tensor
.
nnet
.
softmax
(
x
)
sm
=
tensor
.
nnet
.
softmax
(
x
)
...
@@ -317,8 +317,8 @@ class T_SoftmaxGrad(utt.InferShapeTester):
...
@@ -317,8 +317,8 @@ class T_SoftmaxGrad(utt.InferShapeTester):
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
bdmat
=
matrix
()
bdmat
=
matrix
()
admat_val
=
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
admat_val
=
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
bdmat_val
=
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
bdmat_val
=
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
([
admat
,
bdmat
],
[
SoftmaxGrad
()(
admat
,
bdmat
)],
self
.
_compile_and_check
([
admat
,
bdmat
],
[
SoftmaxGrad
()(
admat
,
bdmat
)],
[
admat_val
,
bdmat_val
],
SoftmaxGrad
)
[
admat_val
,
bdmat_val
],
SoftmaxGrad
)
...
@@ -333,29 +333,29 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
...
@@ -333,29 +333,29 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
crossentropy_softmax_1hot_with_bias
(
a
,
b
,
y_idx
)[
0
]
return
crossentropy_softmax_1hot_with_bias
(
a
,
b
,
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test1
(
self
):
def
test1
(
self
):
y_idx
=
[
0
,
1
,
3
]
y_idx
=
[
0
,
1
,
3
]
def
f
(
a
):
def
f
(
a
):
return
crossentropy_softmax_1hot
(
a
,
y_idx
)[
0
]
return
crossentropy_softmax_1hot
(
a
,
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test_vector
(
self
):
def
test_vector
(
self
):
y_idx
=
[
3
]
y_idx
=
[
3
]
def
f
(
a
):
def
f
(
a
):
return
crossentropy_softmax_1hot
(
T
.
shape_padleft
(
a
),
y_idx
)[
0
]
return
crossentropy_softmax_1hot
(
T
.
shape_padleft
(
a
),
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
4
)])
def
test_vectors
(
self
):
def
test_vectors
(
self
):
y_idx
=
[
3
]
y_idx
=
[
3
]
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
crossentropy_softmax_1hot
(
T
.
shape_padleft
(
a
)
+
b
,
y_idx
)[
0
]
return
crossentropy_softmax_1hot
(
T
.
shape_padleft
(
a
)
+
b
,
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
4
),
numpy
.
random
.
rand
(
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
4
),
np
.
random
.
rand
(
4
)])
class
T_CrossentropySoftmax1HotWithBiasDx
(
utt
.
InferShapeTester
):
class
T_CrossentropySoftmax1HotWithBiasDx
(
utt
.
InferShapeTester
):
...
@@ -364,20 +364,20 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
...
@@ -364,20 +364,20 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
def
ff
(
class_dtype
):
def
ff
(
class_dtype
):
def
f
(
sm
):
def
f
(
sm
):
# Class indices
# Class indices
y
=
n
umpy
.
random
.
randint
(
low
=
0
,
high
=
5
,
size
=
10
)
.
astype
(
class_dtype
)
y
=
n
p
.
random
.
randint
(
low
=
0
,
high
=
5
,
size
=
10
)
.
astype
(
class_dtype
)
return
theano
.
tensor
.
nnet
.
crossentropy_softmax_1hot_with_bias_dx
(
return
theano
.
tensor
.
nnet
.
crossentropy_softmax_1hot_with_bias_dx
(
n
umpy
.
random
.
rand
(
10
),
# Gradient w.r.t. NLL.
n
p
.
random
.
rand
(
10
),
# Gradient w.r.t. NLL.
sm
,
# Softmax output.
sm
,
# Softmax output.
y
)
y
)
return
f
return
f
# Build a random softmax output whose rows sum to 1.
# Build a random softmax output whose rows sum to 1.
softmax_output
=
n
umpy
.
random
.
rand
(
10
,
5
)
softmax_output
=
n
p
.
random
.
rand
(
10
,
5
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
for
dtype
in
[
'uint8'
,
'int8'
,
'uint64'
,
'int64'
]:
for
dtype
in
[
'uint8'
,
'int8'
,
'uint64'
,
'int64'
]:
utt
.
verify_grad
(
ff
(
dtype
),
[
softmax_output
])
utt
.
verify_grad
(
ff
(
dtype
),
[
softmax_output
])
def
test1
(
self
):
def
test1
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
softmax_output
=
rng
.
rand
(
10
,
5
)
softmax_output
=
rng
.
rand
(
10
,
5
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
...
@@ -392,7 +392,7 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
...
@@ -392,7 +392,7 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
10
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
10
,
5
)
.
astype
(
config
.
floatX
)
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
...
@@ -407,7 +407,7 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
...
@@ -407,7 +407,7 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
10
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
10
,
5
)
.
astype
(
config
.
floatX
)
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
...
@@ -431,28 +431,28 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
...
@@ -431,28 +431,28 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
# First test gradient when getting a gradient on the NLL output.
# First test gradient when getting a gradient on the NLL output.
def
grad_on_nll_dtype
(
dtype
):
def
grad_on_nll_dtype
(
dtype
):
def
grad_on_nll
(
x
,
b
):
def
grad_on_nll
(
x
,
b
):
y_idx
=
n
umpy
.
random
.
randint
(
low
=
0
,
high
=
n_classes
,
size
=
n_samples
)
.
astype
(
dtype
)
y_idx
=
n
p
.
random
.
randint
(
low
=
0
,
high
=
n_classes
,
size
=
n_samples
)
.
astype
(
dtype
)
return
self
.
op
(
x
,
b
,
y_idx
=
y_idx
)[
0
]
return
self
.
op
(
x
,
b
,
y_idx
=
y_idx
)[
0
]
return
grad_on_nll
return
grad_on_nll
for
dtype
in
[
'uint8'
,
'int8'
,
'uint64'
,
'int64'
]:
for
dtype
in
[
'uint8'
,
'int8'
,
'uint64'
,
'int64'
]:
utt
.
verify_grad
(
grad_on_nll_dtype
(
dtype
),
utt
.
verify_grad
(
grad_on_nll_dtype
(
dtype
),
[
n
umpy
.
random
.
rand
(
n_samples
,
n_classes
),
[
n
p
.
random
.
rand
(
n_samples
,
n_classes
),
n
umpy
.
random
.
rand
(
n_classes
)])
n
p
.
random
.
rand
(
n_classes
)])
# Then test gradient when getting a gradient on the softmax output.
# Then test gradient when getting a gradient on the softmax output.
def
grad_on_softmax
(
x
,
b
):
def
grad_on_softmax
(
x
,
b
):
return
self
.
op
(
x
,
b
,
y_idx
=
n
umpy
.
random
.
randint
(
return
self
.
op
(
x
,
b
,
y_idx
=
n
p
.
random
.
randint
(
low
=
0
,
high
=
n_classes
,
size
=
n_samples
))[
1
]
low
=
0
,
high
=
n_classes
,
size
=
n_samples
))[
1
]
utt
.
verify_grad
(
utt
.
verify_grad
(
grad_on_softmax
,
grad_on_softmax
,
[
n
umpy
.
random
.
rand
(
n_samples
,
n_classes
),
[
n
p
.
random
.
rand
(
n_samples
,
n_classes
),
n
umpy
.
random
.
rand
(
n_classes
)])
n
p
.
random
.
rand
(
n_classes
)])
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
...
@@ -466,7 +466,7 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
...
@@ -466,7 +466,7 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
...
@@ -482,27 +482,27 @@ class T_prepend(utt.InferShapeTester):
...
@@ -482,27 +482,27 @@ class T_prepend(utt.InferShapeTester):
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
y
=
Prepend_scalar_constant_to_each_row
(
4.
)(
x
)
y
=
Prepend_scalar_constant_to_each_row
(
4.
)(
x
)
f
=
theano
.
function
([
x
],
y
)
f
=
theano
.
function
([
x
],
y
)
m
=
n
umpy
.
random
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
m
=
n
p
.
random
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
my
=
f
(
m
)
my
=
f
(
m
)
self
.
assertTrue
(
my
.
shape
==
(
3
,
6
),
my
.
shape
)
self
.
assertTrue
(
my
.
shape
==
(
3
,
6
),
my
.
shape
)
self
.
assertTrue
(
n
umpy
.
all
(
my
[:,
0
]
==
4.0
))
self
.
assertTrue
(
n
p
.
all
(
my
[:,
0
]
==
4.0
))
def
test1
(
self
):
def
test1
(
self
):
"basic functionality"
"basic functionality"
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
y
=
Prepend_scalar_to_each_row
()(
5.
,
x
)
y
=
Prepend_scalar_to_each_row
()(
5.
,
x
)
f
=
theano
.
function
([
x
],
y
)
f
=
theano
.
function
([
x
],
y
)
m
=
n
umpy
.
ones
((
3
,
5
),
dtype
=
"float32"
)
m
=
n
p
.
ones
((
3
,
5
),
dtype
=
"float32"
)
my
=
f
(
m
)
my
=
f
(
m
)
self
.
assertTrue
(
my
.
shape
==
(
3
,
6
))
self
.
assertTrue
(
my
.
shape
==
(
3
,
6
))
self
.
assertTrue
(
n
umpy
.
all
(
my
[:,
0
]
==
5.0
))
self
.
assertTrue
(
n
p
.
all
(
my
[:,
0
]
==
5.0
))
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
adscal
=
scalar
()
adscal
=
scalar
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
adscal_val
=
n
umpy
.
asarray
(
rng
.
rand
(),
dtype
=
config
.
floatX
)
.
item
()
adscal_val
=
n
p
.
asarray
(
rng
.
rand
(),
dtype
=
config
.
floatX
)
.
item
()
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
admat
],
[
admat
],
[
Prepend_scalar_constant_to_each_row
(
adscal_val
)(
admat
)],
[
Prepend_scalar_constant_to_each_row
(
adscal_val
)(
admat
)],
...
@@ -522,7 +522,7 @@ class T_CrossentropyCategorical1HotGrad(utt.InferShapeTester):
...
@@ -522,7 +522,7 @@ class T_CrossentropyCategorical1HotGrad(utt.InferShapeTester):
advec
=
vector
()
advec
=
vector
()
admat
=
matrix
()
admat
=
matrix
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
advec_val
=
rng
.
rand
(
3
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
3
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
alvec_val
=
[
0
,
1
,
0
]
alvec_val
=
[
0
,
1
,
0
]
...
@@ -541,21 +541,21 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -541,21 +541,21 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
op
=
crossentropy_categorical_1hot
op
=
crossentropy_categorical_1hot
xe
=
op
(
x
,
one_of_n
)
xe
=
op
(
x
,
one_of_n
)
f
=
theano
.
function
([
x
,
one_of_n
],
xe
)
f
=
theano
.
function
([
x
,
one_of_n
],
xe
)
x_val
=
n
umpy
.
asarray
(
x_val
=
n
p
.
asarray
(
[[
.
4
,
.
6
,
.
0
],
[
.
1
,
.
8
,
.
1
]],
[[
.
4
,
.
6
,
.
0
],
[
.
1
,
.
8
,
.
1
]],
dtype
=
config
.
floatX
)
dtype
=
config
.
floatX
)
xe_val
=
f
(
x_val
,
[
0
,
1
])
xe_val
=
f
(
x_val
,
[
0
,
1
])
assert
n
umpy
.
allclose
(
xe_val
,
-
numpy
.
log
([
.
4
,
.
8
]))
assert
n
p
.
allclose
(
xe_val
,
-
np
.
log
([
.
4
,
.
8
]))
def
oplike
(
x
):
def
oplike
(
x
):
return
op
(
x
,
[
0
,
1
])
return
op
(
x
,
[
0
,
1
])
tensor
.
verify_grad
(
oplike
,
[
x_val
],
rng
=
n
umpy
.
random
)
tensor
.
verify_grad
(
oplike
,
[
x_val
],
rng
=
n
p
.
random
)
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
alvec_val
=
[
0
,
1
,
0
]
alvec_val
=
[
0
,
1
,
0
]
self
.
_compile_and_check
(
self
.
_compile_and_check
(
...
@@ -775,10 +775,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -775,10 +775,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
,
4
,
1
])
y_val
=
n
p
.
asarray
([
2
,
4
,
1
])
x
=
T
.
matrix
(
'x'
)
x
=
T
.
matrix
(
'x'
)
b
=
T
.
vector
(
'b'
)
b
=
T
.
vector
(
'b'
)
y
=
T
.
lvector
(
'y'
)
y
=
T
.
lvector
(
'y'
)
...
@@ -954,9 +954,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -954,9 +954,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
,
4
,
1
],
dtype
=
'int64'
)
y_val
=
n
p
.
asarray
([
2
,
4
,
1
],
dtype
=
'int64'
)
x
=
T
.
matrix
(
'x'
)
x
=
T
.
matrix
(
'x'
)
y
=
T
.
lvector
(
'y'
)
y
=
T
.
lvector
(
'y'
)
yi
=
T
.
cast
(
y
,
'int32'
)
yi
=
T
.
cast
(
y
,
'int32'
)
...
@@ -1002,9 +1002,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1002,9 +1002,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
])
y_val
=
n
p
.
asarray
([
2
])
x
=
T
.
vector
(
'x'
)
x
=
T
.
vector
(
'x'
)
y
=
T
.
lvector
(
'y'
)
y
=
T
.
lvector
(
'y'
)
...
@@ -1047,10 +1047,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1047,10 +1047,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
])
y_val
=
n
p
.
asarray
([
2
])
x
=
T
.
vector
(
'x'
)
x
=
T
.
vector
(
'x'
)
b
=
T
.
vector
(
'b'
)
b
=
T
.
vector
(
'b'
)
...
@@ -1107,10 +1107,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1107,10 +1107,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
])
y_val
=
n
p
.
asarray
([
2
])
x
=
T
.
vector
(
'x'
)
x
=
T
.
vector
(
'x'
)
b
=
T
.
vector
(
'b'
)
b
=
T
.
vector
(
'b'
)
...
@@ -1169,10 +1169,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1169,10 +1169,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
])
y_val
=
n
p
.
asarray
([
2
])
x
=
T
.
vector
(
'x'
)
x
=
T
.
vector
(
'x'
)
b
=
T
.
vector
(
'b'
)
b
=
T
.
vector
(
'b'
)
...
@@ -1228,9 +1228,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1228,9 +1228,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
,
4
,
1
])
y_val
=
n
p
.
asarray
([
2
,
4
,
1
])
x
=
T
.
matrix
(
'x'
)
x
=
T
.
matrix
(
'x'
)
y
=
T
.
lvector
(
'y'
)
y
=
T
.
lvector
(
'y'
)
a
=
T
.
scalar
(
'a'
)
a
=
T
.
scalar
(
'a'
)
...
@@ -1445,21 +1445,21 @@ def test_asymptotic_32():
...
@@ -1445,21 +1445,21 @@ def test_asymptotic_32():
for
i
,
n
in
enumerate
(
f
.
maker
.
fgraph
.
toposort
()):
for
i
,
n
in
enumerate
(
f
.
maker
.
fgraph
.
toposort
()):
print
(
i
,
n
)
print
(
i
,
n
)
xval
=
n
umpy
.
zeros
((
5
,
5
),
dtype
=
dtype
)
.
astype
(
dtype
)
xval
=
n
p
.
zeros
((
5
,
5
),
dtype
=
dtype
)
.
astype
(
dtype
)
x2val
=
n
umpy
.
zeros
(
5
,
dtype
=
xval
.
dtype
)
.
astype
(
dtype
)
x2val
=
n
p
.
zeros
(
5
,
dtype
=
xval
.
dtype
)
.
astype
(
dtype
)
for
i
in
xrange
(
100
):
for
i
in
xrange
(
100
):
cval
,
gxval
=
f
(
xval
,
n
umpy
.
arange
(
5
),
x2val
)
cval
,
gxval
=
f
(
xval
,
n
p
.
arange
(
5
),
x2val
)
xval
-=
100.3
*
gxval
xval
-=
100.3
*
gxval
# print cval, gxval
# print cval, gxval
assert
cval
==
0
# no problem going to zero error
assert
cval
==
0
# no problem going to zero error
# what about when x gets really big?
# what about when x gets really big?
xval
=
n
umpy
.
zeros
((
5
,
5
),
dtype
=
dtype
)
xval
=
n
p
.
zeros
((
5
,
5
),
dtype
=
dtype
)
x2val
=
n
umpy
.
zeros
(
5
,
dtype
=
xval
.
dtype
)
x2val
=
n
p
.
zeros
(
5
,
dtype
=
xval
.
dtype
)
for
i
in
xrange
(
100
):
for
i
in
xrange
(
100
):
cval
,
gxval
=
f
(
xval
,
n
umpy
.
arange
(
5
),
x2val
)
cval
,
gxval
=
f
(
xval
,
n
p
.
arange
(
5
),
x2val
)
xval
+=
100000.3
*
gxval
xval
+=
100000.3
*
gxval
# print cval, gxval
# print cval, gxval
...
@@ -1480,7 +1480,7 @@ class Test_softmax_opt:
...
@@ -1480,7 +1480,7 @@ class Test_softmax_opt:
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
self
.
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
self
.
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
self
.
mode
=
self
.
mode
.
including
(
'canonicalize'
)
self
.
mode
=
self
.
mode
.
including
(
'canonicalize'
)
...
@@ -1584,7 +1584,7 @@ class Test_softmax_opt:
...
@@ -1584,7 +1584,7 @@ class Test_softmax_opt:
def
test_softmax_graph
():
def
test_softmax_graph
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
theano
.
shared
(
rng
.
normal
(
size
=
(
3
,
4
)))
x
=
theano
.
shared
(
rng
.
normal
(
size
=
(
3
,
4
)))
def
f
(
inputs
):
def
f
(
inputs
):
...
@@ -1595,7 +1595,7 @@ def test_softmax_graph():
...
@@ -1595,7 +1595,7 @@ def test_softmax_graph():
def
test_grad_softmax_grad
():
def
test_grad_softmax_grad
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
theano
.
shared
(
rng
.
normal
(
size
=
(
3
,
4
)))
x
=
theano
.
shared
(
rng
.
normal
(
size
=
(
3
,
4
)))
def
f
(
inputs
):
def
f
(
inputs
):
...
@@ -1621,39 +1621,39 @@ def test_stabilize_log_softmax():
...
@@ -1621,39 +1621,39 @@ def test_stabilize_log_softmax():
# call the function so debug mode can verify the optimized
# call the function so debug mode can verify the optimized
# version matches the unoptimized version
# version matches the unoptimized version
rng
=
n
umpy
.
random
.
RandomState
([
2012
,
8
,
22
])
rng
=
n
p
.
random
.
RandomState
([
2012
,
8
,
22
])
f
(
n
umpy
.
cast
[
config
.
floatX
](
rng
.
randn
(
2
,
3
)))
f
(
n
p
.
cast
[
config
.
floatX
](
rng
.
randn
(
2
,
3
)))
def
test_relu
():
def
test_relu
():
x
=
matrix
(
'x'
)
x
=
matrix
(
'x'
)
seed
=
theano
.
tests
.
unittest_tools
.
fetch_seed
()
seed
=
theano
.
tests
.
unittest_tools
.
fetch_seed
()
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
config
.
floatX
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
config
.
floatX
)
# test the base case, without custom alpha value
# test the base case, without custom alpha value
y
=
relu
(
x
)
.
eval
({
x
:
X
})
y
=
relu
(
x
)
.
eval
({
x
:
X
})
assert
n
umpy
.
allclose
(
y
,
numpy
.
maximum
(
X
,
0
))
assert
n
p
.
allclose
(
y
,
np
.
maximum
(
X
,
0
))
# test for different constant alpha values (also outside of [0, 1])
# test for different constant alpha values (also outside of [0, 1])
for
alpha
in
0
,
0.3
,
1
,
2
,
-
0.3
,
-
1
,
-
2
:
for
alpha
in
0
,
0.3
,
1
,
2
,
-
0.3
,
-
1
,
-
2
:
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
assert
n
umpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
alpha
*
X
))
assert
n
p
.
allclose
(
y
,
np
.
where
(
X
>
0
,
X
,
alpha
*
X
))
# test for variable alpha (scalar, vector and matrix)
# test for variable alpha (scalar, vector and matrix)
for
alpha
in
scalar
(),
vector
(),
matrix
():
for
alpha
in
scalar
(),
vector
(),
matrix
():
# create value for alpha (correct ndim and broadcastable against X)
# create value for alpha (correct ndim and broadcastable against X)
A
=
n
umpy
.
array
(
rng
.
randn
(
*
X
.
shape
[::
-
1
][:
alpha
.
ndim
][::
-
1
]),
A
=
n
p
.
array
(
rng
.
randn
(
*
X
.
shape
[::
-
1
][:
alpha
.
ndim
][::
-
1
]),
dtype
=
config
.
floatX
)
dtype
=
config
.
floatX
)
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
,
alpha
:
A
})
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
,
alpha
:
A
})
assert
n
umpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
A
*
X
),
rtol
=
3e-5
)
assert
n
p
.
allclose
(
y
,
np
.
where
(
X
>
0
,
X
,
A
*
X
),
rtol
=
3e-5
)
# test that for alpha of ndarray don't cause upcast.
# test that for alpha of ndarray don't cause upcast.
x
=
matrix
(
'x'
,
dtype
=
'float32'
)
x
=
matrix
(
'x'
,
dtype
=
'float32'
)
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
'float32'
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
'float32'
)
alpha
=
n
umpy
.
asarray
(
.
123
,
dtype
=
'float32'
)
alpha
=
n
p
.
asarray
(
.
123
,
dtype
=
'float32'
)
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
assert
n
umpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
alpha
*
X
))
assert
n
p
.
allclose
(
y
,
np
.
where
(
X
>
0
,
X
,
alpha
*
X
))
assert
y
.
dtype
==
'float32'
assert
y
.
dtype
==
'float32'
...
@@ -1681,19 +1681,19 @@ def test_h_softmax():
...
@@ -1681,19 +1681,19 @@ def test_h_softmax():
shared
=
theano
.
shared
shared
=
theano
.
shared
# First level of h_softmax
# First level of h_softmax
W1
=
n
umpy
.
asarray
(
numpy
.
random
.
normal
(
W1
=
n
p
.
asarray
(
np
.
random
.
normal
(
size
=
(
input_size
,
h_softmax_level1_size
)),
dtype
=
floatX
)
size
=
(
input_size
,
h_softmax_level1_size
)),
dtype
=
floatX
)
W1
=
shared
(
W1
)
W1
=
shared
(
W1
)
b1
=
shared
(
n
umpy
.
asarray
(
numpy
.
zeros
((
h_softmax_level1_size
,)),
b1
=
shared
(
n
p
.
asarray
(
np
.
zeros
((
h_softmax_level1_size
,)),
dtype
=
floatX
))
dtype
=
floatX
))
# Second level of h_softmax
# Second level of h_softmax
W2
=
n
umpy
.
asarray
(
numpy
.
random
.
normal
(
W2
=
n
p
.
asarray
(
np
.
random
.
normal
(
size
=
(
h_softmax_level1_size
,
input_size
,
h_softmax_level2_size
)),
size
=
(
h_softmax_level1_size
,
input_size
,
h_softmax_level2_size
)),
dtype
=
floatX
)
dtype
=
floatX
)
W2
=
shared
(
W2
)
W2
=
shared
(
W2
)
b2
=
shared
(
b2
=
shared
(
n
umpy
.
asarray
(
numpy
.
zeros
((
h_softmax_level1_size
,
n
p
.
asarray
(
np
.
zeros
((
h_softmax_level1_size
,
h_softmax_level2_size
)),
dtype
=
floatX
))
h_softmax_level2_size
)),
dtype
=
floatX
))
#############
#############
...
@@ -1719,8 +1719,8 @@ def test_h_softmax():
...
@@ -1719,8 +1719,8 @@ def test_h_softmax():
#############
#############
# Test
# Test
#############
#############
x_mat
=
n
umpy
.
random
.
normal
(
size
=
(
batch_size
,
input_size
))
.
astype
(
floatX
)
x_mat
=
n
p
.
random
.
normal
(
size
=
(
batch_size
,
input_size
))
.
astype
(
floatX
)
y_mat
=
n
umpy
.
random
.
randint
(
0
,
output_size
,
batch_size
)
.
astype
(
'int32'
)
y_mat
=
n
p
.
random
.
randint
(
0
,
output_size
,
batch_size
)
.
astype
(
'int32'
)
tg_output
=
fun_output_tg
(
x_mat
,
y_mat
)
tg_output
=
fun_output_tg
(
x_mat
,
y_mat
)
all_outputs
=
fun_output
(
x_mat
)
all_outputs
=
fun_output
(
x_mat
)
...
@@ -1730,23 +1730,23 @@ def test_h_softmax():
...
@@ -1730,23 +1730,23 @@ def test_h_softmax():
# Verifies that the outputs computed by fun_output_tg are the same as those
# Verifies that the outputs computed by fun_output_tg are the same as those
# computed by fun_output.
# computed by fun_output.
utt
.
assert_allclose
(
utt
.
assert_allclose
(
all_outputs
[
n
umpy
.
arange
(
0
,
batch_size
),
y_mat
],
tg_output
)
all_outputs
[
n
p
.
arange
(
0
,
batch_size
),
y_mat
],
tg_output
)
def
test_elu
():
def
test_elu
():
x
=
matrix
(
'x'
)
x
=
matrix
(
'x'
)
seed
=
theano
.
tests
.
unittest_tools
.
fetch_seed
()
seed
=
theano
.
tests
.
unittest_tools
.
fetch_seed
()
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
config
.
floatX
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
config
.
floatX
)
# test the base case, without custom alpha value
# test the base case, without custom alpha value
y
=
elu
(
x
)
.
eval
({
x
:
X
})
y
=
elu
(
x
)
.
eval
({
x
:
X
})
utt
.
assert_allclose
(
y
,
n
umpy
.
where
(
X
>
0
,
X
,
numpy
.
exp
(
X
)
-
1
))
utt
.
assert_allclose
(
y
,
n
p
.
where
(
X
>
0
,
X
,
np
.
exp
(
X
)
-
1
))
# test for different constant alpha values
# test for different constant alpha values
for
alpha
in
1.5
,
2
,
-
1
,
-
1.5
,
-
2
:
for
alpha
in
1.5
,
2
,
-
1
,
-
1.5
,
-
2
:
y
=
elu
(
x
,
alpha
)
.
eval
({
x
:
X
})
y
=
elu
(
x
,
alpha
)
.
eval
({
x
:
X
})
utt
.
assert_allclose
(
y
,
n
umpy
.
where
(
X
>
0
,
X
,
alpha
*
(
numpy
.
exp
(
X
)
-
1
)))
utt
.
assert_allclose
(
y
,
n
p
.
where
(
X
>
0
,
X
,
alpha
*
(
np
.
exp
(
X
)
-
1
)))
def
test_binary_crossentropy_reshape
():
def
test_binary_crossentropy_reshape
():
...
@@ -1759,13 +1759,13 @@ def test_binary_crossentropy_reshape():
...
@@ -1759,13 +1759,13 @@ def test_binary_crossentropy_reshape():
# This only works when "specialize" options are included
# This only works when "specialize" options are included
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'fast_run'
)
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'fast_run'
)
fga
=
theano
.
function
([
a
],
ga
,
mode
=
mode
)
fga
=
theano
.
function
([
a
],
ga
,
mode
=
mode
)
utt
.
assert_allclose
(
fga
(
n
umpy
.
array
([[[[
30.
]]]],
dtype
=
config
.
floatX
)),
utt
.
assert_allclose
(
fga
(
n
p
.
array
([[[[
30.
]]]],
dtype
=
config
.
floatX
)),
n
umpy
.
zeros
((
1
,
1
,
1
,
1
),
dtype
=
config
.
floatX
))
n
p
.
zeros
((
1
,
1
,
1
,
1
),
dtype
=
config
.
floatX
))
SoftsignTester
=
makeBroadcastTester
(
SoftsignTester
=
makeBroadcastTester
(
op
=
softsign
,
op
=
softsign
,
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
inputs
,
inputs
/
(
1.0
+
n
umpy
.
fabs
(
inputs
)))),
inputs
,
inputs
/
(
1.0
+
n
p
.
fabs
(
inputs
)))),
good
=
_good_broadcast_unary_normal_float_no_complex
,
good
=
_good_broadcast_unary_normal_float_no_complex
,
name
=
'SoftsignTester'
,
name
=
'SoftsignTester'
,
)
)
...
@@ -1774,13 +1774,13 @@ SoftsignTester = makeBroadcastTester(
...
@@ -1774,13 +1774,13 @@ SoftsignTester = makeBroadcastTester(
def
test_confusion_matrix
():
def
test_confusion_matrix
():
# Defining numpy implementation of confusion matrix
# Defining numpy implementation of confusion matrix
def
numpy_conf_mat
(
actual
,
pred
):
def
numpy_conf_mat
(
actual
,
pred
):
order
=
n
umpy
.
union1d
(
actual
,
pred
)
order
=
n
p
.
union1d
(
actual
,
pred
)
colA
=
n
umpy
.
matrix
(
actual
)
.
T
colA
=
n
p
.
matrix
(
actual
)
.
T
colP
=
n
umpy
.
matrix
(
pred
)
.
T
colP
=
n
p
.
matrix
(
pred
)
.
T
oneHotA
=
colA
.
__eq__
(
order
)
.
astype
(
'int64'
)
oneHotA
=
colA
.
__eq__
(
order
)
.
astype
(
'int64'
)
oneHotP
=
colP
.
__eq__
(
order
)
.
astype
(
'int64'
)
oneHotP
=
colP
.
__eq__
(
order
)
.
astype
(
'int64'
)
conf_mat
=
n
umpy
.
dot
(
oneHotA
.
T
,
oneHotP
)
conf_mat
=
n
p
.
dot
(
oneHotA
.
T
,
oneHotP
)
conf_mat
=
n
umpy
.
asarray
(
conf_mat
)
conf_mat
=
n
p
.
asarray
(
conf_mat
)
return
[
conf_mat
,
order
]
return
[
conf_mat
,
order
]
x
=
tensor
.
vector
()
x
=
tensor
.
vector
()
...
@@ -1790,8 +1790,8 @@ def test_confusion_matrix():
...
@@ -1790,8 +1790,8 @@ def test_confusion_matrix():
[[
2
,
0
,
2
,
2
,
0
,
1
],
[
0
,
0
,
2
,
2
,
0
,
2
]]]
[[
2
,
0
,
2
,
2
,
0
,
1
],
[
0
,
0
,
2
,
2
,
0
,
2
]]]
for
case
in
list_inputs
:
for
case
in
list_inputs
:
a
=
n
umpy
.
asarray
(
case
[
0
])
a
=
n
p
.
asarray
(
case
[
0
])
b
=
n
umpy
.
asarray
(
case
[
1
])
b
=
n
p
.
asarray
(
case
[
1
])
out_exp
=
numpy_conf_mat
(
a
,
b
)
out_exp
=
numpy_conf_mat
(
a
,
b
)
outs
=
f
(
case
[
0
],
case
[
1
])
outs
=
f
(
case
[
0
],
case
[
1
])
for
exp
,
out
in
zip
(
out_exp
,
outs
):
for
exp
,
out
in
zip
(
out_exp
,
outs
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论