def test_spectral_radius_bound(): tol = 10**(-6) rng = np.random.RandomState(utt.fetch_seed()) x = theano.tensor.matrix() radius_bound = spectral_radius_bound(x, 5) f = theano.function([x], radius_bound) shp = (3, 4) m = rng.rand(*shp) m = np.cov(m).astype(config.floatX) radius_bound_theano = f(m) # test the approximation mm = m for i in range(5): mm = np.dot(mm, mm) radius_bound_numpy = np.trace(mm)**(2**(-5)) assert abs(radius_bound_numpy - radius_bound_theano) < tol # test the bound eigen_val = numpy.linalg.eig(m) assert (eigen_val[0].max() - radius_bound_theano) < tol # test type errors xx = theano.tensor.vector() ok = False try: spectral_radius_bound(xx, 5) except TypeError: ok = True assert ok ok = False try: spectral_radius_bound(x, 5.0) except TypeError: ok = True assert ok # test value error ok = False try: spectral_radius_bound(x, -5) except ValueError: ok = True assert ok
def test_spectral_radius_bound(): tol = 10 ** (-6) rng = numpy.random.RandomState(utt.fetch_seed()) x = theano.tensor.matrix() radius_bound = spectral_radius_bound(x, 5) f = theano.function([x], radius_bound) shp = (3, 4) m = rng.rand(*shp) m = numpy.cov(m).astype(config.floatX) radius_bound_theano = f(m) # test the approximation mm = m for i in range(5): mm = numpy.dot(mm, mm) radius_bound_numpy = numpy.trace(mm) ** (2 ** (-5)) assert abs(radius_bound_numpy - radius_bound_theano) < tol # test the bound eigen_val = numpy.linalg.eig(m) assert (eigen_val[0].max() - radius_bound_theano) < tol # test type errors xx = theano.tensor.vector() ok = False try: spectral_radius_bound(xx, 5) except TypeError: ok = True assert ok ok = False try: spectral_radius_bound(x, 5.) except TypeError: ok = True assert ok # test value error ok = False try: spectral_radius_bound(x, -5) except ValueError: ok = True assert ok