Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e596e80e
提交
e596e80e
authored
8月 19, 2017
作者:
Boris Fomitchev
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'upstream/master' into tensor_op
上级
0be8ae68
c034ad2b
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
162 行增加
和
0 行删除
+162
-0
basic.py
theano/tensor/basic.py
+61
-0
test_basic.py
theano/tensor/tests/test_basic.py
+101
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
e596e80e
...
@@ -2201,6 +2201,67 @@ def sqr(a):
...
@@ -2201,6 +2201,67 @@ def sqr(a):
square
=
sqr
square
=
sqr
def
cov
(
m
,
y
=
None
,
rowvar
=
True
,
bias
=
False
,
ddof
=
None
,
fweights
=
None
,
aweights
=
None
):
"""Calculate the covariance matrix.
Covariance indicates the level to which two variables vary together.
If we examine N-dimensional samples, :math:`m = [x_1, x_2, ... x_N]^T`,
then the covariance matrix element :math:`C_{ij}` is the covariance of
:math:`x_i` and :math:`x_j`. The element :math:`C_{ii}` is the variance
of :math:`x_i`. Code and docstring ported from numpy.
----------
m : array_like
A 2-D array containing multiple variables and observations.
Each row of `m` represents a variable, and each column is
observations of all those variables.
y : array_like, optional
An additional set of variables and observations. `y` has the same form
as that of `m`.
rowvar : bool, optional
If `rowvar` is True (default), then each row represents a
variable, with observations in the columns. Otherwise, the relationship
is transposed: each column represents a variable, while the rows
contain observations.
bias : bool, optional
Default normalization (False) is by ``(N - 1)``, where ``N`` is the
number of observations given (unbiased estimate). If `bias` is True, then
normalization is by ``N``. These values can be overridden by using the
keyword ``ddof``.
ddof : int, optional
If not ``None`` the default value implied by `bias` is overridden.
The default value is ``None``.
Returns
-------
out : The covariance matrix of the variables.
"""
if
fweights
is
not
None
:
raise
NotImplementedError
(
'fweights are not implemented'
)
if
aweights
is
not
None
:
raise
NotImplementedError
(
'aweights are not implemented'
)
if
not
rowvar
and
m
.
shape
[
0
]
!=
1
:
m
=
m
.
T
if
y
is
not
None
:
if
not
rowvar
and
y
.
shape
[
0
]
!=
1
:
y
=
y
.
T
m
=
theano
.
tensor
.
concatenate
((
m
,
y
),
axis
=
0
)
if
ddof
is
None
:
if
not
bias
:
ddof
=
1
else
:
ddof
=
0
# Determine the normalization
fact
=
m
.
shape
[
1
]
-
ddof
m
-=
m
.
mean
(
axis
=
1
,
keepdims
=
1
)
c
=
m
.
dot
(
m
.
T
)
c
*=
theano
.
tensor
.
constant
(
1
)
/
fact
return
c
.
squeeze
()
@_scal_elemwise
@_scal_elemwise
def
sqrt
(
a
):
def
sqrt
(
a
):
"""square root of a"""
"""square root of a"""
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
e596e80e
...
@@ -8226,6 +8226,107 @@ def test_norm():
...
@@ -8226,6 +8226,107 @@ def test_norm():
assert
np
.
allclose
(
f
([
1
,
1
]),
np
.
sqrt
(
2
))
assert
np
.
allclose
(
f
([
1
,
1
]),
np
.
sqrt
(
2
))
class
test_cov
(
unittest
.
TestCase
):
def
test_core
(
self
):
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
)
f
=
theano
.
function
([
x
],
c
)
# basic cov function
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
1
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
def
test_rowvar
(
self
):
for
rowvar
in
[
True
,
False
]:
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
rowvar
=
rowvar
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
rowvar
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
rowvar
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
rowvar
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
rowvar
))
# check when variables are along the first axis
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
rowvar
=
False
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
1
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
False
))
def
test_y
(
self
):
# test y
x
=
theano
.
tensor
.
matrix
(
'x'
)
y
=
theano
.
tensor
.
matrix
(
'y'
)
c
=
theano
.
tensor
.
cov
(
x
,
y
=
y
)
f
=
theano
.
function
([
x
,
y
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
y
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
,
y
),
np
.
cov
(
data
,
y
=
y
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
y
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
,
y
),
np
.
cov
(
data
,
y
=
y
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
y
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
,
y
),
np
.
cov
(
data
,
y
=
y
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
y
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
,
y
),
np
.
cov
(
data
,
y
=
y
))
def
test_ddof
(
self
):
for
ddof
in
range
(
0
,
5
):
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
ddof
=
ddof
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
ddof
=
ddof
))
def
test_bias
(
self
):
for
bias
in
[
True
,
False
]:
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
bias
=
bias
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
bias
=
bias
))
for
ddof
in
range
(
0
,
5
):
for
bias
in
[
True
,
False
]:
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
ddof
=
ddof
,
bias
=
bias
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
ddof
=
ddof
,
bias
=
bias
))
class
test_ptp
(
unittest
.
TestCase
):
class
test_ptp
(
unittest
.
TestCase
):
def
test_scalar
(
self
):
def
test_scalar
(
self
):
# Should return 0 for all scalar
# Should return 0 for all scalar
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论