Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6080bef5
提交
6080bef5
authored
2月 22, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
did a little work on sandbox.solve to clean it up
上级
3b831aab
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
34 行增加
和
15 行删除
+34
-15
solve.py
theano/sandbox/solve.py
+34
-15
没有找到文件。
theano/sandbox/solve.py
浏览文件 @
6080bef5
import
numpy
import
numpy
,
scipy
.
linalg
from
theano
import
gof
,
tensor
from
theano
import
gof
,
tensor
,
scalar
import
unittest
import
unittest
class
Solve
(
gof
.
Op
):
class
Solve
(
gof
.
Op
):
"""
"""
Find the solution to the linear equation Ax=b,
Find the solution to the linear equation Ax=b,
...
@@ -9,25 +10,43 @@ class Solve(gof.Op):
...
@@ -9,25 +10,43 @@ class Solve(gof.Op):
It use numpy.solve to find the solution.
It use numpy.solve to find the solution.
"""
"""
def
make_node
(
self
,
A
,
b
):
#TODO: Add class options to use the performance-enhancing flags
if
not
isinstance
(
A
,
gof
.
Variable
)
or
not
A
.
type
==
tensor
.
matrix
()
.
type
:
# sym_pos, lower, overwrite_a, overwrite_b
raise
TypeError
(
"We expected that A had a matrix type"
)
if
not
isinstance
(
B
,
gof
.
Variable
)
or
not
B
.
type
==
tensor
.
matrix
()
.
type
:
raise
TypeError
(
"We expected that B had a matrix type"
)
node
=
gof
.
Apply
(
op
=
self
,
inputs
=
[
A
,
B
],
outputs
=
[
tensor
.
matrix
()])
#TODO: Add C code that calls the underlying LAPACK routines
return
node
# and keeps a memory workspace from call to call as a non-default Op output
def
perform
(
self
,
node
,
(
A
,
B
),
(
output
,
)):
def
__eq__
(
self
,
other
):
ret
=
numpy
.
solve
(
A
,
B
)
return
type
(
self
)
==
type
(
other
)
output
[
0
]
=
ret
def
grad
(
self
,
(
theta
,
A
,
B
),
(
gtheta
,)):
def
__hash__
(
self
):
raise
NotImplementedError
()
return
hash
(
type
(
self
))
def
make_node
(
self
,
A
,
b
):
A_
=
tensor
.
as_tensor_variable
(
A
)
b_
=
tensor
.
as_tensor_variable
(
b
)
if
A_
.
broadcastable
!=
(
False
,
False
):
raise
TypeError
(
"A must be a matrix"
,
A_
.
type
)
if
b_
.
broadcastable
not
in
((
False
,),
(
True
,
False
),
(
False
,
False
)):
raise
TypeError
(
"b must be a matrix or vector"
,
b_
.
type
)
odtype
=
scalar
.
upcast
(
A_
.
dtype
,
b_
.
dtype
)
otype
=
tensor
.
TensorType
(
broadcastable
=
b_
.
broadcastable
,
dtype
=
odtype
)
return
gof
.
Apply
(
op
=
self
,
inputs
=
[
A
,
B
],
outputs
=
[
otype
()])
def
perform
(
self
,
node
,
(
A
,
b
),
(
output
,
)):
ret
=
scipy
.
linalg
.
solve
(
A
,
b
)
if
ret
.
dtype
!=
node
.
outputs
[
0
]
.
dtype
:
print
>>
sys
.
stderr
,
"WARNING: Solve.perform() required cast."
ret
=
theano
.
_asarray
(
ret
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
output
[
0
]
=
ret
solve
=
Solve
()
solve
=
Solve
()
## TODO: test dtype conversion
## TODO: test that invalid types are rejected by make_node
## TODO: test that each valid type for A and b works correctly
from
theano.tests
import
unittest_tools
as
utt
class
T_solve
(
unittest
.
TestCase
):
class
T_solve
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
(
666
))
self
.
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
(
666
))
...
@@ -35,7 +54,7 @@ class T_solve(unittest.TestCase):
...
@@ -35,7 +54,7 @@ class T_solve(unittest.TestCase):
def
test0
(
self
):
def
test0
(
self
):
A
=
self
.
rng
.
randn
(
5
,
5
)
A
=
self
.
rng
.
randn
(
5
,
5
)
b
=
numpy
.
array
(
range
(
5
),
dtype
=
float
)
b
=
numpy
.
array
(
range
(
5
),
dtype
=
float
)
x
=
num
py
.
linalg
.
solve
(
A
,
b
)
x
=
sci
py
.
linalg
.
solve
(
A
,
b
)
Ax
=
numpy
.
dot
(
A
,
x
)
Ax
=
numpy
.
dot
(
A
,
x
)
are
=
tensor
.
numeric_grad
.
abs_rel_err
(
Ax
,
b
)
are
=
tensor
.
numeric_grad
.
abs_rel_err
(
Ax
,
b
)
self
.
failUnless
(
numpy
.
all
(
are
<
1.0e-5
),
(
are
,
Ax
,
b
))
self
.
failUnless
(
numpy
.
all
(
are
<
1.0e-5
),
(
are
,
Ax
,
b
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论