def prepare(data_valid, sae): gen_batch = 10000 if len(data_valid) < 2000 else None data_mixed = generate_random(data_valid, sae, gen_batch) try: p = 0 pp = 0 ppp = 0 while len(data_mixed) < len(data_valid) and p < len(data_mixed): p = pp pp = ppp ppp = len(data_mixed) data_mixed = union(data_mixed, generate_random(data_valid, sae, gen_batch)) print( "valid:", len(data_valid), "mixed:", len(data_mixed), "## generation stops when it failed to generate new examples three times in a row" ) except KeyboardInterrupt: pass finally: print("generation stopped") if len(data_valid) < len(data_mixed): # downsample data_mixed = data_mixed[:len(data_valid)] else: # oversample data_mixed = np.repeat(data_mixed, 1 + (len(data_valid) // len(data_mixed)), axis=0) data_mixed = data_mixed[:len(data_valid)] train_in, train_out, test_in, test_out = prepare_binary_classification_data( data_valid, data_mixed) return train_in, train_out, test_in, test_out, data_valid, data_mixed
def prepare_oae_PU(known_transisitons): print( "discriminate correct transitions and other transitions generated by OAE" ) y = generate_oae_action(known_transisitons) # normalize y = y[:len(known_transisitons)] return (default_networks['PUDiscriminator'], *prepare_binary_classification_data(known_transisitons, y))
def prepare(data_valid): print(data_valid.shape) batch = data_valid.shape[0] N = data_valid.shape[1] data_invalid = np.random.randint(0,2,(batch,N),dtype=np.int8) print(data_valid.shape,data_invalid.shape) ai = data_invalid.view([('', data_invalid.dtype)] * N) av = data_valid.view ([('', data_valid.dtype)] * N) data_invalid = np.setdiff1d(ai, av).view(data_valid.dtype).reshape((-1, N)) return prepare_binary_classification_data(data_valid, data_invalid)
def prepare_oae_per_action_PU3(known_transisitons): print("", sep="\n") N = known_transisitons.shape[1] // 2 states = known_transisitons.reshape(-1, N) oae = default_networks['ActionAE'](ae.local("_aae/")).load() actions = oae.encode_action(known_transisitons, batch_size=1000).round().astype(int) L = actions.shape[2] assert L > 1 histogram = np.squeeze(actions.sum(axis=0)) print(histogram) sd3 = default_networks['PUDiscriminator'](ae.local("_sd3/")).load() try: cae = default_networks['SimpleCAE'](sae.local("_cae/")).load() combined_discriminator = default_networks['CombinedDiscriminator'](ae, cae, sd3) except: combined_discriminator = default_networks['CombinedDiscriminator2']( ae, sd3) for label in range(L): print("label", label) known_transisitons_for_this_label = known_transisitons[np.where( actions[:, :, label] > 0.5)[0]] if len(known_transisitons_for_this_label) == 0: yield None else: _actions = np.zeros( (len(states), actions.shape[1], actions.shape[2]), dtype=int) _actions[:, :, label] = 1 y = oae.decode([states, _actions], batch_size=1000).round().astype(np.int8) # prune invalid states ind = np.where( np.squeeze(combined_discriminator(y[:, N:], batch_size=1000)) > 0.5)[0] y = y[ind] y = set_difference(y, known_transisitons_for_this_label) print(y.shape, known_transisitons_for_this_label.shape) train_in, train_out, test_in, test_out = prepare_binary_classification_data( known_transisitons_for_this_label, y) yield (train_in, train_out, test_in, test_out)
def prepare_oae_validated(known_transisitons): print( "generate many actions from states using OEA (at least one action for each state is correct)", "validate it with validators, then discriminate the correct vs wrong transitions.", sep="\n") print("**** CHEATING ****") N = known_transisitons.shape[1] // 2 y = generate_oae_action(known_transisitons) p = latplan.util.puzzle_module(sae.path) batch = 100000 valid_suc = np.zeros(len(y), dtype=bool) for i in range(1 + len(y) // batch): print(i, "/", len(y) // batch) suc_images = sae.decode_binary(y[batch * i:batch * (i + 1), N:], batch_size=1000) valid_suc[batch * i:batch * (i + 1)] = p.validate_states( suc_images, verbose=False, batch_size=1000) # This state validation is just for reducing the later effort for validating transitions before_len = len(y) y = y[valid_suc] print("removing invalid successor states:", before_len, "->", len(y)) answers = np.zeros(len(y), dtype=int) for i in range(1 + len(y) // batch): print(i, "/", len(y) // batch) pre_images = sae.decode_binary(y[batch * i:batch * (i + 1), :N], batch_size=1000) suc_images = sae.decode_binary(y[batch * i:batch * (i + 1), N:], batch_size=1000) answers[batch * i:batch * (i + 1)] = np.array( p.validate_transitions([pre_images, suc_images], batch_size=1000)).astype(int) l = len(y) positive = np.count_nonzero(answers) print(positive, l - positive) y_positive = y[answers.astype(bool)] y_negative = y[(1 - answers).astype(bool)] y_negative = y_negative[:len(y_positive)] return (default_networks['Discriminator'], *prepare_binary_classification_data(y_positive, y_negative))
def prepare(data): print( "discriminate correct transitions and nop, suc-permutation, reconstructable, and random bits combined" ) print( "**** does not discriminate OEA-generated states quite well, do not use ****" ) data_invalid = np.concatenate(tuple([ generate_nop(data), *[permute_suc(data) for i in range(inflation)], *[generate_random_action(data, sae) for i in range(inflation)], *[generate_random_action2(data) for i in range(inflation)] ]), axis=0) data_valid = np.repeat(data, len(data_invalid) // len(data), axis=0) return (default_networks['PUDiscriminator'], *prepare_binary_classification_data(data_valid, data_invalid))
def prepare_oae_PU5(known_transisitons): print("Learn from pre + suc + action label", "*** INCOMPATIBLE MODEL! ***", sep="\n") N = known_transisitons.shape[1] // 2 y = generate_oae_action(known_transisitons) ind = np.where(np.squeeze(combined(y[:, N:])) > 0.5)[0] y = y[ind] actions = oae.encode_action(known_transisitons, batch_size=1000).round() positive = np.concatenate((known_transisitons, np.squeeze(actions)), axis=1) actions = oae.encode_action(y, batch_size=1000).round() negative = np.concatenate((y, np.squeeze(actions)), axis=1) # random.shuffle(negative) # negative = negative[:len(positive)] # normalize return (default_networks['PUDiscriminator'], *prepare_binary_classification_data(positive, negative))
def learn(path): network = latplan.model.get('PUDiscriminator') true_actions = np.loadtxt(sae.local("actions.csv"), dtype=np.int8) fake_actions = np.loadtxt(aae.local("fake_actions.csv"), dtype=np.int8) train_in, train_out, val_in, val_out = prepare_binary_classification_data( true_actions, fake_actions) discriminator, _, _ = grid_search( curry( nn_task, network, path, train_in, train_out, val_in, val_out, ), default_parameters, parameters, path, ) discriminator.save() return discriminator
def prepare_oae_PU3(known_transisitons): print( "discriminate the correct transitions and the other transitions generated by OAE,", " filtered by the learned state discriminator", sep="\n") N = known_transisitons.shape[1] // 2 y = generate_oae_action(known_transisitons) print("removing invalid successors (sd3)") ind = np.where(np.squeeze(combined(y[:, N:])) > 0.5)[0] y = y[ind] if len(known_transisitons) > 100: y = y[:len(known_transisitons)] # undersample print( "valid:", len(known_transisitons), "mixed:", len(y), ) print("creating binary classification labels") return (default_networks['PUDiscriminator'], *prepare_binary_classification_data(known_transisitons, y))
def prepare_oae_PU2(known_transisitons): print( "discriminate the correct transitions and the other transitions generated by OAE, filtered by the state validator" ) print("**** CHEATING ****") N = known_transisitons.shape[1] // 2 y = generate_oae_action(known_transisitons) p = latplan.util.puzzle_module(sae.path) batch = 100000 valid_suc = np.zeros(len(y), dtype=bool) for i in range(1 + len(y) // batch): print(i, "/", len(y) // batch) suc_images = sae.decode_binary(y[batch * i:batch * (i + 1), N:], batch_size=1000) valid_suc[batch * i:batch * (i + 1)] = p.validate_states( suc_images, verbose=False, batch_size=1000) before_len = len(y) y = y[valid_suc] print("removing invalid successor states:", before_len, "->", len(y)) y = y[:len(known_transisitons)] # normalize return (default_networks['PUDiscriminator'], *prepare_binary_classification_data(known_transisitons, y))
def prepare_random(data_valid, sae, inflation=1): batch = data_valid.shape[0] data_mixed = random.randint(0,2,data_valid.shape,dtype=np.int8) train_in, train_out, test_in, test_out = prepare_binary_classification_data(data_valid, data_mixed) return train_in, train_out, test_in, test_out, data_valid, data_mixed