Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3f3704ef
提交
3f3704ef
authored
6月 26, 2012
作者:
Nicolas Bouchard
提交者:
Frederic
7月 06, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add MultinomialTester and some corrections to Multinomial.
上级
b435dbd6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
60 行增加
和
7 行删除
+60
-7
sp2.py
theano/sparse/sandbox/sp2.py
+23
-7
test_sp2.py
theano/sparse/tests/test_sp2.py
+37
-0
没有找到文件。
theano/sparse/sandbox/sp2.py
浏览文件 @
3f3704ef
...
...
@@ -582,10 +582,17 @@ class Multinomial(gof.op.Op):
density having number of experiment `n` and probability of succes
`p`.
:param n: Number of experiment.
:param p: Sparse matrix of probability for each of the different outcomes.
:param n: Tensor type vector or scalar representing the number of
experiment for each row. If `n` is a scalar, it will be
used for each row.
:param p: Sparse matrix of probability where each row is a probability
vector representing the probability of succes. N.B. Each row
must sum to one.
:return: A sparse matrix of random integers of a multinomial density.
:return: A sparse matrix of random integers from a multinomial density
for each row.
:note: It will works only if `p` have csr format.
"""
def
__eq__
(
self
,
other
):
...
...
@@ -607,15 +614,24 @@ class Multinomial(gof.op.Op):
raise
NotImplemented
()
out
[
0
]
=
p
.
copy
()
for
i
in
xrange
(
p
.
shape
[
0
]):
k
,
l
=
p
.
indptr
[
i
],
p
.
indptr
[
i
+
1
]
out
[
0
]
.
data
[
k
:
l
]
=
numpy
.
random
.
multinomial
(
n
[
i
],
p
.
data
[
k
:
l
])
if
n
.
ndim
==
0
:
for
i
in
xrange
(
p
.
shape
[
0
]):
k
,
l
=
p
.
indptr
[
i
],
p
.
indptr
[
i
+
1
]
out
[
0
]
.
data
[
k
:
l
]
=
numpy
.
random
.
multinomial
(
n
,
p
.
data
[
k
:
l
])
elif
n
.
ndim
==
1
:
if
n
.
shape
[
0
]
!=
p
.
shape
[
0
]:
raise
ValueError
(
'The number of element of n must be '
'the same as the number of row of p.'
)
for
i
in
xrange
(
p
.
shape
[
0
]):
k
,
l
=
p
.
indptr
[
i
],
p
.
indptr
[
i
+
1
]
out
[
0
]
.
data
[
k
:
l
]
=
numpy
.
random
.
multinomial
(
n
[
i
],
p
.
data
[
k
:
l
])
def
grad
(
self
,
inputs
,
outputs_gradients
):
return
[
None
,
None
]
def
infer_shape
(
self
,
node
,
ins_shapes
):
return
ins_shapes
return
[
ins_shapes
[
1
]]
def
__str__
(
self
):
return
self
.
__class__
.
__name__
...
...
theano/sparse/tests/test_sp2.py
浏览文件 @
3f3704ef
...
...
@@ -288,6 +288,43 @@ class PoissonTester(utt.InferShapeTester):
self
.
op_class
)
class
MultinomialTester
(
utt
.
InferShapeTester
):
p
=
sparse
.
csr_matrix
()
_p
=
sp
.
csr_matrix
(
np
.
asarray
([[
0.0
,
0.5
,
0.0
,
0.5
],
[
0.1
,
0.2
,
0.3
,
0.4
],
[
0.0
,
1.0
,
0.0
,
0.0
],
[
0.3
,
0.3
,
0.0
,
0.4
]]))
def
setUp
(
self
):
super
(
MultinomialTester
,
self
)
.
setUp
()
self
.
op_class
=
S2
.
Multinomial
def
test_op
(
self
):
n
=
tensor
.
lscalar
()
f
=
theano
.
function
([
self
.
p
,
n
],
S2
.
multinomial
(
n
,
self
.
p
))
_n
=
5
tested
=
f
(
self
.
_p
,
_n
)
assert
tested
.
shape
==
self
.
_p
.
shape
assert
np
.
allclose
(
np
.
floor
(
tested
.
todense
()),
tested
.
todense
())
assert
tested
[
2
,
1
]
==
_n
n
=
tensor
.
lvector
()
f
=
theano
.
function
([
self
.
p
,
n
],
S2
.
multinomial
(
n
,
self
.
p
))
_n
=
np
.
asarray
([
1
,
2
,
3
,
4
],
dtype
=
'int64'
)
tested
=
f
(
self
.
_p
,
_n
)
assert
tested
.
shape
==
self
.
_p
.
shape
assert
np
.
allclose
(
np
.
floor
(
tested
.
todense
()),
tested
.
todense
())
assert
tested
[
2
,
1
]
==
_n
[
2
]
def
test_infer_shape
(
self
):
self
.
_compile_and_check
([
self
.
p
],
[
S2
.
multinomial
(
5
,
self
.
p
)],
[
self
.
_p
],
self
.
op_class
)
class
StructuredAddSVTester
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论