Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f5801429
提交
f5801429
authored
3月 24, 2008
作者:
james@mackie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
bringing back sparse
上级
a7e53331
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
156 行增加
和
89 行删除
+156
-89
_test_sparse.py
_test_sparse.py
+150
-83
op.py
gof/op.py
+1
-1
sparse.py
sparse.py
+0
-0
tensor.py
tensor.py
+5
-5
没有找到文件。
_test_sparse.py
浏览文件 @
f5801429
from
sparse
import
*
from
sparse
import
*
import
unittest
import
unittest
import
compile
class
_testCase
_transpose
(
unittest
.
TestCase
):
class
T
_transpose
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
core
.
build_eval_mode
()
numpy
.
random
.
seed
(
44
)
numpy
.
random
.
seed
(
44
)
def
te
arDown
(
self
):
def
te
st_transpose_csc
(
self
):
core
.
pop_mode
(
)
sp
=
sparse
.
csc_matrix
(
sparse
.
speye
(
5
,
3
)
)
def
test_transpose
(
self
):
a
=
assparse
(
sp
)
a
=
SparseR
(
sparse
.
csr_matrix
(
sparse
.
speye
(
5
,
3
))
)
self
.
failUnless
(
a
.
data
is
sp
)
self
.
failUnless
(
a
.
data
.
shape
==
(
5
,
3
))
self
.
failUnless
(
a
.
data
.
shape
==
(
5
,
3
))
self
.
failUnless
(
a
.
dtype
==
'float64'
)
self
.
failUnless
(
a
.
format
==
'csc'
,
a
.
format
)
ta
=
transpose
(
a
)
ta
=
transpose
(
a
)
self
.
failUnless
(
ta
.
data
.
shape
==
(
3
,
5
))
self
.
failUnless
(
ta
.
dtype
==
'float64'
,
ta
.
dtype
)
self
.
failUnless
(
ta
.
format
==
'csr'
,
ta
.
format
)
vta
=
compile
.
eval_outputs
([
ta
])
self
.
failUnless
(
vta
.
shape
==
(
3
,
5
))
def
test_transpose_csr
(
self
):
a
=
assparse
(
sparse
.
csr_matrix
(
sparse
.
speye
(
5
,
3
)))
self
.
failUnless
(
a
.
data
.
shape
==
(
5
,
3
))
self
.
failUnless
(
a
.
dtype
==
'float64'
)
self
.
failUnless
(
a
.
format
==
'csr'
)
ta
=
transpose
(
a
)
self
.
failUnless
(
ta
.
dtype
==
'float64'
,
ta
.
dtype
)
self
.
failUnless
(
ta
.
format
==
'csc'
,
ta
.
format
)
vta
=
compile
.
eval_outputs
([
ta
])
self
.
failUnless
(
vta
.
shape
==
(
3
,
5
))
class
T_Add
(
unittest
.
TestCase
):
def
test0
(
self
):
sp_a
=
sparse
.
csc_matrix
(
sparse
.
speye
(
5
,
3
))
a
=
assparse
(
sp_a
)
sp_b
=
sparse
.
csc_matrix
(
sparse
.
speye
(
5
,
3
))
b
=
assparse
(
sp_b
)
self
.
failUnless
(
a
.
data
is
sp_a
)
apb
=
add_s_s
(
a
,
b
)
self
.
failUnless
(
apb
.
dtype
==
a
.
dtype
,
apb
.
dtype
)
self
.
failUnless
(
apb
.
format
==
a
.
format
,
apb
.
format
)
val
=
compile
.
eval_outputs
([
apb
])
self
.
failUnless
(
val
.
shape
==
(
5
,
3
))
self
.
failUnless
(
numpy
.
all
(
val
.
todense
()
==
(
sp_a
+
sp_b
)
.
todense
()))
class
T_conversion
(
unittest
.
TestCase
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
44
)
def
test0
(
self
):
a
=
tensor
.
astensor
(
numpy
.
random
.
rand
(
5
))
s
=
sparse_from_dense
(
a
,
'csc'
)
val
=
compile
.
eval_outputs
([
s
])
self
.
failUnless
(
str
(
val
.
dtype
)
==
'float64'
)
self
.
failUnless
(
val
.
format
==
'csc'
)
def
test1
(
self
):
a
=
tensor
.
astensor
(
numpy
.
random
.
rand
(
5
))
s
=
sparse_from_dense
(
a
,
'csr'
)
val
=
compile
.
eval_outputs
([
s
])
self
.
failUnless
(
str
(
val
.
dtype
)
==
'float64'
)
self
.
failUnless
(
val
.
format
==
'csr'
)
def
test2
(
self
):
csr
=
sparse
.
csr_matrix
((
2
,
5
))
d
=
dense_from_sparse
(
csr
)
csr
[
0
,
0
]
=
1.0
val
=
compile
.
eval_outputs
([
d
])
self
.
failUnless
(
str
(
val
.
dtype
)
==
'float64'
)
self
.
failUnless
(
numpy
.
all
(
val
[
0
]
==
[
1
,
0
,
0
,
0
,
0
]))
class
_testCase_dot
(
unittest
.
TestCase
):
class
_testCase_dot
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
core
.
build_eval_mode
()
numpy
.
random
.
seed
(
44
)
numpy
.
random
.
seed
(
44
)
def
tearDown
(
self
):
core
.
pop_mode
()
def
test
(
self
):
def
test_basic0
(
self
):
"""Bring back the tests for sparse dot"""
for
mtype
in
[
sparse
.
csc_matrix
,
sparse
.
csr_matrix
]:
raise
NotImplementedError
()
x
=
SparseR
(
mtype
(
sparse
.
speye
(
5
,
3
)))
if
0
:
y
=
core
.
wrap
(
numpy
.
random
.
rand
(
3
,
2
))
def
test_basic0
(
self
):
for
mtype
in
[
sparse
.
csc_matrix
,
sparse
.
csr_matrix
]:
z
=
dot
(
x
,
y
)
x
=
assparse
(
mtype
(
sparse
.
speye
(
5
,
3
)))
self
.
failUnless
(
z
.
data
.
shape
==
(
5
,
2
))
y
=
astensor
(
numpy
.
random
.
rand
(
3
,
2
))
self
.
failUnless
(
type
(
z
.
data
)
is
mtype
)
def
test_basic1
(
self
):
z
=
dot
(
x
,
y
)
"""dot: sparse left"""
self
.
failUnless
(
z
.
data
.
shape
==
(
5
,
2
))
a
=
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
self
.
failUnless
(
type
(
z
.
data
)
is
mtype
)
dtype
=
'float64'
)
b
=
numpy
.
random
.
rand
(
5
,
3
)
def
test_basic1
(
self
):
for
mtype
in
[
sparse
.
csr_matrix
,
sparse
.
csc_matrix
,
sparse
.
dok_matrix
,
"""dot: sparse left"""
sparse
.
lil_matrix
]:
#, sparse.coo_matrix]:
a
=
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
#print type(a), mtype
dtype
=
'float64'
)
m
=
mtype
(
a
)
b
=
numpy
.
random
.
rand
(
5
,
3
)
ab
=
m
.
dot
(
b
)
for
mtype
in
[
sparse
.
csr_matrix
,
sparse
.
csc_matrix
,
sparse
.
dok_matrix
,
try
:
sparse
.
lil_matrix
]:
#, sparse.coo_matrix]:
z
=
dot
(
SparseR
(
m
),
core
.
ResultBase
(
data
=
b
))
#print type(a), mtype
m
=
mtype
(
a
)
ab
=
m
.
dot
(
b
)
try
:
z
=
dot
(
SparseR
(
m
),
core
.
ResultBase
(
data
=
b
))
self
.
failUnless
(
z
.
data
.
shape
==
ab
.
shape
)
self
.
failUnless
(
type
(
z
.
data
)
==
type
(
ab
))
except
Exception
,
e
:
print
'cccc'
,
mtype
,
e
,
str
(
e
)
raise
def
test_basic2
(
self
):
"""dot: sparse right"""
a
=
numpy
.
random
.
rand
(
2
,
5
)
b
=
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
dtype
=
'float64'
)
.
transpose
()
for
mtype
in
[
sparse
.
csr_matrix
,
sparse
.
csc_matrix
,
sparse
.
dok_matrix
,
sparse
.
lil_matrix
]:
#, sparse.coo_matrix]:
m
=
mtype
(
b
)
ab
=
m
.
transpose
()
.
dot
(
a
.
transpose
())
.
transpose
()
z
=
dot
(
core
.
ResultBase
(
data
=
a
),
SparseR
(
mtype
(
b
)))
self
.
failUnless
(
z
.
data
.
shape
==
ab
.
shape
)
self
.
failUnless
(
z
.
data
.
shape
==
ab
.
shape
)
self
.
failUnless
(
type
(
z
.
data
)
==
type
(
ab
))
self
.
failUnless
(
type
(
z
.
data
)
==
type
(
ab
))
except
Exception
,
e
:
print
'cccc'
,
mtype
,
e
,
str
(
e
)
def
test_graph_bprop0
(
self
):
raise
x
=
core
.
wrap
(
numpy
.
random
.
rand
(
10
,
2
))
def
test_basic2
(
self
):
w
=
SparseR
(
sparse
.
csr_matrix
(
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
"""dot: sparse right"""
0
]],
dtype
=
'float64'
)))
a
=
numpy
.
random
.
rand
(
2
,
5
)
b
=
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
for
epoch
in
xrange
(
50
):
dtype
=
'float64'
)
.
transpose
()
xw
=
sparse2dense
(
dot
(
x
,
w
))
y
=
sparse2dense
(
dot
(
xw
,
transpose
(
w
)))
for
mtype
in
[
sparse
.
csr_matrix
,
sparse
.
csc_matrix
,
sparse
.
dok_matrix
,
loss
=
core
.
sum
(
core
.
sqr
(
x
-
y
))
sparse
.
lil_matrix
]:
#, sparse.coo_matrix]:
gy
=
y
-
x
m
=
mtype
(
b
)
g
=
grad
.
Grad
({
y
:
gy
})
ab
=
m
.
transpose
()
.
dot
(
a
.
transpose
())
.
transpose
()
g
.
bprop
()
z
=
dot
(
core
.
ResultBase
(
data
=
a
),
SparseR
(
mtype
(
b
)))
lr
=
0.002
self
.
failUnless
(
z
.
data
.
shape
==
ab
.
shape
)
g
(
w
)
.
data
[
1
,
0
]
=
0
self
.
failUnless
(
type
(
z
.
data
)
==
type
(
ab
))
g
(
w
)
.
data
[
1
,
4
]
=
0
def
test_graph_bprop0
(
self
):
w
.
data
=
-
lr
*
g
(
w
)
.
data
+
w
.
data
x
=
core
.
wrap
(
numpy
.
random
.
rand
(
10
,
2
))
w
=
SparseR
(
sparse
.
csr_matrix
(
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
self
.
failUnless
(
'3.08560636025'
==
str
(
loss
.
data
))
0
]],
dtype
=
'float64'
)))
def
test_graph_bprop1
(
self
):
for
epoch
in
xrange
(
50
):
x
=
core
.
wrap
(
numpy
.
random
.
rand
(
10
,
2
))
xw
=
sparse2dense
(
dot
(
x
,
w
))
w
=
SparseR
(
sparse
.
csr_matrix
(
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
y
=
sparse2dense
(
dot
(
xw
,
transpose
(
w
)))
0
]],
dtype
=
'float64'
)))
loss
=
core
.
sum
(
core
.
sqr
(
x
-
y
))
gy
=
y
-
x
for
epoch
in
xrange
(
50
):
g
=
grad
.
Grad
({
y
:
gy
})
xw
=
sparse2dense
(
dot
(
x
,
w
))
g
.
bprop
()
y
=
sparse2dense
(
dot
(
xw
,
transpose
(
w
)))
lr
=
0.002
loss
=
core
.
sum
(
core
.
sqr
(
x
-
y
))
g
(
w
)
.
data
[
1
,
0
]
=
0
g
=
grad
.
grad
(
loss
)
g
(
w
)
.
data
[
1
,
4
]
=
0
lr
=
0.001
w
.
data
=
-
lr
*
g
(
w
)
.
data
+
w
.
data
g
(
w
)
.
data
[
1
,
0
]
=
0
self
.
failUnless
(
'3.08560636025'
==
str
(
loss
.
data
))
g
(
w
)
.
data
[
1
,
4
]
=
0
w
.
data
=
-
lr
*
g
(
w
)
.
data
+
w
.
data
def
test_graph_bprop1
(
self
):
x
=
core
.
wrap
(
numpy
.
random
.
rand
(
10
,
2
))
self
.
failUnless
(
'3.08560636025'
==
str
(
loss
.
data
))
w
=
SparseR
(
sparse
.
csr_matrix
(
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
dtype
=
'float64'
)))
for
epoch
in
xrange
(
50
):
xw
=
sparse2dense
(
dot
(
x
,
w
))
y
=
sparse2dense
(
dot
(
xw
,
transpose
(
w
)))
loss
=
core
.
sum
(
core
.
sqr
(
x
-
y
))
g
=
grad
.
grad
(
loss
)
lr
=
0.001
g
(
w
)
.
data
[
1
,
0
]
=
0
g
(
w
)
.
data
[
1
,
4
]
=
0
w
.
data
=
-
lr
*
g
(
w
)
.
data
+
w
.
data
self
.
failUnless
(
'3.08560636025'
==
str
(
loss
.
data
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
gof/op.py
浏览文件 @
f5801429
...
@@ -144,7 +144,7 @@ class Op(object):
...
@@ -144,7 +144,7 @@ class Op(object):
TODO: consider moving this function to the python linker.
TODO: consider moving this function to the python linker.
"""
"""
res
=
self
.
impl
(
*
[
input
.
data
for
input
in
self
.
inputs
])
res
=
self
.
impl
(
*
[
input
.
data
for
input
in
self
.
inputs
])
if
self
.
nout
==
1
:
if
len
(
self
.
outputs
)
==
1
:
self
.
outputs
[
0
]
.
data
=
res
self
.
outputs
[
0
]
.
data
=
res
else
:
else
:
assert
len
(
res
)
==
len
(
self
.
outputs
)
assert
len
(
res
)
==
len
(
self
.
outputs
)
...
...
sparse.py
浏览文件 @
f5801429
差异被折叠。
点击展开。
tensor.py
浏览文件 @
f5801429
...
@@ -70,6 +70,8 @@ class Tensor(BaseTensor):
...
@@ -70,6 +70,8 @@ class Tensor(BaseTensor):
# alternate Tensor constructor
# alternate Tensor constructor
def
astensor
(
data
,
broadcastable
=
None
,
role
=
None
,
name
=
None
):
def
astensor
(
data
,
broadcastable
=
None
,
role
=
None
,
name
=
None
):
"""Return a Tensor containing given data"""
"""Return a Tensor containing given data"""
if
isinstance
(
data
,
Tensor
)
and
broadcastable
is
None
and
role
is
None
and
name
is
None
:
return
data
data
=
numpy
.
asarray
(
data
)
data
=
numpy
.
asarray
(
data
)
if
broadcastable
is
None
:
if
broadcastable
is
None
:
broadcastable
=
[
s
==
1
for
s
in
data
.
shape
]
broadcastable
=
[
s
==
1
for
s
in
data
.
shape
]
...
@@ -116,11 +118,9 @@ def _assert_tensor_scalar(x, a):
...
@@ -116,11 +118,9 @@ def _assert_tensor_scalar(x, a):
if
numpy
.
product
(
a
.
shape
)
!=
1
:
if
numpy
.
product
(
a
.
shape
)
!=
1
:
raise
ValueError
(
"The second argument must be a scalar."
)
raise
ValueError
(
"The second argument must be a scalar."
)
def
_as_tensor
(
obj
):
# this has a different name, because _as_tensor is the function which ops use
if
isinstance
(
obj
,
Tensor
):
# to upcast their arguments... this internal-use function is a good place to put debugging stuff, better than the global astensor.
return
obj
_as_tensor
=
astensor
else
:
return
astensor
(
obj
)
class
_Op
(
BaseTensorOp
):
class
_Op
(
BaseTensorOp
):
"""A convenient base for the ops in this file"""
"""A convenient base for the ops in this file"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论