Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4b29e343
提交
4b29e343
authored
3月 10, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed Tensor
上级
f86767b2
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
54 行增加
和
84 行删除
+54
-84
cc.py
gof/cc.py
+0
-43
grad.py
grad.py
+12
-16
tensor.py
tensor.py
+42
-25
没有找到文件。
gof/cc.py
浏览文件 @
4b29e343
...
@@ -417,49 +417,6 @@ class CLinker(Linker):
...
@@ -417,49 +417,6 @@ class CLinker(Linker):
except
AbstractFunctionError
:
pass
except
AbstractFunctionError
:
pass
return
ret
return
ret
# def make_function(self, in_order, out_order):
# nin = len(self.inputs)
# nout = len(self.outputs)
# if nin != len(in_order):
# raise TypeError("Wrong number of inputs.")
# if nout != len(out_order):
# raise TypeError("Wrong number of outputs.")
# in_storage = []
# out_storage = []
# cthunk_in_args = [None] * nin
# cthunk_out_args = [None] * nout
# for result in in_order:
# idx = self.inputs.index(result)
# storage = [None]
# cthunk_in_args[idx] = storage
# in_storage.append(storage)
# for result in out_order:
# idx = self.outputs.index(result)
# storage = [None]
# cthunk_out_args[idx] = storage
# out_storage.append(storage)
# for arg in cthunk_in_args + cthunk_out_args:
# if arg is None:
# raise Exception("The inputs or outputs are underspecified.")
# error_storage = [None, None, None]
# cthunk = self.cthunk_factory(error_storage, cthunk_in_args, cthunk_out_args)
# def execute(*args):
# for arg, storage in zip(args, in_storage):
# storage[0] = arg
# failure = cutils.run_cthunk(cthunk)
# if failure:
# raise error_storage[0], error_storage[1] + " " + str(self.find_task(failure - 1))
# return utils.to_return_values([storage[0] for storage in out_storage])
# return execute
def
__compile__
(
self
,
inplace
=
False
):
def
__compile__
(
self
,
inplace
=
False
):
if
inplace
:
if
inplace
:
in_results
=
self
.
inputs
in_results
=
self
.
inputs
...
...
grad.py
浏览文件 @
4b29e343
...
@@ -29,9 +29,8 @@ class Grad(object):
...
@@ -29,9 +29,8 @@ class Grad(object):
def
__getitem__
(
self
,
item
):
def
__getitem__
(
self
,
item
):
"""Map item to its id and retrieve it."""
"""Map item to its id and retrieve it."""
key
=
core
.
wrap
(
item
)
try
:
try
:
return
self
.
map
[
key
]
return
self
.
map
[
item
]
except
KeyError
:
except
KeyError
:
return
Undefined
return
Undefined
...
@@ -60,16 +59,16 @@ class Grad(object):
...
@@ -60,16 +59,16 @@ class Grad(object):
# nothing to do
# nothing to do
return
return
if
r
.
data
is
not
None
and
dr
.
data
is
not
None
:
#
if r.data is not None and dr.data is not None:
if
not
hasattr
(
r
,
'shape'
):
#
if not hasattr(r, 'shape'):
raise
ValueError
((
'Grad::add r lacks shape: type='
,
#
raise ValueError(('Grad::add r lacks shape: type=',
type
(
r
)))
#
type(r)))
if
not
hasattr
(
dr
,
'shape'
):
#
if not hasattr(dr, 'shape'):
raise
ValueError
((
'Grad::add dr lacks shape: type='
,
#
raise ValueError(('Grad::add dr lacks shape: type=',
type
(
dr
)))
#
type(dr)))
if
r
.
shape
!=
dr
.
shape
:
#
if r.shape != dr.shape:
raise
ValueError
((
'Grad::add r, dr shape mismatch'
,
#
raise ValueError(('Grad::add r, dr shape mismatch',
r
.
shape
,
dr
.
shape
))
#
r.shape, dr.shape))
# prevent 'r' from being re-calculated by self.__call__ in 'build_eval' mode
# prevent 'r' from being re-calculated by self.__call__ in 'build_eval' mode
if
r
.
state
is
gof
.
result
.
Computed
:
if
r
.
state
is
gof
.
result
.
Computed
:
...
@@ -102,14 +101,12 @@ class Grad(object):
...
@@ -102,14 +101,12 @@ class Grad(object):
"""
"""
if
not
maybe_redo
and
self
.
did_bprop
:
if
not
maybe_redo
and
self
.
did_bprop
:
raise
Exception
(
'bprop has already been done. Consider calling with maybe_redo=True.'
)
raise
Exception
(
'bprop has already been done. Consider calling with maybe_redo=True.'
)
core
.
build_mode
()
try
:
try
:
outputs
=
self
.
outputs
outputs
=
self
.
outputs
inputs
=
gof
.
graph
.
inputs
(
outputs
)
inputs
=
gof
.
graph
.
inputs
(
outputs
)
for
op
in
gof
.
graph
.
io_toposort
(
inputs
,
outputs
)
.
__reversed__
():
for
op
in
gof
.
graph
.
io_toposort
(
inputs
,
outputs
)
.
__reversed__
():
op
.
update_gradient
(
self
)
op
.
update_gradient
(
self
)
finally
:
finally
:
core
.
pop_mode
()
self
.
did_bprop
=
True
self
.
did_bprop
=
True
def
__call__
(
self
,
item
):
def
__call__
(
self
,
item
):
...
@@ -121,8 +118,7 @@ class Grad(object):
...
@@ -121,8 +118,7 @@ class Grad(object):
if
not
self
.
did_bprop
:
if
not
self
.
did_bprop
:
raise
Exception
(
'Grad.__call__ only makes sense after a bprop'
)
raise
Exception
(
'Grad.__call__ only makes sense after a bprop'
)
rval
=
self
[
item
]
rval
=
self
[
item
]
if
rval
is
not
Undefined
\
if
rval
is
not
Undefined
:
and
core
.
current_mode
()
==
'build_eval'
:
compute_from
([
rval
],
self
.
_compute_history
)
compute_from
([
rval
],
self
.
_compute_history
)
return
rval
return
rval
...
...
tensor.py
浏览文件 @
4b29e343
...
@@ -6,27 +6,31 @@ from gof import ResultBase
...
@@ -6,27 +6,31 @@ from gof import ResultBase
from
gof
import
Op
from
gof
import
Op
class
NumpyR
(
ResultBase
):
def
tensor
(
data
,
name
=
None
):
return
Tensor
(
data
.
dtype
,
[
0
]
*
len
(
data
.
shape
),
data
,
name
)
def
__init__
(
self
,
dtype
,
nd
,
name
=
None
):
def
_broadcastable_pattern
(
pattern
):
self
.
nd
=
nd
def
factory
(
data
=
None
,
name
=
None
):
self
.
dtype
=
dtype
if
data
:
assert
len
(
data
.
shape
)
==
len
(
pattern
)
ResultBase
.
__init__
(
self
,
role
=
None
,
data
=
None
,
name
=
name
)
return
Tensor
(
data
.
dtype
,
pattern
,
data
,
name
)
matrix
=
_broadcastable_pattern
([
0
,
0
])
row
=
_broadcastable_pattern
([
1
,
0
])
col
=
_broadcastable_pattern
([
0
,
1
])
def
validate
(
self
,
data
):
if
not
isinstance
(
data
,
numpy
.
ndarray
):
raise
TypeError
(
"Expected ndarray instance."
)
elif
not
len
(
data
.
shape
)
==
self
.
nd
:
raise
TypeError
(
"Expected ndarray with
%
i dimensions."
%
self
.
nd
)
elif
not
str
(
data
.
dtype
)
==
self
.
dtype
:
raise
TypeError
(
"Expected ndarray with data type
%
i."
%
self
.
dtype
)
class
Tensor
(
ResultBase
):
def
__init__
(
self
,
dtype
,
broadcastable
,
data
=
None
,
name
=
None
):
self
.
broadcastable
=
broadcastable
self
.
dtype
=
dtype
ResultBase
.
__init__
(
self
,
role
=
None
,
data
=
None
,
name
=
name
)
# def to_c_type(self, dtype
):
def
filter
(
self
,
data
):
# if dtype == "float64":
arr
=
numpy
.
asarray
(
data
,
dtype
=
self
.
dtype
)
# return "double"
for
b
,
s
in
zip
(
self
.
broadcastable
,
arr
.
shape
):
# else:
assert
not
b
or
s
==
1
# raise TypeError("Cannot translate dtype to C.")
return
arr
def
c_declare
(
self
):
def
c_declare
(
self
):
return
"""
return
"""
...
@@ -64,35 +68,48 @@ class NumpyR(ResultBase):
...
@@ -64,35 +68,48 @@ class NumpyR(ResultBase):
return
[]
return
[]
def
__copy__
(
self
):
def
__copy__
(
self
):
cpy
=
self
.
__class__
(
self
.
dtype
,
self
.
nd
,
self
.
name
)
"""
Returns a copy of this Tensor. If there is data stored inside it, it is also copied.
"""
cpy
=
self
.
__class__
(
self
.
dtype
,
self
.
broadcastable
,
None
,
self
.
name
)
cpy
.
data
=
copy
(
self
.
data
)
cpy
.
data
=
copy
(
self
.
data
)
return
cpy
return
cpy
def
T
heano
Op
(
Op
):
def
T
ensor
Op
(
Op
):
nin
=
-
1
nin
=
-
1
nout
=
1
nout
=
1
def
__init__
(
self
,
*
inputs
):
def
__init__
(
self
,
*
inputs
):
def
wrap_as_tensor
(
x
):
if
isinstance
(
x
,
Tensor
):
return
x
else
:
return
Tensor
(
x
)
inputs
=
map
(
wrap_as_tensor
,
inputs
)
if
self
.
nin
>=
0
:
if
self
.
nin
>=
0
:
if
len
(
inputs
)
!=
self
.
nin
:
if
len
(
inputs
)
!=
self
.
nin
:
raise
TypeError
(
"Wrong number of inputs for
%
s (got
%
i, expected
%
i)"
)
\
raise
TypeError
(
"Wrong number of inputs for
%
s (got
%
i, expected
%
i)"
)
\
%
(
self
,
len
(
inputs
),
self
.
nin
)
%
(
self
,
len
(
inputs
),
self
.
nin
)
i_
nds
=
[
getattr
(
input
,
'nd
'
,
None
)
for
input
in
inputs
]
i_
broadcastables
=
[
getattr
(
input
,
'broadcastable
'
,
None
)
for
input
in
inputs
]
i_dtypes
=
[
getattr
(
input
,
'dtype'
,
None
)
for
input
in
inputs
]
i_dtypes
=
[
getattr
(
input
,
'dtype'
,
None
)
for
input
in
inputs
]
o_
nds
=
self
.
propagate_nd
(
*
i_nds
)
o_
broadcastables
=
utils
.
from_return_values
(
self
.
propagate_broadcastable
(
*
i_broadcastables
)
)
o_dtypes
=
self
.
propagate_dtypes
(
*
i_dtypes
)
o_dtypes
=
utils
.
from_return_values
(
self
.
propagate_dtype
(
*
i_dtypes
)
)
return
[
NumpyR
(
nd
,
dtype
)
for
nd
,
dtype
in
zip
(
o_nds
,
o_dtypes
)]
self
.
inputs
=
inputs
self
.
outputs
=
[
Tensor
(
dtype
,
broadcastable
)
for
broadcastable
,
dtype
in
zip
(
o_broadcastables
,
o_dtypes
)]
def
propagate_
nds
(
self
,
*
inputs
):
def
propagate_
broadcastable
(
self
,
*
inputs
):
raise
AbstractFunctionError
()
raise
AbstractFunctionError
()
def
propagate_dtype
s
(
self
,
*
inputs
):
def
propagate_dtype
(
self
,
*
inputs
):
raise
AbstractFunctionError
()
raise
AbstractFunctionError
()
def
impl
(
self
,
*
inputs
):
def
impl
(
self
,
*
inputs
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论