Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
681c5c64
提交
681c5c64
authored
8月 31, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the loop exercice solution.
上级
283b66d5
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
24 行增加
和
30 行删除
+24
-30
loop_solution_1.py
doc/tutorial/loop_solution_1.py
+24
-30
没有找到文件。
doc/tutorial/loop_solution_1.py
浏览文件 @
681c5c64
...
@@ -2,17 +2,15 @@
...
@@ -2,17 +2,15 @@
# Theano tutorial
# Theano tutorial
# Solution to Exercise in section 'Loop'
# Solution to Exercise in section 'Loop'
"""
# 1. First example
# 1. First example (runs satisfactorily)
import
theano
import
theano
import theano.tensor as
T
import
theano.tensor
as
tensor
theano
.
config
.
warn
.
subtensor_merge_bug
=
False
theano
.
config
.
warn
.
subtensor_merge_bug
=
False
k =
T
.iscalar("k")
k
=
tensor
.
iscalar
(
"k"
)
A =
T
.vector("A")
A
=
tensor
.
vector
(
"A"
)
def
inner_fct
(
prior_result
,
A
):
def
inner_fct
(
prior_result
,
A
):
...
@@ -20,76 +18,72 @@ def inner_fct(prior_result, A):
...
@@ -20,76 +18,72 @@ def inner_fct(prior_result, A):
# Symbolic description of the result
# Symbolic description of the result
result
,
updates
=
theano
.
scan
(
fn
=
inner_fct
,
result
,
updates
=
theano
.
scan
(
fn
=
inner_fct
,
outputs_info=T
.ones_like(A),
outputs_info
=
tensor
.
ones_like
(
A
),
non_sequences=A, n_steps=k)
non_sequences
=
A
,
n_steps
=
k
)
# Scan has provided us with A ** 1 through A ** k. Keep only the last
# Scan has provided us with A ** 1 through A ** k. Keep only the last
# value. Scan notices this and does not waste memory saving them.
# value. Scan notices this and does not waste memory saving them.
final_result
=
result
[
-
1
]
final_result
=
result
[
-
1
]
power
=
theano
.
function
(
inputs
=
[
A
,
k
],
outputs
=
final_result
,
power
=
theano
.
function
(
inputs
=
[
A
,
k
],
outputs
=
final_result
,
updates=updates)
updates
=
updates
)
print
power
(
range
(
10
),
2
)
print
power
(
range
(
10
),
2
)
# [ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
# [ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
# 2. Second example
(runs satisfactorily)
# 2. Second example
import
numpy
import
numpy
import
theano
import
theano
import theano.tensor as
T
import
theano.tensor
as
tensor
coefficients
=
theano
.
tensor
.
vector
(
"coefficients"
)
coefficients
=
theano
.
tensor
.
vector
(
"coefficients"
)
x =
T
.scalar("x")
x
=
tensor
.
scalar
(
"x"
)
max_coefficients_supported
=
10000
max_coefficients_supported
=
10000
# Generate the components of the polynomial
# Generate the components of the polynomial
full_range
=
theano
.
tensor
.
arange
(
max_coefficients_supported
)
full_range
=
theano
.
tensor
.
arange
(
max_coefficients_supported
)
components
,
updates
=
theano
.
scan
(
fn
=
lambda
coeff
,
power
,
free_var
:
components
,
updates
=
theano
.
scan
(
fn
=
lambda
coeff
,
power
,
free_var
:
coeff * (free_var ** power),
coeff
*
(
free_var
**
power
),
outputs_info=None
,
sequences
=
[
coefficients
,
full_range
]
,
sequences=[coefficients, full_range]
,
outputs_info
=
None
,
non_sequences=x)
non_sequences
=
x
)
polynomial
=
components
.
sum
()
polynomial
=
components
.
sum
()
calculate_polynomial1
=
theano
.
function
(
inputs
=
[
coefficients
,
x
],
calculate_polynomial1
=
theano
.
function
(
inputs
=
[
coefficients
,
x
],
outputs=polynomial)
outputs
=
polynomial
)
test_coeff
=
numpy
.
asarray
([
1
,
0
,
2
],
dtype
=
numpy
.
float32
)
test_coeff
=
numpy
.
asarray
([
1
,
0
,
2
],
dtype
=
numpy
.
float32
)
print
calculate_polynomial1
(
test_coeff
,
3
)
print
calculate_polynomial1
(
test_coeff
,
3
)
# 19.0
# 19.0
"""
# 3. Reduction performed inside scan
# 3. Reduction performed inside scan
# TODO: repair this code: yields 56.0 instead of 19.0
import
numpy
import
numpy
import
theano
import
theano
import
theano.tensor
as
T
import
theano.tensor
as
tensor
theano
.
config
.
warn
.
subtensor_merge_bug
=
False
theano
.
config
.
warn
.
subtensor_merge_bug
=
False
coefficients
=
theano
.
tensor
.
vector
(
"coefficients"
)
coefficients
=
theano
.
tensor
.
vector
(
"coefficients"
)
x
=
T
.
scalar
(
"x"
)
x
=
tensor
.
scalar
(
"x"
)
max_coefficients_supported
=
10000
max_coefficients_supported
=
10000
# Generate the components of the polynomial
# Generate the components of the polynomial
full_range
=
theano
.
tensor
.
arange
(
max_coefficients_supported
)
full_range
=
theano
.
tensor
.
arange
(
max_coefficients_supported
)
outputs_info
=
T
.
as_tensor_variable
(
numpy
.
asarray
(
0
,
'float64'
))
outputs_info
=
tensor
.
as_tensor_variable
(
numpy
.
asarray
(
0
,
'float64'
))
components
,
updates
=
theano
.
scan
(
fn
=
lambda
prior_value
,
coeff
,
power
,
free_var
:
components
,
updates
=
theano
.
scan
(
fn
=
lambda
coeff
,
power
,
prior_value
,
free_var
:
prior_value
+
(
coeff
*
(
free_var
**
power
)),
prior_value
+
(
coeff
*
(
free_var
**
power
)),
outputs_info
=
outputs_info
,
sequences
=
[
coefficients
,
full_range
]
,
sequences
=
[
coefficients
,
full_range
]
,
outputs_info
=
outputs_info
,
non_sequences
=
x
)
non_sequences
=
x
)
polynomial
=
components
[
-
1
]
polynomial
=
components
[
-
1
]
calculate_polynomial
=
theano
.
function
(
inputs
=
[
coefficients
,
x
],
calculate_polynomial
=
theano
.
function
(
inputs
=
[
coefficients
,
x
],
outputs
=
polynomial
,
updates
=
updates
)
outputs
=
polynomial
,
updates
=
updates
)
test_coeff
=
numpy
.
asarray
([
1
,
0
,
2
],
dtype
=
numpy
.
float32
)
test_coeff
=
numpy
.
asarray
([
1
,
0
,
2
],
dtype
=
numpy
.
float32
)
print
calculate_polynomial
(
test_coeff
,
3
)
print
calculate_polynomial
(
test_coeff
,
3
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论