def __init__(self, g_config = [512, 'leaky_relu', 1024, 'leaky_relu', 1024, 'leaky_relu', 256], d_config = [128, 0.25, 'leaky_relu', 128, 0.25, 'leaky_relu', 1, 'sigmoid'], batch_sz = 2048, n_epochs = 500): super(ConditionalGAN, self).__init__() self.G = nets.FullyConnectedUnits(g_config) self.D = nets.FullyConnectedUnits(d_config) self.batch_sz = batch_sz self.ds = None self.n_epochs = n_epochs
def test_single_task_training(file_path): # load dataset df = pd.read_csv(file_path) y_tr = df.values[:, 1] x_tr = df.values[:, -1] lang_obj = lang.Lang(x_tr) vocab_size = len(lang_obj.idx2ch) x_tensor = lang.preprocessing(x_tr, lang_obj) # define models enc_f = nets.Encoder(vocab_size=vocab_size, reverse=False) enc_b = nets.Encoder(vocab_size=vocab_size, reverse=True) attention = nets.BidirectionalAttention(128) fcuk = nets.FullyConnectedUnits([128, 'tanh', 0.25, 32, 'tanh', 0.10, 1]) # define the flow function def flow(xs, models): enc_f, enc_b, attention, fcuk = models eo_f, h_f = enc_f(xs) eo_b, h_b = enc_b(xs) attention_weights = attention(eo_f, eo_b, h_f, h_b) ys = fcuk(attention_weights) return ys box = nets.Box(flow=flow, models=[enc_f, enc_b, attention, fcuk], n_epochs=1, batch_sz=32) box.train(x_tensor, y_tr) box.save_weights('box')
# save the dataset for later use np.save('y_tr', y_tr) np.save('x_tr', x_tr) np.save('y_te', y_te) np.save('x_te', x_te) # create the language object and map it to strings lang_obj = lang.Lang(list(x_tr) + list(x_te)) vocab_size = len(lang_obj.idx2ch) + 1 x_tr = lang.preprocessing(x_tr, lang_obj) # define models enc_f = nets.Encoder(vocab_size=vocab_size, batch_sz=BATCH_SZ, reverse=False) enc_b = nets.Encoder(vocab_size=vocab_size, batch_sz=BATCH_SZ, reverse=True) attention = nets.BidirectionalWideAttention(128) fcuk = nets.FullyConnectedUnits( [512, 'tanh', 0.30, 512, 'tanh', 0.30, 512, 'tanh', 0.25]) fcuk_props = nets.FullyConnectedUnits([9]) decoder = nets.AttentionDecoder(vocab_size=vocab_size) # convert to tensor x_tr = tf.convert_to_tensor(x_tr) y_tr = tf.convert_to_tensor(y_tr) # make them into a dataset object ds = tf.data.Dataset.from_tensor_slices((x_tr, y_tr)).shuffle(y_tr.shape[0]) ds = ds.apply(tf.contrib.data.batch_and_drop_remainder(BATCH_SZ)) # get your favorite optimizer optimizer = tf.train.AdamOptimizer()
# define models enc_f = nets.GRUEncoder(vocab_size=vocab_size, batch_sz=BATCH_SZ, reverse=False, enc_units=128) enc_b = nets.GRUEncoder(vocab_size=vocab_size, batch_sz=BATCH_SZ, reverse=True, enc_units=128) conv_encoder = nets.ConvEncoder( conv_units=[256, 512, 512], # pool_sizes=[8, 8, 8, 8], conv_kernel_sizes=[8, 12, 16], fcs=[128, 0.2, 'elu', 512, 0.2, 'elu', 512]) fcuk = nets.FullyConnectedUnits([512, 'leaky_relu', 0.25, 512]) d_mean = nets.FullyConnectedUnits([32]) d_log_var = nets.FullyConnectedUnits([32]) fcuk_props = nets.FullyConnectedUnits([9]) fcuk_fp = nets.FullyConnectedUnits([167, 'sigmoid']) decoder = nets.OneHotDecoder(vocab_size=vocab_size, dec_units=256) bypass_v_f = nets.FullyConnectedUnits([1]) simple_decoder = nets.SimpleDecoder(vocab_size=vocab_size, dec_units=1024, batch_sz=BATCH_SZ) # initialize xs = tf.zeros([BATCH_SZ, 64], dtype=tf.int64) eo_f, h_f = enc_f(xs) eo_b, h_b = enc_b(xs)
x_tr = np.load('x_tr.npy') x_te = np.load('x_te.npy') y_tr = np.load('y_tr.npy') y_te = np.load('y_te.npy') # create the language object and map it to strings lang_obj = lang.Lang(list(x_tr) + list(x_te)) vocab_size = len(lang_obj.idx2ch) + 1 x_tr = lang.preprocessing(x_tr, lang_obj) # define models enc_f = nets.Encoder(vocab_size=vocab_size, batch_sz = BATCH_SZ, reverse=False) enc_b = nets.Encoder(vocab_size=vocab_size, batch_sz = BATCH_SZ, reverse=True) attention = nets.BidirectionalWideAttention(128) fcuk = nets.FullyConnectedUnits([128, 'tanh', 0.30, 1024, 'tanh', 0.30, 1024, 'tanh', 0.25, 256, 'tanh', 0.10]) fcuk_props = nets.FullyConnectedUnits([9]) decoder = nets.AttentionDecoder(vocab_size=vocab_size) # convert to tensor x_tr = tf.convert_to_tensor(x_tr) y_tr = tf.convert_to_tensor(y_tr) # initialize xs = tf.zeros((BATCH_SZ, 64)) eo_f, h_f = enc_f(xs) eo_b, h_b = enc_b(xs) attention_weights = attention(eo_f, eo_b, h_f, h_b) attention_weights = fcuk(attention_weights) ys_hat = fcuk_props(attention_weights) dec_input = tf.expand_dims([lang_obj.ch2idx['G']] * BATCH_SZ, 1)