def main(params): opts = get_params(params) print(opts, flush=True) device = opts.device force_eos = opts.force_eos == 1 if opts.probs == 'uniform': probs = np.ones(opts.n_features) elif opts.probs == 'powerlaw': probs = 1 / np.arange(1, opts.n_features + 1, dtype=np.float32) else: probs = np.array([float(x) for x in opts.probs.split(',')], dtype=np.float32) probs /= probs.sum() print('the probs are: ', probs, flush=True) train_loader = OneHotLoader(n_features=opts.n_features, batch_size=opts.batch_size, batches_per_epoch=opts.batches_per_epoch, probs=probs) # single batches with 1s on the diag test_loader = UniformLoader(opts.n_features) ################################# # define sender (speaker) agent # ################################# sender = Sender(n_features=opts.n_features, n_hidden=opts.sender_hidden) sender = RnnSenderReinforce(sender, opts.vocab_size, opts.sender_embedding, opts.sender_hidden, cell=opts.sender_cell, max_len=opts.max_len, num_layers=opts.sender_num_layers, force_eos=force_eos, noise_loc=opts.sender_noise_loc, noise_scale=opts.sender_noise_scale) #################################### # define receiver (listener) agent # #################################### receiver = Receiver(n_features=opts.n_features, n_hidden=opts.receiver_hidden) receiver = RnnReceiverDeterministic(receiver, opts.vocab_size, opts.receiver_embedding, opts.receiver_hidden, cell=opts.receiver_cell, num_layers=opts.receiver_num_layers, noise_loc=opts.receiver_noise_loc, noise_scale=opts.receiver_noise_scale) ################### # define channel # ################### channel = Channel(vocab_size=opts.vocab_size, p=opts.channel_repl_prob) game = SenderReceiverRnnReinforce( sender, receiver, loss, sender_entropy_coeff=opts.sender_entropy_coeff, receiver_entropy_coeff=opts.receiver_entropy_coeff, length_cost=opts.length_cost, effective_max_len=opts.effective_max_len, channel=channel, sender_entropy_common_ratio=opts.sender_entropy_common_ratio) optimizer = core.build_optimizer(game.parameters()) callbacks = [ EarlyStopperAccuracy(opts.early_stopping_thr), core.ConsoleLogger(as_json=True, print_train_loss=True) ] if opts.checkpoint_dir: ''' info in checkpoint_name: - n_features as f - vocab_size as vocab - random_seed as rs - lr as lr - sender_hidden as shid - receiver_hidden as rhid - sender_entropy_coeff as sentr - length_cost as reg - max_len as max_len - sender_noise_scale as sscl - receiver_noise_scale as rscl - channel_repl_prob as crp - sender_entropy_common_ratio as scr ''' checkpoint_name = ( f'{opts.name}' + '_aer' + ('_uniform' if opts.probs == 'uniform' else '') + f'_f{opts.n_features}' + f'_vocab{opts.vocab_size}' + f'_rs{opts.random_seed}' + f'_lr{opts.lr}' + f'_shid{opts.sender_hidden}' + f'_rhid{opts.receiver_hidden}' + f'_sentr{opts.sender_entropy_coeff}' + f'_reg{opts.length_cost}' + f'_max_len{opts.max_len}' + f'_sscl{opts.sender_noise_scale}' + f'_rscl{opts.receiver_noise_scale}' + f'_crp{opts.channel_repl_prob}' + f'_scr{opts.sender_entropy_common_ratio}') callbacks.append( core.CheckpointSaver(checkpoint_path=opts.checkpoint_dir, checkpoint_freq=opts.checkpoint_freq, prefix=checkpoint_name)) trainer = core.Trainer(game=game, optimizer=optimizer, train_data=train_loader, validation_data=test_loader, callbacks=callbacks) trainer.train(n_epochs=opts.n_epochs) print('<div id="prefix test without eos">') prefix_test(trainer.game, opts.n_features, device, add_eos=False) print('</div>') print('<div id="prefix test with eos">') prefix_test(trainer.game, opts.n_features, device, add_eos=True) print('<div id="suffix test">') suffix_test(trainer.game, opts.n_features, device) print('</div>') print('<div id="replacement test">') replacement_test(trainer.game, opts.n_features, opts.vocab_size, device) print('</div>') print('<div id="dump">') dump(trainer.game, opts.n_features, device, False) print('</div>') core.close()
def c2p(): m0 = Word.Mask('00ff') m1 = Word.Mask('0f0f') print "=[ c2p 1x1 ham6 (blitter + mangled) ]=".center(48, '-') def MakeWord(c, color): # mangle: [ 0 0 0 0 r0 r1 r2 r3 g0 g1 g2 g3 b0 b1 b2 b3] => # [r0 g0 b0 b0 r1 g1 b1 b1 r2 g2 b2 b2 r3 g3 b3 b3] return Word([Bit.Var(c[0], 0, color), Bit.Var(c[1], 0, color), Bit.Var(c[2], 0, color), Bit.Var(c[3], 0, color), Bit.Var(c[0], 1, color), Bit.Var(c[1], 1, color), Bit.Var(c[2], 1, color), Bit.Var(c[3], 1, color), Bit.Var(c[0], 2, color), Bit.Var(c[1], 2, color), Bit.Var(c[2], 2, color), Bit.Var(c[3], 2, color), Bit.Var(c[0], 3, color), Bit.Var(c[1], 3, color), Bit.Var(c[2], 3, color), Bit.Var(c[3], 3, color)]) A = Array.Make(MakeWord) N = len(A) Array.Print("Data:", *A) B = Array.Zero(N, 16) Blit(lambda a, b: ((a >> 8) & m0) | (b & ~m0), N / 4, 2, Channel(A, 2, 2), Channel(A, 0, 2), Channel(B, 0, 2)) Blit(lambda a, b: ((a << 8) & ~m0) | (b & m0), N / 4, 2, Channel(A, 0, 2), Channel(A, 2, 2), Channel(B, 2, 2)) Array.Print("Swap 8x4:", *B) C = [Array.Zero(N / 4, 16) for i in range(4)] Blit(lambda a, b: ((a >> 4) & m1) | (b & ~m1), N / 4, 1, Channel(B, 1, 3), Channel(B, 0, 3), Channel(C[0])) Blit(lambda a, b: ((a >> 4) & m1) | (b & ~m1), N / 4, 1, Channel(B, 3, 3), Channel(B, 2, 3), Channel(C[2])) Blit(lambda a, b: ((a << 4) & ~m1) | (b & m1), N / 4, 1, Channel(B, 0, 3), Channel(B, 1, 3), Channel(C[1])) Blit(lambda a, b: ((a << 4) & ~m1) | (b & m1), N / 4, 1, Channel(B, 2, 3), Channel(B, 3, 3), Channel(C[3])) print("Bitplanes:") Array.Print("[0]:", *C[0]) Array.Print("[1]:", *C[1]) Array.Print("[2]:", *C[2]) Array.Print("[3]:", *C[3])
def c2p(bitplane_output=True): m0 = Word.Mask('ff00') m1 = Word.Mask('f0f0') m2 = Word.Mask('cccc') print "=[ c2p 1x1 4bpl (blitter + mangled) ]=".center(48, '-') def MakeWord(c, color): return Word([ Bit.Var(c[0], 0, color), Bit.Var(c[1], 0, color), Bit.Var(c[0], 2, color), Bit.Var(c[1], 2, color), Bit.Var(c[0], 1, color), Bit.Var(c[1], 1, color), Bit.Var(c[0], 3, color), Bit.Var(c[1], 3, color), Bit.Var(c[2], 0, color), Bit.Var(c[3], 0, color), Bit.Var(c[2], 2, color), Bit.Var(c[3], 2, color), Bit.Var(c[2], 1, color), Bit.Var(c[3], 1, color), Bit.Var(c[2], 3, color), Bit.Var(c[3], 3, color) ]) A = Array.Make(MakeWord) N = len(A) Array.Print("Data:", *A) B = Array.Zero(N, 16) Blit(lambda a, b: (a & m0) | ((b >> 8) & ~m0), N / 4, 2, Channel(A, 0, 2), Channel(A, 2, 2), Channel(B, 0, 2)) Blit(lambda a, b: ((a << 8) & m0) | (b & ~m0), N / 4, 2, Channel(A, 0, 2), Channel(A, 2, 2), Channel(B, 2, 2)) Array.Print("Swap 8x4:", *B) C = Array.Zero(N, 16) Blit(lambda a, b: (a & m1) | ((b >> 4) & ~m1), N / 2, 1, Channel(B, 0, 1), Channel(B, 1, 1), Channel(C, 0, 1)) Blit(lambda a, b: ((a << 4) & m1) | (b & ~m1), N / 2, 1, Channel(B, 0, 1), Channel(B, 1, 1), Channel(C, 1, 1)) Array.Print("Swap 4x2:", *C) if bitplane_output: D = [Array.Zero(N / 4, 16) for i in range(4)] Blit(lambda a, b: (a & m2) | ((b >> 2) & ~m2), N / 4, 1, Channel(C, 0, 3), Channel(C, 2, 3), Channel(D[0], 0, 0)) Blit(lambda a, b: (a & m2) | ((b >> 2) & ~m2), N / 4, 1, Channel(C, 1, 3), Channel(C, 3, 3), Channel(D[1], 0, 0)) Blit(lambda a, b: ((a << 2) & m2) | (b & ~m2), N / 4, 1, Channel(C, 0, 3), Channel(C, 2, 3), Channel(D[2], 0, 0)) Blit(lambda a, b: ((a << 2) & m2) | (b & ~m2), N / 4, 1, Channel(C, 1, 3), Channel(C, 3, 3), Channel(D[3], 0, 0)) print("Bitplanes:") Array.Print("[0]:", *D[0]) Array.Print("[1]:", *D[1]) Array.Print("[2]:", *D[2]) Array.Print("[3]:", *D[3]) else: D = Array.Zero(N, 16) Blit(lambda a, b: ((a >> 2) & ~m2) | (b & m2), N / 4, 2, Channel(C, 2, 2), Channel(C, 0, 2), Channel(D, 0, 2)) Blit(lambda a, b: ((a << 2) & m2) | (b & ~m2), N / 4, 2, Channel(C, 0, 2), Channel(C, 2, 2), Channel(D, 2, 2)) Array.Print("Swap 2x2:", *D)
def c2p(bitplane_output=True): m0 = Word.Mask('00ff') m1 = Word.Mask('0f0f') m2 = Word.Mask('5555') print "=[ c2p 2x1 4bpl (blitter + mangled) ]=".center(48, '-') # premangled pixels: # 1) [- - - - a b c d] => [a b - - c d - -] # 2) [- - - - e f g h] => [- - e f - - g h] def MakeWord(c, color): return Word([Bit.Var(c[0], 0, color), Bit.Var(c[0], 1, color), Bit.Var(c[1], 0, color), Bit.Var(c[1], 1, color), Bit.Var(c[0], 2, color), Bit.Var(c[0], 3, color), Bit.Var(c[1], 2, color), Bit.Var(c[1], 3, color), Bit.Var(c[2], 0, color), Bit.Var(c[2], 1, color), Bit.Var(c[3], 0, color), Bit.Var(c[3], 1, color), Bit.Var(c[2], 2, color), Bit.Var(c[2], 3, color), Bit.Var(c[3], 2, color), Bit.Var(c[3], 3, color)]) A = Array.Make(MakeWord) N = len(A) Array.Print("Data:", *A) B = Array.Zero(N, 16) Blit(lambda a, b: ((a >> 8) & m0) | (b & ~m0), N / 2, 1, Channel(A, 1, 1), Channel(A, 0, 1), Channel(B, 0, 1)) Blit(lambda a, b: ((a << 8) & ~m0) | (b & m0), N / 2, 1, Channel(A, 0, 1), Channel(A, 1, 1), Channel(B, 1, 1)) Array.Print("Swap 8x2:", *B) C = Array.Zero(N, 16) Blit(lambda a, b: ((a & ~m1) | ((b >> 4) & m1)), N / 2, 1, Channel(B, 0, 1), Channel(B, 1, 1), Channel(C, 0, 1)) Blit(lambda a, b: (((a << 4) & ~m1) | (b & m1)), N / 2, 1, Channel(B, 0, 1), Channel(B, 1, 1), Channel(C, 1, 1)) Array.Print("Swap 4x2:", *C) if bitplane_output: D = [Array.Zero(N / 2, 16) for i in range(4)] Blit(lambda a, b: (((a >> 1) & m2) | (b & ~m2)), N / 2, 1, Channel(C, 0, 1), Channel(C, 0, 1), Channel(D[0], 0, 0)) Blit(lambda a, b: (((a >> 1) & m2) | (b & ~m2)), N / 2, 1, Channel(C, 1, 1), Channel(C, 1, 1), Channel(D[2], 0, 0)) Blit(lambda a, b: (((a << 1) & ~m2) | (b & m2)), N / 2, 1, Channel(C, 0, 1), Channel(C, 0, 1), Channel(D[1], 0, 0)) Blit(lambda a, b: (((a << 1) & ~m2) | (b & m2)), N / 2, 1, Channel(C, 1, 1), Channel(C, 1, 1), Channel(D[3], 0, 0)) print("Bitplanes:") Array.Print("[0]:", *D[0]) Array.Print("[1]:", *D[1]) Array.Print("[2]:", *D[2]) Array.Print("[3]:", *D[3]) else: D = Array.Zero(N * 2, 16) Blit(lambda a, b: (((a >> 1) & m2) | (b & ~m2)), N, 1, Channel(C, 0, 0), Channel(C, 0, 0), Channel(D, 0, 1)) Blit(lambda a, b: (((a << 1) & ~m2) | (b & m2)), N, 1, Channel(C, 0, 0), Channel(C, 0, 0), Channel(D, 1, 1)) Array.Print("Expand 2x1:", *D)
def c2p(bitplane_output=True): print "=[ c2p 2x1 4bpl (blitter + mangled) ]=".center(48, '-') # premangled pixels: # 1) [- - - - a b c d] => [a b - - c d - -] # 2) [- - - - e f g h] => [- - e f - - g h] def MakeWord(c, color): return Word([ Bit.Var(c[0], 0, color), Bit.Var(c[0], 1, color), Bit.Var(c[1], 0, color), Bit.Var(c[1], 1, color), Bit.Var(c[0], 2, color), Bit.Var(c[0], 3, color), Bit.Var(c[1], 2, color), Bit.Var(c[1], 3, color), Bit.Var(c[2], 0, color), Bit.Var(c[2], 1, color), Bit.Var(c[3], 0, color), Bit.Var(c[3], 1, color), Bit.Var(c[2], 2, color), Bit.Var(c[2], 3, color), Bit.Var(c[3], 2, color), Bit.Var(c[3], 3, color) ]) # premangled pixel buffer: [AB CD EF GH] => [AB EF CD GH] def ArrayMake(fn): return [ fn("abef", color="31m"), fn("cdgh", color="32m"), fn("ijmn", color="33m"), fn("klop", color="34m"), fn("qruv", color="35m"), fn("stwx", color="36m"), fn("ABEF", color="31;1m"), fn("CDGH", color="32;1m"), fn("IJMN", color="33;1m"), fn("KLOP", color="34;1m"), fn("QRUV", color="35;1m"), fn("STWX", color="36;1m") ] A = ArrayMake(MakeWord) N = len(A) Array.Print("Data:", *A) m0 = Word.Mask('f0f0') m1 = Word.Mask('aaaa') B = [Array.Zero(N / 2, 16) for i in range(2)] Blit(lambda a, b: (a & m0) | ((b >> 4) & ~m0), N / 2, 1, Channel(A, 0, 1), Channel(A, 1, 1), Channel(B[0])) Blit(lambda a, b: ((a << 4) & m0) | (b & ~m0), N / 2, 1, Channel(A, 0, 1), Channel(A, 1, 1), Channel(B[1])) print("Swap 4x2:") Array.Print("[0]:", *B[0]) Array.Print("[1]:", *B[1]) C = [Array.Zero(N / 2, 16) for i in range(4)] Blit(lambda a, b: (a & m1) | ((b >> 1) & ~m1), N / 2, 1, Channel(B[0]), Channel(B[0]), Channel(C[0])) Blit(lambda a, b: (a & m1) | ((b >> 1) & ~m1), N / 2, 1, Channel(B[1]), Channel(B[1]), Channel(C[2])) Blit(lambda a, b: ((a << 1) & m1) | (b & ~m1), N / 2, 1, Channel(B[0]), Channel(B[0]), Channel(C[1])) Blit(lambda a, b: ((a << 1) & m1) | (b & ~m1), N / 2, 1, Channel(B[1]), Channel(B[1]), Channel(C[3])) print("Expand 2x1:") Array.Print("[0]:", *C[0]) Array.Print("[1]:", *C[1]) Array.Print("[2]:", *C[2]) Array.Print("[3]:", *C[3])
def c2p(bitplane_output=True): m0 = Word.Mask('00ff') m1 = Word.Mask('0f0f') m2 = Word.Mask('3333') m3 = Word.Mask('5555') print "=[ c2p 1x1 4bpl (blitter) ]=".center(48, '-') def MakeWord(chars, color): bits = [] for c in chars: for i in range(4): bits.append(Bit.Var(c, i, color)) return Word(bits) A = Array.Make(MakeWord) N = len(A) Array.Print("Data:", *A) B = Array.Zero(N, 16) Blit(lambda a, b: ((a >> 8) & m0) | (b & ~m0), N / 4, 2, Channel(A, 2, 2), Channel(A, 0, 2), Channel(B, 0, 2)) Blit(lambda a, b: ((a << 8) & ~m0) | (b & m0), N / 4, 2, Channel(A, 0, 2), Channel(A, 2, 2), Channel(B, 2, 2)) Array.Print("Swap 8x4:", *B) C = Array.Zero(N, 16) Blit(lambda a, b: ((a >> 4) & m1) | (b & ~m1), N / 2, 1, Channel(B, 1, 1), Channel(B, 0, 1), Channel(C, 0, 1)) Blit(lambda a, b: ((a << 4) & ~m1) | (b & m1), N / 2, 1, Channel(B, 0, 1), Channel(B, 1, 1), Channel(C, 1, 1)) Array.Print("Swap 4x2:", *C) D = Array.Zero(N, 16) Blit(lambda a, b: ((a >> 2) & m2) | (b & ~m2), N / 4, 2, Channel(C, 2, 2), Channel(C, 0, 2), Channel(D, 0, 2)) Blit(lambda a, b: ((a << 2) & ~m2) | (b & m2), N / 4, 2, Channel(C, 0, 2), Channel(C, 2, 2), Channel(D, 2, 2)) Array.Print("Swap 2x2:", *D) if bitplane_output: E = [Array.Zero(N / 4, 16) for i in range(4)] Blit(lambda a, b: ((a >> 1) & m3) | (b & ~m3), N / 4, 1, Channel(D, 1, 3), Channel(D, 0, 3), Channel(E[0], 0, 0)) Blit(lambda a, b: ((a >> 1) & m3) | (b & ~m3), N / 4, 1, Channel(D, 3, 3), Channel(D, 2, 3), Channel(E[2], 0, 0)) Blit(lambda a, b: ((a << 1) & ~m3) | (b & m3), N / 4, 1, Channel(D, 0, 3), Channel(D, 1, 3), Channel(E[1], 0, 0)) Blit(lambda a, b: ((a << 1) & ~m3) | (b & m3), N / 4, 1, Channel(D, 2, 3), Channel(D, 3, 3), Channel(E[3], 0, 0)) print("Bitplanes:") Array.Print("[0]:", *E[0]) Array.Print("[1]:", *E[1]) Array.Print("[2]:", *E[2]) Array.Print("[3]:", *E[3]) else: E = Array.Zero(N, 16) Blit(lambda a, b: ((a >> 1) & m3) | (b & ~m3), N / 2, 1, Channel(D, 1, 1), Channel(D, 0, 1), Channel(E, 0, 1)) Blit(lambda a, b: ((a << 1) & ~m3) | (b & m3), N / 2, 1, Channel(D, 0, 1), Channel(D, 1, 1), Channel(E, 1, 1)) Array.Print("Swap 1x1:", *E)