def validate_all_samples(): # Run some checks on the samples, this should help catch any bugs layers = [ model.visible_layer ] + model.hidden_layers def check_batch_size(l): if isinstance(l, (list, tuple)): map(check_batch_size, l) else: assert l.get_value().shape[0] == m for layer in layers: state = layer_to_state[layer] space = layer.get_total_state_space() space.validate(state) if 'DenseMaxPool' in str(type(layer)): p, h = state p = p.get_value() h = h.get_value() assert np.all(p == h) assert is_binary(p) if 'BinaryVisLayer' in str(type(layer)): v = state.get_value() assert is_binary(v) if 'Softmax' in str(type(layer)): y = state.get_value() assert is_binary(y) s = y.sum(axis=1) assert np.all(s == 1 ) if 'Ising' in str(type(layer)): s = state.get_value() assert is_binary((s + 1.) / 2.)
def check_gaussian_samples(value, nsamples, nvis, rows, cols, channels, expected_mean, tol): """ Tests that a matrix of Gaussian samples (observations in rows, variables in columns) 1) Has the right shape 2) Is not binary 3) Converges to the right mean """ if nvis: expected_shape = (nsamples, nvis) else: expected_shape = (nsamples,rows,cols,channels) assert value.shape == expected_shape assert not is_binary(value) mean = value.mean(axis=0) max_error = np.abs(mean-expected_mean).max() print 'Actual mean:' print mean print 'Expected mean:' print expected_mean print 'Maximal error:', max_error print 'Tolerable variance:', tol if max_error > tol: raise ValueError("Samples don't seem to have the right mean.") else: print 'Mean is within expected range'
def check_gaussian_samples(value, nsamples, nvis, rows, cols, channels, expected_mean, tol): """ Tests that a matrix of Gaussian samples (observations in rows, variables in columns) 1) Has the right shape 2) Is not binary 3) Converges to the right mean """ if nvis: expected_shape = (nsamples, nvis) else: expected_shape = (nsamples, rows, cols, channels) assert value.shape == expected_shape assert not is_binary(value) mean = value.mean(axis=0) max_error = np.abs(mean - expected_mean).max() print 'Actual mean:' print mean print 'Expected mean:' print expected_mean print 'Maximal error:', max_error print 'Tolerable variance:', tol if max_error > tol: raise ValueError("Samples don't seem to have the right mean.") else: print 'Mean is within expected range'
def check_binary_samples(value, expected_shape, expected_mean, tol): """ Tests that a matrix of binary samples (observations in rows, variables in columns) 1) Has the right shape 2) Is binary 3) Converges to the right mean """ assert value.shape == expected_shape assert is_binary(value) mean = value.mean(axis=0) max_error = np.abs(mean-expected_mean).max() if max_error > tol: print 'Actual mean:' print mean print 'Expected mean:' print expected_mean print 'Maximal error:', max_error raise ValueError("Samples don't seem to have the right mean.")
def check_multinomial_samples(value, expected_shape, expected_mean, tol): """ Tests that a matrix of multinomial samples (observations in rows, variables in columns) 1) Has the right shape 2) Is binary 3) Has one 1 per row 4) Converges to the right mean """ assert value.shape == expected_shape assert is_binary(value) assert np.all(value.sum(axis=1) == 1) mean = value.mean(axis=0) max_error = np.abs(mean-expected_mean).max() if max_error > tol: print('Actual mean:') print(mean) print('Expected mean:') print(expected_mean) print('Maximal error:', max_error) raise ValueError("Samples don't seem to have the right mean.")
def check_multinomial_samples(value, expected_shape, expected_mean, tol): """ Tests that a matrix of multinomial samples (observations in rows, variables in columns) 1) Has the right shape 2) Is binary 3) Has one 1 per row 4) Converges to the right mean """ assert value.shape == expected_shape assert is_binary(value) assert np.all(value.sum(axis=1) == 1) mean = value.mean(axis=0) max_error = np.abs(mean - expected_mean).max() if max_error > tol: print('Actual mean:') print(mean) print('Expected mean:') print(expected_mean) print('Maximal error:', max_error) raise ValueError("Samples don't seem to have the right mean.")
def check_bvmp_samples(value, num_samples, n, pool_size, mean, tol): """ bvmp=BinaryVectorMaxPool value: a tuple giving (pooled batch, detector batch) (all made with same params) num_samples: number of samples there should be in the batch n: detector layer dimension pool_size: size of each pool region mean: (expected value of pool unit, expected value of detector units) tol: amount the emprical mean is allowed to deviate from the analytical expectation checks that: 1) all values are binary 2) detector layer units are mutually exclusive 3) pooled unit is max of the detector units 4) correct number of samples is present 5) variables are of the right shapes 6) samples converge to the right expected value """ pv, hv = value assert n % pool_size == 0 num_pools = n // pool_size assert pv.ndim == 2 assert pv.shape[0] == num_samples assert pv.shape[1] == num_pools assert hv.ndim == 2 assert hv.shape[0] == num_samples assert hv.shape[1] == n assert is_binary(pv) assert is_binary(hv) for i in xrange(num_pools): sub_p = pv[:,i] assert sub_p.shape == (num_samples,) sub_h = hv[:,i*pool_size:(i+1)*pool_size] assert sub_h.shape == (num_samples, pool_size) if not np.all(sub_p == sub_h.max(axis=1)): for j in xrange(num_samples): print sub_p[j], sub_h[j,:] assert sub_p[j] == sub_h[j,:] assert False assert np.max(sub_h.sum(axis=1)) == 1 p, h = mean assert p.ndim == 1 assert h.ndim == 1 emp_p = pv.mean(axis=0) emp_h = hv.mean(axis=0) max_diff = np.abs(p - emp_p).max() if max_diff > tol: print 'expected value of pooling units: ',p print 'empirical expectation: ',emp_p print 'maximum difference: ',max_diff raise ValueError("Pooling unit samples have an unlikely mean.") max_diff = np.abs(h - emp_h).max() if max_diff > tol: assert False