def systemTestAffineCouplingMLP(): # Test real case scenario: batch = 4 d = 3 n = 2 z = tf.ones([batch, d, n, 1], dtype=DTYPE) model = AffineCoupling(shift_and_scale_model=MLP(activation=tf.nn.relu, mode="shift_and_scale"), split_dim=2, split_sizes=[1, 1]) x = model(z) assert_equal(x.shape, z.shape) # Another test with non-even splitting split_dim = 1 na = 1 nb = 2 model = AffineCoupling(shift_and_scale_model=MLP( activation=tf.nn.relu, mode="shift_and_scale", out_shape=[batch, na, n, 2]), split_dim=split_dim, split_sizes=[na, nb]) x = model(z) assert_equal(x.shape, z.shape) print('systemTestAffineCouplingMLP passed')
def _ready_litnodes(self): """Start two lit nodes and connect them.""" # Start lit node 0 and open websocket connection self.add_litnode() self.litnodes[0].args.extend([self.coins[0]["wallit_code"], "127.0.0.1"]) self.litnodes[0].start_node() self.litnodes[0].add_rpc_connection("127.0.0.1", "8001") # Start lit node 1 and open websocket connection self.add_litnode() self.litnodes[1].args.extend(["--rpcport", "8002", self.coins[0]["wallit_code"], "127.0.0.1"]) self.litnodes[1].start_node() self.litnodes[1].add_rpc_connection("127.0.0.1", "8002") self.log.info("Wait until lit nodes are sync'ed") wait_until(lambda: self.litnodes[0].get_height(self.coins[0]['code']) == 500) wait_until(lambda: self.litnodes[1].get_height(self.coins[0]['code']) == 500) self.log.info("Connect lit nodes") res = self.litnodes[0].Listen(Port="127.0.0.1:10001")["result"] self.litnodes[0].lit_address = res["Adr"] + '@' + res["LisIpPorts"][0] res = self.litnodes[1].Connect(LNAddr=self.litnodes[0].lit_address) assert not res['error'] # Check that litnode0 and litnode1 are connected wait_until(lambda: len(self.litnodes[0].ListConnections()['result']['Connections']) == 1) assert_equal(len(self.litnodes[1].ListConnections()['result']['Connections']), 1) self.log.info("lit nodes connected")
def testMLP(): input = tf.ones([3, 2, 3, 1]) # Test default: mode=symplectic_shift and activation=softplus model = MLP() out = model(input) assert (out.shape == input.shape) # We should be able to pass different minibatches input = tf.ones([15, 2, 3, 1]) out = model(input) assert (out.shape == input.shape) # input = tf.ones([3, 5, 6, 7, 1]) # Test default: mode=symplectic_shift and activation=softplus model = MLP() out = model(input) assert (out.shape == input.shape) # Test: mode=shift and activation=softplus model = MLP(mode="shift", activation="relu") out = model(input) assert (out.shape == input.shape) # Test: mode=shift_and_scale and activation=softplus model = MLP(mode="shift_and_scale", activation="relu") expected_shape = tf.constant([3, 5, 6, 7, 2]) assert_equal(tf.shape(model(input)), expected_shape) # with out_dim model = MLP(mode="shift_and_scale", activation="relu", out_shape=[3, 3, 2, 1, 32]) expected_shape = tf.constant([3, 3, 2, 1, 32]) assert_equal(tf.shape(model(input)), expected_shape) print('testMLP passed')
def test_open_toda(): x = tf.reshape(tf.range(1, 7, dtype=DTYPE), [1, 1, 3, 2]) # q = [1,3,5], p=[2,4,6] h = open_toda(x) expected_h = tf.constant(1 / 2 * (4 + 16 + 36) + np.exp(1. - 3.) + np.exp(3. - 5.), dtype=DTYPE) assert_equal(h, expected_h) print('test_open_toda passed')
def _ready_litnodes(self): """Start two lit nodes and connect them.""" # Start lit node 0 and open websocket connection self.log.info("Starting Lit Node 1") self.add_litnode() self.litnodes[0].args.extend( [self.coins[0]["wallit_code"], "127.0.0.1"]) self.litnodes[0].start_node() self.litnodes[0].add_rpc_connection("127.0.0.1", "8001") # Start lit node 1 and open websocket connection self.log.info("Starting Lit Node 2") self.add_litnode() self.litnodes[1].args.extend( ["--rpcport", "8002", self.coins[0]["wallit_code"], "127.0.0.1"]) self.litnodes[1].start_node() self.litnodes[1].add_rpc_connection("127.0.0.1", "8002") self.log.info("Wait until lit nodes are sync'ed") wait_until( lambda: self.litnodes[0].get_height(self.coins[0]['code']) == 500) wait_until( lambda: self.litnodes[1].get_height(self.coins[0]['code']) == 500) self.log.info("Listen on Node 0") res = self.litnodes[0].Listen(Port="127.0.0.1:10001")["result"] self.log.info("Try to connect to incorrect pkh") self.litnodes[ 0].lit_address = "ln1p7lhcxmlfgd5mltv6pc335aulv443tkw49q6er" + '@' + res[ "LisIpPorts"][0] failingRes = self.litnodes[1].Connect( LNAddr=self.litnodes[0].lit_address) assert failingRes['error'] self.log.info("Connect to correct pkh") self.litnodes[0].lit_address = res["Adr"] + '@' + res["LisIpPorts"][0] res = self.litnodes[1].Connect(LNAddr=self.litnodes[0].lit_address) assert not res['error'] time.sleep(1) #RPC timeout # Check that litnode0 and litnode1 are connected self.log.info("Waiting for nodes to connect to each other") wait_until(lambda: len(self.litnodes[0].ListConnections()['result'][ 'Connections']) == 1) #self.log.info("Does wait_until actually trigger?") #time.sleep(10) #RPC timeout, so this doesn't affect the program flow # Wait until both nodes are connected assert_equal( len(self.litnodes[1].ListConnections()['result']['Connections']), 1) self.log.info("lit nodes connected")
def _ready_coinnode(self): """Starts a coin node and makes sure it's segwit activated.""" # Start a coin node self.add_coinnode(self.coins[0]) self.coinnodes[0].start_node() self.log.info("Generate 500 blocks to activate segwit") self.coinnodes[0].generate(500) self.chain_height = 500 network_info = self.coinnodes[0].getblockchaininfo().json()['result'] assert_equal(network_info['bip9_softforks']['segwit']['status'], 'active')
def testLinearSymplectic(): # 2 samples, 5 particles in 2d. q = even numbers, p = odd numbers x = tf.reshape(tf.range(0, 16, dtype=DTYPE), shape=(2, 2, 2, 2)) model = LinearSymplectic() y = model(x) assert_equal(tf.shape(x), tf.shape(y)) x_inv = model.inverse(y) assert_allclose(x, x_inv, rtol=1e-6, atol=1e-5) # x = tf.reshape(x[0, :, :, :], [1, 2, 2, 2]) assert (is_symplectic(model, x, atol=1e-6)) print('testLinearSymplectic passed')
def _push_funds_through_channel(self): self.log.info("Now push some funds from lit node 0 to lit node 1") self.litnodes[0].Push(ChanIdx=1, Amt=100000000) litnode0_channel = self.litnodes[0].ChannelList( )['result']['Channels'][0] litnode1_channel = self.litnodes[1].ChannelList( )['result']['Channels'][0] assert_equal(litnode0_channel['MyBalance'], 900000000) assert_equal(litnode1_channel['MyBalance'], 100000000) self.log_channel_balance(self.litnodes[0], 0, self.litnodes[1], 0) self.log.info("Push some funds back") self.litnodes[1].Push(ChanIdx=1, Amt=50000000) litnode0_channel = self.litnodes[0].ChannelList( )['result']['Channels'][0] litnode1_channel = self.litnodes[1].ChannelList( )['result']['Channels'][0] assert_equal(litnode0_channel['MyBalance'], 950000000) assert_equal(litnode1_channel['MyBalance'], 50000000) self.log_channel_balance(self.litnodes[0], 0, self.litnodes[1], 0) self.log_balances(self.coins[0]['code'])
def _close_channel(self): self.log.info("Close channel") self.litnodes[0].CloseChannel(ChanIdx=1) self.confirm_transactions(self.coinnodes[0], self.litnodes[0], 1) # Make sure balances are as expected wait_until(lambda: abs(self.litnodes[1].get_balance(self.coins[0]['code'])['TxoTotal'] - 50000000) < self.coins[0]["feerate"] * 2000) litnode1_balance = self.litnodes[1].get_balance(self.coins[0]['code']) assert_equal(litnode1_balance['TxoTotal'], litnode1_balance['MatureWitty']) litnode0_balance = self.litnodes[0].get_balance(self.coins[0]['code']) assert abs(self.balance + 950000000 - litnode0_balance['TxoTotal']) < self.coins[0]["feerate"] * 2000 assert_equal(litnode0_balance['TxoTotal'], litnode0_balance['MatureWitty']) self.log_balances(self.coins[0]['code'])
def testZeroCenter(): # 2 samples, 5 particles in 2d. q = even numbers, p = odd numbers x = tf.reshape(tf.range(0, 16, dtype=DTYPE), shape=(2, 2, 2, 2)) # Test training mode mean_per_channel = tf.constant([7., 8.]) # At 1st update: decay = .99 moving_mean = mean_per_channel * (1 - decay) model = ZeroCenter() y = model(x) assert_equal(y, x + mean_per_channel) # 2nd update x = 0.5 * tf.reshape(tf.range(0, 16, dtype=DTYPE), shape=(2, 2, 2, 2)) mean_per_channel = 0.5 * mean_per_channel moving_mean = decay * moving_mean + (1 - decay) * mean_per_channel # moving_mean /= (1-decay**2) y = model(x) assert_equal(y, x + mean_per_channel) # Test prediction mode - still offset is zero. model.is_training = False y = model(x) assert_equal(y, x + moving_mean) assert_equal(x, model.inverse(y)) assert (is_symplectic( model, tf.reshape(tf.range(0, 20, dtype=DTYPE), [1, 2, 5, 2]))) print('testZeroCenter passed')
def testConstantShiftAndScale(): z = tf.constant([.1, 2, 3, .4], shape=(2, 2, 1, 1), dtype=DTYPE) model = ConstantShiftAndScale() # Test call x = model(z) assert_equal(x, z) # Since scale and shift are zero, identity assert_equal(model.inverse(x), z) # This is not robust as tf equal zeros is always true assert_equal(model.log_jacobian_det(z), tf.zeros((2, ), dtype=DTYPE)) # Scale only: model = ConstantShiftAndScale(shift=False) # Test call x = model(z) assert_equal(x, z) # Since scale zero, identity assert_equal(model.inverse(x), z) print("testConstantShiftAndScale passed")
def testChain(): batch_size = 3 x = tf.ones([batch_size, 1, 2]) bijectors = [SymplecticExchange() for i in range(3)] # Test call model = Chain(bijectors) y = model(x) # q,p -> p,-q -> -q,-p -> -p,q expected_y = tf.concat( [-tf.ones([batch_size, 1, 1]), tf.ones([batch_size, 1, 1])], 2) assert_equal(y, expected_y) # Test inverse inverted_y = model.inverse(y) assert_equal(x, inverted_y) # Test inverse stop_at inverted_y = model.inverse(y, stop_at=1) expected_x = tf.concat( [tf.ones([batch_size, 1, 1]), -tf.ones([batch_size, 1, 1])], 2) assert_equal(expected_x, inverted_y) # Test log jacobian determinant z = tf.ones([3, 2, 7, 1]) * 1.2345 # arbitrary n_models = 15 model = Chain([TimesTwoBijector() for i in range(n_models)]) expected_log_jac_det = n_models * tf.log(2.) * 2 * 7 * 1 * tf.ones( (3, ), dtype=tf.float32) assert_allclose(model.log_jacobian_det(z), expected_log_jac_det) # Test set_is_training n_bij = 3 class FakeBijectorWithIsTraining(): def __init__(self): self.is_training = True bijectors = [FakeBijectorWithIsTraining() for i in range(n_bij)] model = Chain(bijectors) model.set_is_training(False) for b in model.bijectors: assert_equal(b.is_training, False) model.set_is_training(True) for b in model.bijectors: assert_equal(b.is_training, True) print('testChain passed')
def render_frame(obs, smap, fmap=None, processed_obs=False): if processed_obs: obs = obs[..., -1] obs = obs.astype(np.float32) / 255 if processed_obs: if fmap is None: fmap = np.zeros_like(obs) else: fmap_max = fmap.max() if fmap_max: fmap = fmap / fmap_max assert_equal(obs.shape, smap.shape, fmap.shape) frame = np.stack([fmap, smap, obs], axis=-1) else: b, h, w, c = obs.shape assert c == 3 if fmap is None: assert_equal(obs.shape[:3], smap.shape) frame = 0.5 * (obs + smap[..., np.newaxis]) else: assert_equal(obs.shape[:3], smap.shape, fmap.shape) frame = 0.5 * obs + 0.5 * smap[..., np.newaxis] fmap_max = fmap.max() if fmap_max: frame[..., 0] = np.maximum(frame[..., 0], fmap / fmap_max) return frame
def testSqueezeAndShift(): x = tf.constant([1., 2.], shape=(1, 1, 2)) model = SqueezeAndShift(tf.keras.layers.Lambda(lambda x: x)) # Test call y = model(x) expected_y = tf.constant([1., 2. + 1.], shape=(1, 1, 2)) assert_equal(y, expected_y) # Test inverse inverted_y = model.inverse(y) assert_equal(x, inverted_y) # Test zero log jacobian determinant z = tf.reshape(tf.range(1, 11), shape=(5, 1, 2)) assert_equal(model.log_jacobian_det(z), tf.zeros([5], dtype=tf.int32)) print('testSqueezeAndShift passed')
def testSymplecticAdditiveCoupling(): x = tf.constant([1, 2], shape=(1, 1, 2)) model = SymplecticAdditiveCoupling(tf.keras.layers.Lambda(lambda x: x)) # Test call y = model(x) expected_y = tf.constant([1, 2 + 1], shape=(1, 1, 2)) assert_equal(y, expected_y) # Test inverse inverted_y = model.inverse(y) assert_equal(x, inverted_y) # Test zero log jacobian determinant z = tf.reshape(tf.range(1, 11), shape=(5, 1, 2)) model = SymplecticAdditiveCoupling(tf.keras.layers.Lambda(lambda x: x)) assert_equal(model.log_jacobian_det(z), tf.zeros([5], dtype=tf.int32)) print('testSymplecticAdditiveCoupling passed')
def testSymplecticExchange(): batch_size = 3 x = tf.ones([batch_size, 1, 2]) # Test call model = SymplecticExchange() y = model(x) expected_y = tf.concat( [tf.ones([batch_size, 1, 1]), -tf.ones([batch_size, 1, 1])], 2) assert_equal(y, expected_y) # Test inverse inverted_y = model.inverse(y) assert_equal(x, inverted_y) # Test zero log jacobian determinant z = tf.reshape(tf.range(1, 11), shape=(5, 1, 2)) assert_equal(model.log_jacobian_det(z), tf.zeros([5], dtype=tf.int32)) # Assert symplecticity model = SymplecticExchange() assert (is_symplectic( model, tf.reshape(tf.range(0, 20, dtype=DTYPE), [1, 2, 5, 2]))) print('testSymplecticExchange passed')
def testhouseholder(): batch_size = 1 d = 1 num_p = 1 n = num_p * d # q = .1, p=.2 x = tf.reshape([.1, .2], (batch_size, d, num_p, 2)) q, p = extract_q_p(x) a = tf.ones(n, dtype=DTYPE) b = tf.ones(n, dtype=DTYPE) newq, newp = householder(q, p, a, b) y = join_q_p(newq, newp) # [.1 - 1*(.1 +.1), .2-1*(.2+.2)] expected_y = tf.reshape([-.1, -.2], (batch_size, d, num_p, 2)) assert_equal(y, expected_y) ## batch_size = 2 d = 3 num_p = 2 n = num_p * d x = tf.reshape(tf.range(0, batch_size * n * 2, dtype=DTYPE), (batch_size, d, num_p, 2)) q, p = extract_q_p(x) q = tf.reshape(q, [batch_size, 1, 1, -1]) p = tf.reshape(p, [batch_size, 1, 1, -1]) # only real a = tf.ones(n, dtype=DTYPE) b = tf.zeros(n, dtype=DTYPE) q1, p1 = householder(q, p, a, b) # only imag b = tf.ones(n, dtype=DTYPE) a = tf.zeros(n, dtype=DTYPE) q2, p2 = householder(q, p, a, b) assert_equal(q1, q2) assert_equal(p1, p2) print('testhouseholder passed')
def test_equal_keys_are_equal(self): k1 = PublicKey(b"\x00" * crypto_box_PUBLICKEYBYTES) k2 = PublicKey(b"\x00" * crypto_box_PUBLICKEYBYTES) assert_equal(k1, k1) assert_equal(k1, k2)
def run_can_make_change_k_coins(): assert_equal(can_make_change_k_coins(1, [1], 1), True) assert_equal(can_make_change_k_coins(10, [5], 1), False) assert_equal(can_make_change_k_coins(8, [3, 5], 1), False) assert_equal(can_make_change_k_coins(8, [3, 5], 2), True) assert_equal(can_make_change_k_coins(55, [5, 10], 6), True) assert_equal(can_make_change_k_coins(65, [5, 10], 6), False)
def test_equal_keys_are_equal(self): k1 = SigningKey(b"\x00" * crypto_sign_SEEDBYTES) k2 = SigningKey(b"\x00" * crypto_sign_SEEDBYTES) assert_equal(k1, k1) assert_equal(k1, k2)
def _open_channel(self): self.log.info("Open channel from lit node 0 to lit node 1") assert_equal(self.litnodes[0].ChannelList()['result']['Channels'], []) assert_equal(self.litnodes[1].ChannelList()['result']['Channels'], []) self.litnodes[0].FundChannel(Peer=1, CoinType=self.coins[0]['code'], Capacity=1000000000) self.confirm_transactions(self.coinnodes[0], self.litnodes[0], 1) self.log.info("lit node 0 has funded channel") # Wait for channel to open wait_until(lambda: len(self.litnodes[0].ChannelList()['result']['Channels']) > 0) assert len(self.litnodes[1].ChannelList()['result']['Channels']) > 0 self.log.info("Channel open") # Why does this error? #assert abs(self.balance - self.litnodes[0].get_balance(self.coins[0]['code'])['TxoTotal'] - 1000000000) < self.coins[0]["feerate"] * 250 self.balance = self.litnodes[0].get_balance(self.coins[0]['code'])['TxoTotal'] self.log_balances(self.coins[0]['code']) litnode0_channel = self.litnodes[0].ChannelList()['result']['Channels'][0] litnode1_channel = self.litnodes[1].ChannelList()['result']['Channels'][0] assert_equal(litnode0_channel['Capacity'], 1000000000) assert_equal(litnode0_channel['StateNum'], 0) assert not litnode0_channel['Closed'] assert_equal(litnode0_channel['MyBalance'], 1000000000) assert_equal(litnode1_channel['Capacity'], 1000000000) assert_equal(litnode1_channel['StateNum'], 0) assert not litnode1_channel['Closed'] assert_equal(litnode1_channel['MyBalance'], 0) self.log_channel_balance(self.litnodes[0], 0, self.litnodes[1], 0)
def run_lps(): assert_equal(lps("a"), 1) assert_equal(lps("aa"), 2) assert_equal(lps("aba"), 3) assert_equal(lps("abcbaf"), 5) assert_equal(lps("racecar"), 7) assert_equal(lps("yracecarxx"), 7)
def test_equal_keys_are_equal(self): k1 = VerifyKey(b"\x00" * crypto_sign_PUBLICKEYBYTES) k2 = VerifyKey(b"\x00" * crypto_sign_PUBLICKEYBYTES) assert_equal(k1, k1) assert_equal(k1, k2)
def test_toy_hamiltonian(): x = tf.constant([2., 3.], shape=(1, 1, 1, 2)) h = toy_hamiltonian(x) expected_h = tf.constant(1 / 2 * (2 - 1 / 4 * 3**2)**2 + 1 / 2 * 3**2 / 16) assert_equal(h, expected_h) print('test_toy_hamiltonian passed')
def model_trial_maps( model, network_name: str, trial_number: int, atari_head: AtariHead, eval_seed: int, return_raw=True, return_prc=False, progress=False): env_name = atari_head.get_env_name(trial_number) _, raw_observations, prc_observations, saliency_maps = run_baselines.evaluate_model( model, network_name=network_name, env_name=env_name, num_env=1, seed=eval_seed, frame_stack_size=4, # a lower bound that always seems to work is run['NumberOfFrames'] // 4 + 2 max_eplen=None, noop_reset=False, fire_reset=False, eval_dir=None, return_raw=return_raw, return_prc=return_prc, progress=progress, ) saliency_maps = [smap[0] for smap in saliency_maps] # 1st dimension is for batch df_gazes = atari_head.read_gazes(trial_number) last_frame = atari_head.get_run(trial_number)['NumberOfFrames'] - 1 if last_frame % 4 == 0: last_frame -= 4 raw_fixation_maps, prc_fixation_maps = zip(*[ make_fixation_map([ (y, x) for frame_gazes in df_gazes['gaze_positions'][df_gazes['frame_idx'].isin(idx_for_framestack(i))] for (y, x) in frame_gazes ]) for i in range(0, last_frame + 1, 4) ]) if return_raw: if return_prc: assert_equal(len(raw_observations), len(prc_observations), len(saliency_maps), len(raw_fixation_maps)) else: assert_equal(len(raw_observations), len(saliency_maps), len(raw_fixation_maps)) elif return_prc: assert_equal(len(prc_observations), len(saliency_maps), len(raw_fixation_maps)) assert_equal(prc_observations[0].shape[1:-1], saliency_maps[0].shape, raw_fixation_maps[0].shape) else: assert_equal(len(saliency_maps), len(raw_fixation_maps)) assert_equal(saliency_maps[0].shape, raw_fixation_maps[0].shape) return raw_observations, prc_observations, saliency_maps, raw_fixation_maps, prc_fixation_maps
def test_DiracDistribution(): # num_samples = 1 sh = (3,2,1) vals = np.arange(3*2) d = DiracDistribution(sh, vals) x = d.sample(4) expected_shape = [4, 3, 2, 1] assert x.shape.as_list() == expected_shape assert_equal(x[0,...], x[1,...]) assert_equal(x[1,...], x[2,...]) assert_equal(x[2,...], x[3,...]) # num_samples = 2 sh = (3,1,1) vals = np.reshape(np.arange(2*3), (2,3)) d = DiracDistribution(sh, vals) x = d.sample(4) expected_shape = [4, 3, 1, 1] assert x.shape.as_list() == expected_shape assert_equal(x[0,...], tf.reshape(tf.constant([0,1,2],dtype=DTYPE),(3,1,1))) assert_equal(x[1,...], tf.reshape(tf.constant([3,4,5],dtype=DTYPE),(3,1,1))) assert_equal(x[0,...], x[2,...]) assert_equal(x[1,...], x[3,...]) print('test_DiracDistribution passed')
def run_lis(): assert_equal(lis([10, 9, 2, 5, 3, 7, 19, 18]), 4) assert_equal(lis([0, 1, 0, 3, 2, 3]), 4) assert_equal(lis([3, 10, 2, 1, 20]), 3) assert_equal(lis([3, 2, 1]), 1) assert_equal(lis([10, 22, 9, 33, 21, 50, 41, 60, 80]), 6) assert_equal(lis([7, 7, 7, 7, 7, 7, 7]), 1) assert_equal(lis([2, 8, 1, 0, 4, 7, 10]), 4) assert_equal(lis([50, 3, 10, 7, 40, 80]), 4)
def run_grid_frog(): assert_equal(grid_frog(3), 0) assert_equal(grid_frog(4), 2) assert_equal(grid_frog(7), 6) assert_equal(grid_frog(10), 20) assert_equal(grid_frog(11), 0) assert_equal(grid_frog(13), 70) assert_equal(grid_frog(16), 252) assert_equal(grid_frog(19), 924) assert_equal(grid_frog(31), 184756)
def run_linear_frog(): assert_equal(linear_frog(3), 1) assert_equal(linear_frog(5), 2) assert_equal(linear_frog(6), 3) assert_equal(linear_frog(7), 4)
def test_equal_keys_are_equal(self): k1 = PrivateKey(b"\x00" * crypto_box_SECRETKEYBYTES) k2 = PrivateKey(b"\x00" * crypto_box_SECRETKEYBYTES) assert_equal(k1, k1) assert_equal(k1, k2)