Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
98309232
提交
98309232
authored
7月 24, 2012
作者:
Nicolas Bouchard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix binomial and moved out of sandbox.
上级
d5d90999
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
94 行增加
和
109 行删除
+94
-109
basic.py
theano/sparse/basic.py
+52
-0
sp2.py
theano/sparse/sandbox/sp2.py
+1
-68
test_basic.py
theano/sparse/tests/test_basic.py
+41
-1
test_sp2.py
theano/sparse/tests/test_sp2.py
+0
-40
没有找到文件。
theano/sparse/basic.py
浏览文件 @
98309232
...
@@ -3005,6 +3005,58 @@ class Poisson(gof.op.Op):
...
@@ -3005,6 +3005,58 @@ class Poisson(gof.op.Op):
poisson
=
Poisson
()
poisson
=
Poisson
()
class
Binomial
(
gof
.
op
.
Op
):
"""Return a sparse matrix having random values from a binomial
density having number of experiment `n` and probability of succes
`p`.
:param n: Tensor scalar representing the number of experiment.
:param p: Tensor scalar representing the probability of success.
:param shape: Tensor vector for the output shape.
:return: A sparse matrix of integers representing the number
of success.
"""
def
__init__
(
self
,
format
,
dtype
):
self
.
format
=
format
self
.
dtype
=
dtype
def
__eq__
(
self
,
other
):
return
((
type
(
self
)
==
type
(
other
))
and
self
.
format
==
other
.
format
and
self
.
dtype
==
other
.
dtype
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
format
)
^
hash
(
self
.
dtype
)
def
make_node
(
self
,
n
,
p
,
shape
):
n
=
tensor
.
as_tensor_variable
(
n
)
p
=
tensor
.
as_tensor_variable
(
p
)
shape
=
tensor
.
as_tensor_variable
(
shape
)
return
gof
.
Apply
(
self
,
[
n
,
p
,
shape
],
[
SparseType
(
dtype
=
self
.
dtype
,
format
=
self
.
format
)
.
make_variable
()])
def
perform
(
self
,
node
,
(
n
,
p
,
shape
,
),
(
out
,
)):
binomial
=
numpy
.
random
.
binomial
(
n
,
p
,
size
=
shape
)
csx_matrix
=
getattr
(
scipy
.
sparse
,
self
.
format
+
'_matrix'
)
out
[
0
]
=
csx_matrix
(
binomial
,
dtype
=
self
.
dtype
)
def
grad
(
self
,
(
n
,
p
,
shape
,
),
(
gz
,)):
return
None
,
None
,
None
def
infer_shape
(
self
,
node
,
ins_shapes
):
return
[(
node
.
inputs
[
2
][
0
],
node
.
inputs
[
2
][
1
])]
def
__str__
(
self
):
return
self
.
__class__
.
__name__
csr_fbinomial
=
Binomial
(
'csr'
,
'float32'
)
csc_fbinomial
=
Binomial
(
'csc'
,
'float32'
)
csr_dbinomial
=
Binomial
(
'csr'
,
'float64'
)
csc_dbinomial
=
Binomial
(
'csc'
,
'float64'
)
class
Multinomial
(
gof
.
op
.
Op
):
class
Multinomial
(
gof
.
op
.
Op
):
"""Return a sparse matrix having random values from a multinomial
"""Return a sparse matrix having random values from a multinomial
density having number of experiment `n` and probability of succes
density having number of experiment `n` and probability of succes
...
...
theano/sparse/sandbox/sp2.py
浏览文件 @
98309232
...
@@ -19,6 +19,7 @@ from theano.sparse.basic import (
...
@@ -19,6 +19,7 @@ from theano.sparse.basic import (
AddSSData
,
add_s_s_data
,
AddSSData
,
add_s_s_data
,
MulSDCSC
,
mul_s_d_csc
,
MulSDCSR
,
mul_s_d_csr
,
MulSDCSC
,
mul_s_d_csc
,
MulSDCSR
,
mul_s_d_csr
,
Multinomial
,
multinomial
,
Poisson
,
poisson
,
Multinomial
,
multinomial
,
Poisson
,
poisson
,
Binomial
,
csr_fbinomial
,
csc_fbinomial
,
csr_dbinomial
,
csc_dbinomial
,
structured_monoid
,
structured_monoid
,
structured_sigmoid
,
structured_exp
,
structured_log
,
structured_pow
,
structured_sigmoid
,
structured_exp
,
structured_log
,
structured_pow
,
structured_minimum
,
structured_maximum
,
structured_add
,
structured_minimum
,
structured_maximum
,
structured_add
,
...
@@ -35,71 +36,3 @@ from theano.sparse.opt import (
...
@@ -35,71 +36,3 @@ from theano.sparse.opt import (
# Alias to maintain compatibility
# Alias to maintain compatibility
EliminateZeros
=
Remove0
EliminateZeros
=
Remove0
eliminate_zeros
=
remove0
eliminate_zeros
=
remove0
class
Binomial
(
gof
.
op
.
Op
):
# TODO This op is not an equivalent of numpy.random.binomial. In
# facts, this does not follow a binomial distribution at all.
# To see it, just try with p = 1.
"""Return a sparse matrix having random values from a binomial
density having number of experiment `n` and probability of succes
`p`.
.. warning::
For now, this op does not return a true binomial
distribution. It is a random disposition of ones
in a sparse matrix.
:param n: Tensor scalar representing the number of experiment.
:param p: Tensor scalar representing the probability of success.
:param shape: Tensor vector for the output shape.
:return: A sparse matrix of integers representing the number
of success.
"""
def
__init__
(
self
,
format
,
dtype
):
self
.
format
=
format
self
.
dtype
=
dtype
def
__eq__
(
self
,
other
):
return
((
type
(
self
)
==
type
(
other
))
and
self
.
format
==
other
.
format
and
self
.
dtype
==
other
.
dtype
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
format
)
^
hash
(
self
.
dtype
)
def
make_node
(
self
,
n
,
p
,
shape
):
n
=
tensor
.
as_tensor_variable
(
n
)
p
=
tensor
.
as_tensor_variable
(
p
)
shape
=
tensor
.
as_tensor_variable
(
shape
)
return
gof
.
Apply
(
self
,
[
n
,
p
,
shape
],
[
SparseType
(
dtype
=
self
.
dtype
,
format
=
self
.
format
)
.
make_variable
()])
def
perform
(
self
,
node
,
(
n
,
p
,
shape
,
),
(
out
,
)):
N
=
n
*
p
*
shape
[
0
]
*
shape
[
1
]
data
=
numpy
.
ones
(
N
,
dtype
=
self
.
dtype
)
row
=
numpy
.
random
.
randint
(
0
,
shape
[
0
],
N
)
col
=
numpy
.
random
.
randint
(
0
,
shape
[
1
],
N
)
res
=
scipy
.
sparse
.
coo_matrix
((
data
,
(
row
,
col
)),
shape
=
shape
)
out
[
0
]
=
getattr
(
res
,
'to'
+
self
.
format
)()
out
[
0
]
.
data
=
numpy
.
ones_like
(
out
[
0
]
.
data
)
def
grad
(
self
,
(
n
,
p
,
shape
,
),
(
gz
,)):
return
None
,
None
,
None
def
infer_shape
(
self
,
node
,
ins_shapes
):
return
[(
node
.
inputs
[
2
][
0
],
node
.
inputs
[
2
][
1
])]
def
__str__
(
self
):
return
self
.
__class__
.
__name__
csr_fbinomial
=
Binomial
(
'csr'
,
'float32'
)
csc_fbinomial
=
Binomial
(
'csc'
,
'float32'
)
csr_dbinomial
=
Binomial
(
'csr'
,
'float64'
)
csc_dbinomial
=
Binomial
(
'csc'
,
'float64'
)
theano/sparse/tests/test_basic.py
浏览文件 @
98309232
...
@@ -33,7 +33,7 @@ from theano.sparse import (
...
@@ -33,7 +33,7 @@ from theano.sparse import (
Dot
,
Usmm
,
UsmmCscDense
,
sp_ones_like
,
GetItemScalar
,
Dot
,
Usmm
,
UsmmCscDense
,
sp_ones_like
,
GetItemScalar
,
SparseFromDense
,
SparseFromDense
,
Cast
,
HStack
,
VStack
,
AddSSData
,
add_s_s_data
,
Cast
,
HStack
,
VStack
,
AddSSData
,
add_s_s_data
,
Poisson
,
poisson
,
Multinomial
,
multinomial
,
Poisson
,
poisson
,
Binomial
,
Multinomial
,
multinomial
,
structured_sigmoid
,
structured_exp
,
structured_log
,
structured_sigmoid
,
structured_exp
,
structured_log
,
structured_pow
,
structured_minimum
,
structured_maximum
,
structured_add
,
structured_pow
,
structured_minimum
,
structured_maximum
,
structured_add
,
MulSV
,
mul_s_v
,
StructuredAddSV
,
structured_add_s_v
,
MulSV
,
mul_s_v
,
StructuredAddSV
,
structured_add_s_v
,
...
@@ -2142,6 +2142,46 @@ class PoissonTester(utt.InferShapeTester):
...
@@ -2142,6 +2142,46 @@ class PoissonTester(utt.InferShapeTester):
self
.
op_class
)
self
.
op_class
)
class
BinomialTester
(
utt
.
InferShapeTester
):
n
=
tensor
.
scalar
()
p
=
tensor
.
scalar
()
shape
=
tensor
.
lvector
()
_n
=
5
_p
=
.
25
_shape
=
numpy
.
asarray
([
3
,
5
],
dtype
=
'int64'
)
inputs
=
[
n
,
p
,
shape
]
_inputs
=
[
_n
,
_p
,
_shape
]
def
setUp
(
self
):
super
(
BinomialTester
,
self
)
.
setUp
()
self
.
op_class
=
Binomial
def
test_op
(
self
):
for
sp_format
in
sparse
.
sparse_formats
:
for
o_type
in
sparse
.
float_dtypes
:
f
=
theano
.
function
(
self
.
inputs
,
Binomial
(
sp_format
,
o_type
)(
*
self
.
inputs
))
tested
=
f
(
*
self
.
_inputs
)
assert
tested
.
shape
==
tuple
(
self
.
_shape
)
assert
tested
.
format
==
sp_format
assert
tested
.
dtype
==
o_type
assert
numpy
.
allclose
(
numpy
.
floor
(
tested
.
todense
()),
tested
.
todense
())
def
test_infer_shape
(
self
):
for
sp_format
in
sparse
.
sparse_formats
:
for
o_type
in
sparse
.
float_dtypes
:
self
.
_compile_and_check
(
self
.
inputs
,
[
Binomial
(
sp_format
,
o_type
)(
*
self
.
inputs
)],
self
.
_inputs
,
self
.
op_class
)
class
MultinomialTester
(
utt
.
InferShapeTester
):
class
MultinomialTester
(
utt
.
InferShapeTester
):
p
=
sparse
.
csr_matrix
()
p
=
sparse
.
csr_matrix
()
_p
=
sp
.
csr_matrix
(
numpy
.
asarray
([[
0.0
,
0.5
,
0.0
,
0.5
],
_p
=
sp
.
csr_matrix
(
numpy
.
asarray
([[
0.0
,
0.5
,
0.0
,
0.5
],
...
...
theano/sparse/tests/test_sp2.py
浏览文件 @
98309232
...
@@ -22,45 +22,5 @@ from theano.tests import unittest_tools as utt
...
@@ -22,45 +22,5 @@ from theano.tests import unittest_tools as utt
from
theano.sparse.basic
import
verify_grad_sparse
from
theano.sparse.basic
import
verify_grad_sparse
class
BinomialTester
(
utt
.
InferShapeTester
):
n
=
tensor
.
scalar
()
p
=
tensor
.
scalar
()
shape
=
tensor
.
lvector
()
_n
=
5
_p
=
.
25
_shape
=
np
.
asarray
([
3
,
5
],
dtype
=
'int64'
)
inputs
=
[
n
,
p
,
shape
]
_inputs
=
[
_n
,
_p
,
_shape
]
def
setUp
(
self
):
super
(
BinomialTester
,
self
)
.
setUp
()
self
.
op_class
=
S2
.
Binomial
def
test_op
(
self
):
for
sp_format
in
sparse
.
sparse_formats
:
for
o_type
in
sparse
.
float_dtypes
:
f
=
theano
.
function
(
self
.
inputs
,
S2
.
Binomial
(
sp_format
,
o_type
)(
*
self
.
inputs
))
tested
=
f
(
*
self
.
_inputs
)
assert
tested
.
shape
==
tuple
(
self
.
_shape
)
assert
tested
.
format
==
sp_format
assert
tested
.
dtype
==
o_type
assert
np
.
allclose
(
np
.
floor
(
tested
.
todense
()),
tested
.
todense
())
def
test_infer_shape
(
self
):
for
sp_format
in
sparse
.
sparse_formats
:
for
o_type
in
sparse
.
float_dtypes
:
self
.
_compile_and_check
(
self
.
inputs
,
[
S2
.
Binomial
(
sp_format
,
o_type
)(
*
self
.
inputs
)],
self
.
_inputs
,
self
.
op_class
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论