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
*
import
unittest
import
compile
class
_testCase
_transpose
(
unittest
.
TestCase
):
class
T
_transpose
(
unittest
.
TestCase
):
def
setUp
(
self
):
core
.
build_eval_mode
()
numpy
.
random
.
seed
(
44
)
def
te
arDown
(
self
):
core
.
pop_mode
(
)
def
test_transpose
(
self
):
a
=
SparseR
(
sparse
.
csr_matrix
(
sparse
.
speye
(
5
,
3
))
)
def
te
st_transpose_csc
(
self
):
sp
=
sparse
.
csc_matrix
(
sparse
.
speye
(
5
,
3
)
)
a
=
assparse
(
sp
)
self
.
failUnless
(
a
.
data
is
sp
)
self
.
failUnless
(
a
.
data
.
shape
==
(
5
,
3
))
self
.
failUnless
(
a
.
dtype
==
'float64'
)
self
.
failUnless
(
a
.
format
==
'csc'
,
a
.
format
)
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
):
def
setUp
(
self
):
core
.
build_eval_mode
()
numpy
.
random
.
seed
(
44
)
def
tearDown
(
self
):
core
.
pop_mode
()
def
test_basic0
(
self
):
for
mtype
in
[
sparse
.
csc_matrix
,
sparse
.
csr_matrix
]:
x
=
SparseR
(
mtype
(
sparse
.
speye
(
5
,
3
)))
y
=
core
.
wrap
(
numpy
.
random
.
rand
(
3
,
2
))
z
=
dot
(
x
,
y
)
self
.
failUnless
(
z
.
data
.
shape
==
(
5
,
2
))
self
.
failUnless
(
type
(
z
.
data
)
is
mtype
)
def
test_basic1
(
self
):
"""dot: sparse left"""
a
=
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
dtype
=
'float64'
)
b
=
numpy
.
random
.
rand
(
5
,
3
)
for
mtype
in
[
sparse
.
csr_matrix
,
sparse
.
csc_matrix
,
sparse
.
dok_matrix
,
sparse
.
lil_matrix
]:
#, sparse.coo_matrix]:
#print type(a), mtype
m
=
mtype
(
a
)
ab
=
m
.
dot
(
b
)
try
:
z
=
dot
(
SparseR
(
m
),
core
.
ResultBase
(
data
=
b
))
def
test
(
self
):
"""Bring back the tests for sparse dot"""
raise
NotImplementedError
()
if
0
:
def
test_basic0
(
self
):
for
mtype
in
[
sparse
.
csc_matrix
,
sparse
.
csr_matrix
]:
x
=
assparse
(
mtype
(
sparse
.
speye
(
5
,
3
)))
y
=
astensor
(
numpy
.
random
.
rand
(
3
,
2
))
z
=
dot
(
x
,
y
)
self
.
failUnless
(
z
.
data
.
shape
==
(
5
,
2
))
self
.
failUnless
(
type
(
z
.
data
)
is
mtype
)
def
test_basic1
(
self
):
"""dot: sparse left"""
a
=
numpy
.
asarray
([[
1
,
0
,
3
,
0
,
5
],
[
0
,
0
,
-
2
,
0
,
0
]],
dtype
=
'float64'
)
b
=
numpy
.
random
.
rand
(
5
,
3
)
for
mtype
in
[
sparse
.
csr_matrix
,
sparse
.
csc_matrix
,
sparse
.
dok_matrix
,
sparse
.
lil_matrix
]:
#, sparse.coo_matrix]:
#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
(
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
(
type
(
z
.
data
)
==
type
(
ab
))
def
test_graph_bprop0
(
self
):
x
=
core
.
wrap
(
numpy
.
random
.
rand
(
10
,
2
))
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
))
gy
=
y
-
x
g
=
grad
.
Grad
({
y
:
gy
})
g
.
bprop
()
lr
=
0.002
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
))
def
test_graph_bprop1
(
self
):
x
=
core
.
wrap
(
numpy
.
random
.
rand
(
10
,
2
))
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
))
def
test_graph_bprop0
(
self
):
x
=
core
.
wrap
(
numpy
.
random
.
rand
(
10
,
2
))
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
))
gy
=
y
-
x
g
=
grad
.
Grad
({
y
:
gy
})
g
.
bprop
()
lr
=
0.002
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
))
def
test_graph_bprop1
(
self
):
x
=
core
.
wrap
(
numpy
.
random
.
rand
(
10
,
2
))
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__'
:
unittest
.
main
()
gof/op.py
浏览文件 @
f5801429
...
...
@@ -144,7 +144,7 @@ class Op(object):
TODO: consider moving this function to the python linker.
"""
res
=
self
.
impl
(
*
[
input
.
data
for
input
in
self
.
inputs
])
if
self
.
nout
==
1
:
if
len
(
self
.
outputs
)
==
1
:
self
.
outputs
[
0
]
.
data
=
res
else
:
assert
len
(
res
)
==
len
(
self
.
outputs
)
...
...
sparse.py
浏览文件 @
f5801429
差异被折叠。
点击展开。
tensor.py
浏览文件 @
f5801429
...
...
@@ -70,6 +70,8 @@ class Tensor(BaseTensor):
# alternate Tensor constructor
def
astensor
(
data
,
broadcastable
=
None
,
role
=
None
,
name
=
None
):
"""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
)
if
broadcastable
is
None
:
broadcastable
=
[
s
==
1
for
s
in
data
.
shape
]
...
...
@@ -116,11 +118,9 @@ def _assert_tensor_scalar(x, a):
if
numpy
.
product
(
a
.
shape
)
!=
1
:
raise
ValueError
(
"The second argument must be a scalar."
)
def
_as_tensor
(
obj
):
if
isinstance
(
obj
,
Tensor
):
return
obj
else
:
return
astensor
(
obj
)
# this has a different name, because _as_tensor is the function which ops use
# to upcast their arguments... this internal-use function is a good place to put debugging stuff, better than the global astensor.
_as_tensor
=
astensor
class
_Op
(
BaseTensorOp
):
"""A convenient base for the ops in this file"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论