提交 c2905e7b authored 作者: boulanni's avatar boulanni

linalg

上级 71a8a1c9
from ops import (cholesky, matrix_inverse, solve,
diag, extract_diag, alloc_diag,
det, PSD_hint,
trace, spectral_radius_bound)
差异被折叠。
import numpy
from theano import tensor, function
from .ops import *
if 0:
def test_cholesky():
#TODO: test upper and lower triangular
#todo: unittest randomseed
rng = numpy.random.RandomState(1234)
r = rng.randn(5,5)
pd = numpy.dot(r,r.T)
x = tensor.matrix()
chol = Cholesky()(x)
f = function([x], tensor.dot(chol, chol.T)) # an optimization could remove this
ch_f = function([x], chol)
# quick check that chol is upper-triangular
ch = ch_f(pd)
print ch
assert ch[0,4] != 0
assert ch[4,0] == 0
assert numpy.allclose(numpy.dot(ch.T,ch),pd)
assert not numpy.allclose(numpy.dot(ch,ch.T),pd)
def test_inverse_correctness():
#todo: unittest randomseed
rng = numpy.random.RandomState(12345)
r = rng.randn(4,4)
x = tensor.matrix()
xi = matrix_inverse(x)
ri = function([x], xi)(r)
assert ri.shape == r.shape
assert ri.dtype == r.dtype
rir = numpy.dot(ri,r)
rri = numpy.dot(r,ri)
assert numpy.allclose(numpy.identity(4), rir), rir
assert numpy.allclose(numpy.identity(4), rri), rri
def test_inverse_grad():
rng = numpy.random.RandomState(1234)
r = rng.randn(4,4)
tensor.verify_grad(matrix_inverse, [r], rng=numpy.random)
def test_det_grad():
rng = numpy.random.RandomState(1234)
r = rng.randn(5,5)
tensor.verify_grad(det, [r], rng=numpy.random)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论