Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
60ec46ad
提交
60ec46ad
authored
1月 18, 2014
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add tutorial example in the tests.
上级
760ff1f8
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
82 行增加
和
1 行删除
+82
-1
test_tutorial.py
theano/tests/test_tutorial.py
+82
-1
没有找到文件。
theano/tests/test_tutorial.py
浏览文件 @
60ec46ad
...
@@ -9,7 +9,9 @@ import numpy
...
@@ -9,7 +9,9 @@ import numpy
from
numpy
import
array
from
numpy
import
array
from
theano
import
config
from
theano
import
config
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano.sandbox.rng_mrg
import
MRG_RandomStreams
from
theano.tensor.shared_randomstreams
import
RandomStreams
class
T_extending
(
unittest
.
TestCase
):
class
T_extending
(
unittest
.
TestCase
):
...
@@ -650,7 +652,86 @@ class T_examples(unittest.TestCase):
...
@@ -650,7 +652,86 @@ class T_examples(unittest.TestCase):
rng
.
set_state
(
state_after_v0
)
rng
.
set_state
(
state_after_v0
)
rv_u
.
rng
.
set_value
(
rng
,
borrow
=
True
)
rv_u
.
rng
.
set_value
(
rng
,
borrow
=
True
)
v2
=
f
()
# v2 != v1
v2
=
f
()
# v2 != v1
v3
=
f
()
# v3 == v1
assert
numpy
.
all
(
v1
!=
v2
)
assert
numpy
.
all
(
v1
!=
v2
)
assert
numpy
.
all
(
v1
==
v3
)
def
test_copy_random_state
(
self
):
class
Graph
():
def
__init__
(
self
,
seed
=
123
):
self
.
rng
=
RandomStreams
(
seed
)
self
.
y
=
self
.
rng
.
uniform
(
size
=
(
1
,))
g1
=
Graph
(
seed
=
123
)
f1
=
theano
.
function
([],
g1
.
y
)
g2
=
Graph
(
seed
=
987
)
f2
=
theano
.
function
([],
g2
.
y
)
#print 'By default, the two functions are out of sync.'
v1
=
f1
()
v2
=
f2
()
def
copy_random_state
(
g1
,
g2
):
if
isinstance
(
g1
.
rng
,
MRG_RandomStreams
):
g2
.
rng
.
rstate
=
g1
.
rng
.
rstate
for
(
su1
,
su2
)
in
zip
(
g1
.
rng
.
state_updates
,
g2
.
rng
.
state_updates
):
su2
[
0
]
.
set_value
(
su1
[
0
]
.
get_value
())
#print 'We now copy the state of the theano random number generators.'
copy_random_state
(
g1
,
g2
)
v3
=
f1
()
v4
=
f2
()
assert
numpy
.
allclose
(
v1
,
0.72803009
)
assert
numpy
.
allclose
(
v2
,
0.55056769
)
assert
numpy
.
allclose
(
v3
,
0.59044123
)
assert
numpy
.
allclose
(
v4
,
0.59044123
)
def
test_examples_real_example
(
self
):
rng
=
numpy
.
random
N
=
400
feats
=
784
D
=
(
rng
.
randn
(
N
,
feats
),
rng
.
randint
(
size
=
N
,
low
=
0
,
high
=
2
))
training_steps
=
10000
# Declare Theano symbolic variables
x
=
T
.
matrix
(
"x"
)
y
=
T
.
vector
(
"y"
)
w
=
theano
.
shared
(
rng
.
randn
(
feats
),
name
=
"w"
)
b
=
theano
.
shared
(
0.
,
name
=
"b"
)
print
"Initial model:"
print
w
.
get_value
(),
b
.
get_value
()
# Construct Theano expression graph
p_1
=
1
/
(
1
+
T
.
exp
(
-
T
.
dot
(
x
,
w
)
-
b
))
# Probability that target = 1
prediction
=
p_1
>
0.5
# The prediction thresholded
xent
=
-
y
*
T
.
log
(
p_1
)
-
(
1
-
y
)
*
T
.
log
(
1
-
p_1
)
# Cross-entropy loss function
cost
=
xent
.
mean
()
+
0.01
*
(
w
**
2
)
.
sum
()
# The cost to minimize
gw
,
gb
=
T
.
grad
(
cost
,
[
w
,
b
])
# Compute the gradient of the cost
# (we shall return to this in a
# following section of this tutorial)
# Compile
train
=
theano
.
function
(
inputs
=
[
x
,
y
],
outputs
=
[
prediction
,
xent
],
updates
=
((
w
,
w
-
0.1
*
gw
),
(
b
,
b
-
0.1
*
gb
)))
predict
=
theano
.
function
(
inputs
=
[
x
],
outputs
=
prediction
)
# Train
for
i
in
range
(
training_steps
):
pred
,
err
=
train
(
D
[
0
],
D
[
1
])
print
"Final model:"
print
w
.
get_value
(),
b
.
get_value
()
print
"target values for D:"
,
D
[
1
]
print
"prediction on D:"
,
predict
(
D
[
0
])
# A user reported that this happened on the mailig list.
assert
not
numpy
.
isnan
(
b
.
get_value
())
.
any
()
assert
not
numpy
.
isnan
(
w
.
get_value
())
.
any
()
class
T_aliasing
(
unittest
.
TestCase
):
class
T_aliasing
(
unittest
.
TestCase
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论