| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
|
| 5 |
np.random.seed(0) |
| 6 |
from numpy.testing import assert_allclose |
| 7 |
|
| 8 |
from hypothesis import given, assume |
| 9 |
from hypothesis.strategies import tuples, lists, integers, floats |
| 10 |
from hypothesis.extra.numpy import arrays |
| 11 |
|
| 12 |
|
| 13 |
def lengths(lo=1, hi=10): |
| 14 |
return integers(min_value=lo, max_value=hi) |
| 15 |
|
| 16 |
|
| 17 |
def shapes(min_rows=1, max_rows=100, min_cols=1, max_cols=100): |
| 18 |
return tuples(lengths(lo=min_rows, hi=max_rows), lengths(lo=min_cols, hi=max_cols)) |
| 19 |
|
| 20 |
|
| 21 |
def ndarrays_of_shape(shape, lo=-1000.0, hi=1000.0, dtype='float64'): |
| 22 |
return arrays(dtype, shape=shape, elements=floats(min_value=lo, max_value=hi, width=32)) |
| 23 |
|
| 24 |
|
| 25 |
def ndarrays(min_len=0, max_len=10, min_val=-10000000.0, max_val=1000000.0, dtype='float64'): |
| 26 |
return lengths(lo=min_len, hi=max_len).flatmap( |
| 27 |
lambda n: ndarrays_of_shape(n, lo=min_val, hi=max_val, dtype=dtype)) |
| 28 |
|
| 29 |
|
| 30 |
def matrices(min_rows=1, max_rows=10, min_cols=1, max_cols=10, |
| 31 |
min_value=-10000000.0, max_value=1000000.0, dtype='float64'): |
| 32 |
return shapes(min_rows=min_rows, max_rows=max_rows, |
| 33 |
min_cols=min_cols, max_cols=max_cols).flatmap( |
| 34 |
lambda mn: ndarrays_of_shape(mn, lo=min_value, hi=max_value, |
| 35 |
dtype=dtype)) |
| 36 |
|
| 37 |
|
| 38 |
def positive_ndarrays(min_len=0, max_len=10, max_val=100000.0, dtype='float64'): |
| 39 |
return ndarrays(min_len=min_len, max_len=max_len, min_val=0, max_val=max_val, |
| 40 |
dtype=dtype) |
| 41 |
|
| 42 |
|
| 43 |
def negative_ndarrays(min_len=0, max_len=10, min_val=-100000.0, dtype='float64'): |
| 44 |
return ndarrays(min_len=min_len, max_len=max_len, min_val=min_val, max_val=-1e-10, |
| 45 |
dtype=dtype) |
| 46 |
|
| 47 |
|
| 48 |
def parse_layer(layer_data): |
| 49 |
# Get the first row, excluding the first column |
| 50 |
x = layer_data[0,1:] |
| 51 |
# Get the first column, excluding the first row |
| 52 |
# .ascontiguousarray is support important here!!!! |
| 53 |
b = np.ascontiguousarray(layer_data[1:,0], dtype='float64') |
| 54 |
# Slice out the row and the column used for the X and the bias |
| 55 |
W = layer_data[1:,1:] |
| 56 |
assert x.ndim == 1 |
| 57 |
assert b.ndim == 1 |
| 58 |
assert b.shape[0] == W.shape[0] |
| 59 |
assert x.shape[0] == W.shape[1] |
| 60 |
assume(not np.isnan(W.sum())) |
| 61 |
assume(not np.isnan(x.sum())) |
| 62 |
assume(not np.isnan(b.sum())) |
| 63 |
assume(not any(np.isinf(val) for val in W.flatten())) |
| 64 |
assume(not any(np.isinf(val) for val in x)) |
| 65 |
assume(not any(np.isinf(val) for val in b)) |
| 66 |
return x, b, W |
| 67 |
|
| 68 |
|
| 69 |
def split_row(layer_data): |
| 70 |
return (layer_data[0,:], layer_data[:,:]) |
| 71 |
|
| 72 |
|