def main(): args = parse_arg() if args.book == 'wonderland': from config import DefaultConfig as Config elif args.book == 'copperfield': from config import Copperfield as Config else: raise NotImplementedError config = Config() data = load(config) X, y, chars, dataX, dataY = preprocessing(data, config) model, callbacks = build_model(X, y, config) train(model, X, y, callbacks, config) generate(model, dataX, chars, config)
def get (self): # the random funtion generates random 15 digits using datetime now = datetime.datetime.now() # converting time to strinh and striping :,. and - from my expected output now1 = str(now).replace(":","").replace(" ","").replace("-","").replace(".","") pin = now1[:15] # pin = ''.join([random.choice(string.digits) for n in range (15)]) # creating a variable data and saving the value of the randomly generated pin and serial number data = generate(pin) # adding the pin and serial_number to the database db.session.add(data) # commiting the new added items db.session.commit() # querying a column by the pin and saving it to the variable result result = generate.query.filter_by(pin = pin).first() # fetching the id of the particular pin and saving in s_N i_d = result.id s_n = '{:012}'.format(i_d) return{'pin': pin, "SN":s_n }
def test_cell(self): test_cases = [ { "name": "test live cell with more than three neighbours", "input": {"status": '1', "cell_neighbours": 4}, "expected": '0' }, { "name": "test_live_cell_with_three_neighbours", "input": {"status": '1', "cell_neighbours": 3}, "expected": '1' } ] for test_case in test_cases: c = test_case["input"] model.generate(c) self.assertEqual(c["status"], test_case["expected"], test_case["name"])
def start(csvpath = "csv_queries/", app_path="my_story/"): print "Start" if not os.path.exists(csvpath): print "Either the csv path or the output path don't exist" else: file_list = [f for f in os.listdir(csvpath) if path.splitext(f)[1] == '.csv'] print file_list for csvfile in file_list: category = path.splitext(csvfile)[0] print "Reading question bank for {0}".format(category) src_path = path.join(csvpath, csvfile) #Check if the django model path exists if not path.exists(app_path): os.mkdir(app_path) # Check if the category path exists cat_path = "/".join([app_path, category]) if not path.exists(cat_path): os.mkdir(cat_path) open(path.join(cat_path, "__init__.py"),'w').close() # Get to work with open(src_path, 'r') as f_obj: csvreader = csv.reader(f_obj, quoting=csv.QUOTE_NONNUMERIC, delimiter=",", quotechar='"') model.generate(category, csvreader, cat_path) view.generate(category, csvreader, cat_path)
def main() -> None: parser = cli_parser() args = parser.parse_args() output_path: Path = args.output if output_path is None: output_path = Path('./model.graphml') log.info(f'Using default output path {output_path.absolute()}') else: if output_path.exists(): log.warning(f'Will overwrite file {output_path.absolute()}') if output_path.is_dir(): raise RuntimeError(f'Specified output path {output_path.absolute()} is a directory.') network = model.generate(vars(args)) if output_path.is_file(): log.warning(f'Overwriting file {output_path}') with open(output_path.absolute(), 'w') as f: ig.write(network, f, format='graphml')
def get(self): # the random funtion generates random 15 digits pin = ''.join([random.choice(string.digits) for n in range (15)]) serial_number = ''.join([random.choice(string.digits) for n in range(12)]) # creating a variable data and saving the value of the randomly generated pin data = generate(serial_number,pin) # adding the pin to the database db.session.add(data) # commiting the new added item db.session.commit() # querying a column by the pin and saving it to the variable result result = generate.query.filter_by(pin = pin).first() # fetching the id of the particular pin and saving in s_N s_n = result.serial_number return {'pin': pin, "SN":s_n }
def get(self): # the uuid funtion generates random 15 digits numbers a = str(uuid.uuid4().int) a.split() # specifying the length of numbers from uuid generated pin = a[:15] # creating a variable data and saving the value of the randomly generated pin data = generate(pin) # adding the pin to the database db.session.add(data) # commiting the new added item db.session.commit() # querying a column by the pin and saving it to the variable result result = generate.query.filter_by(pin=pin).first() # fetching the id of the particular pin and saving in s_N s_n = result.id return {'pin': pin, "SN": s_n}
import numpy as np import model from model import SET_HYPERPARAMETER SET_HYPERPARAMETER("diffLatentSpace", 6) data = np.load("./npz/diffs.npz")["arr_1"] model = model.emptyModel("DIFF_17jan_ls6_g", inputsShape=list(data.shape[1:]), use="diff", log=False) model.restore("DIFF_17jan_ls6_f") code = [[0, 0, 0, 0, 0, 0]] result = model.generate(code, data[0:1]) print(result) print(result.shape)
# ('smile', 'laughter', 'cry') # ) col1, col2 = st.beta_columns(2) if img: col1.write('### Source image:') image = Image.open(img) col1.image(image ) # image: numpy array # input_vedio = open('./input/04.mp4' , 'rb') # video_bytes = input_vedio.read() # col2.write("### Source video") # col2.video(video_bytes ) clicked = st.button('Stylize') if clicked: # image = imageio.imread(image) with st.spinner('Processing...'): output = generate(asarray(image)) video_file = open('result.mp4', 'rb') video_bytes = video_file.read() st.success("Done!") col2.write('### Output Video') col2.video(video_bytes) # model = style.load_model(model) # style.stylize(model, input_image, output_image) # st.write('### Output image:') #image = Image.open(output_image) #st.image(image, width=400)
def main(): #load configuration conf, _ = get_config() pp.pprint(conf) if conf.is_gray : n_channel=1 else: n_channel=3 n_grid_row = int(np.sqrt(conf.n_batch)) ##========================= DEFINE MODEL ===========================## z = tf.random_uniform( (conf.n_batch, conf.n_z), minval=-1.0, maxval=1.0) x_net = tf.placeholder(tf.float32, [conf.n_batch, conf.n_img_pix, conf.n_img_pix, n_channel], name='real_images') k_t = tf.Variable(0., trainable=False, name='k_t') # define generator g_net, g_vars = generate(z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=True, reuse=False) # define discriminator e_g_net, enc_vars,_ = encode(g_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, is_train=True, reuse=False) d_g_net, dec_vars = decode(e_g_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=True, reuse=False) e_x_net, _,_ = encode(x_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, is_train=True, reuse=True) d_x_net, _ = decode(e_x_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=True, reuse=True) # image de-normalization g_img=tf.clip_by_value((g_net + 1)*127.5, 0, 255) d_g_img=tf.clip_by_value((d_g_net + 1)*127.5, 0, 255) d_x_img=tf.clip_by_value((d_x_net + 1)*127.5, 0, 255) d_vars = enc_vars + dec_vars # define discriminator and generator losses d_loss_g = tf.reduce_mean(tf.abs(d_g_net - g_net)) d_loss_x = tf.reduce_mean(tf.abs(d_x_net - x_net)) d_loss= d_loss_x - k_t * d_loss_g g_loss = tf.reduce_mean(tf.abs(d_g_net - g_net)) # define optimizer d_optim = tf.train.AdamOptimizer(conf.d_lr).minimize(d_loss, var_list=d_vars) g_optim = tf.train.AdamOptimizer(conf.g_lr).minimize(g_loss, var_list=g_vars) balance = conf.gamma * d_loss_x - g_loss measure = d_loss_x + tf.abs(balance) with tf.control_dependencies([d_optim, g_optim]): k_update = tf.assign(k_t, tf.clip_by_value(k_t + conf.lambda_k * balance, 0, 1)) # define summary for tensorboard summary_op = tf.summary.merge([ tf.summary.image("G", g_img), tf.summary.image("AE_G", d_g_img), tf.summary.image("AE_x", d_x_img), tf.summary.scalar("loss/dloss", d_loss), tf.summary.scalar("loss/d_loss_real", d_loss_x), tf.summary.scalar("loss/d_loss_fake", d_loss_g), tf.summary.scalar("loss/gloss", g_loss), tf.summary.scalar("misc/m", measure), tf.summary.scalar("misc/kt", k_t), tf.summary.scalar("misc/balance", balance), ]) # start session sess = tf.InteractiveSession()#config=tf.ConfigProto(log_device_placement=True)) init = tf.global_variables_initializer() sess.run(init) # init directories checkpoint_dir = os.path.join(conf.log_dir,conf.curr_time) if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) # init summary writer for tensorboard summary_writer = tf.summary.FileWriter(checkpoint_dir,sess.graph) saver = tf.train.Saver() if(conf.is_reload): saver.restore(sess, os.path.join(conf.load_dir, conf.ckpt_nm)) # load real image info and shuffle them data_files = glob(os.path.join(conf.data_dir,conf.dataset, "*")) shuffle(data_files) # save real fixed image x_fix = data_files[0:conf.n_batch] x_fix=[get_image(f, conf.n_img_pix, is_crop=conf.is_crop, resize_w=conf.n_img_out_pix, is_grayscale = conf.is_gray) for f in x_fix] x_fix = np.array(x_fix).astype(np.float32) x_fix = x_fix.reshape(x_fix.shape[0],x_fix.shape[1], x_fix.shape[2],n_channel ) save_images(x_fix, [n_grid_row,n_grid_row],'{}/x_fix.png'.format(checkpoint_dir)) cost_file = open(checkpoint_dir+ "/cost.txt", 'w', conf.n_buffer) n_step=0 for epoch in range(conf.n_epoch): ## shuffle data shuffle(data_files) ## load image data n_iters = int(len(data_files)/conf.n_batch) for idx in range(0, n_iters): # make image batch f_batch = data_files[idx*conf.n_batch:(idx+1)*conf.n_batch] data_batch = [get_image(f, conf.n_img_pix, is_crop=conf.is_crop, resize_w=conf.n_img_out_pix, is_grayscale = conf.is_gray) for f in f_batch] img_batch = np.array(data_batch).astype(np.float32) if conf.is_gray : s,h,w = img_batch.shape img_batch = img_batch.reshape(s, h, w, n_channel ) fetch_dict = { "kupdate": k_update, "m": measure, } if n_step % conf.n_save_log_step == 0: fetch_dict.update({ "summary": summary_op, "gloss": g_loss, "dloss": d_loss, "kt": k_t, }) start_time = time.time() # run the session! result = sess.run(fetch_dict, feed_dict={x_net:img_batch}) # get the result m = result['m'] if n_step % conf.n_save_log_step == 0: summary_writer.add_summary(result['summary'], n_step) summary_writer.flush() # write cost to a file gloss = result['gloss'] dloss = result['dloss'] kt = result['kt'] cost_file.write("Epoch: ["+str(epoch)+"/"+str(conf.n_epoch)+"] ["+str(idx)+"/"+str(n_iters)+"] time: "+str(time.time() - start_time)+", d_loss: "+str(dloss)+", g_loss:"+ str(gloss)+" measure: "+str(m)+", k_t: "+ str(kt)+ "\n") # save generated image file if n_step % conf.n_save_img_step == 0: g_sample, g_ae, x_ae = sess.run([g_img, d_g_img,d_x_img] ,feed_dict={x_net: x_fix}) save_image(g_sample,os.path.join(checkpoint_dir, '{}_G.png'.format(n_step))) save_image(g_ae, os.path.join(checkpoint_dir, '{}_AE_G.png'.format(n_step))) save_image(x_ae, os.path.join(checkpoint_dir, '{}_AE_X.png'.format(n_step))) n_step+=1 # save checkpoint saver.save(sess, os.path.join(checkpoint_dir,str(epoch)+"_"+str(n_step)+"_began2_model.ckpt") ) # save final checkpoint saver.save(sess, os.path.join(checkpoint_dir,"final_began2_model.ckpt")) cost_file.close() sess.close()
print('temperature = ', temperature) print('output_path = ', output_path) state = torch.load(model_path) model = PerformanceRNN(**state['model_config']).to(device) model.load_state_dict(state['model_state']) model.eval() print(model) print('-' * 50) init = torch.randn(batch_size, model.init_dim).to(device) with torch.no_grad(): outputs = model.generate(init, max_len, controls=controls, temperature=temperature, verbose=True) outputs = outputs.cpu().numpy().T # [batch, steps] print(outputs) os.makedirs(output_path, exist_ok=True) for i, output in enumerate(outputs): name = f'output-{i:03d}.midi' path = os.path.join(output_path, name) n_notes = utils.event_indeces_to_midi_file(output, path) print(f'===> {path} ({n_notes} notes)')
def main(): #load configuration conf, _ = get_config() pp.pprint(conf) if conf.is_gray : n_channel=1 else: n_channel=3 n_grid_row = int(np.sqrt(conf.n_batch)) z = tf.random_uniform((conf.n_batch, conf.n_z), minval=-1.0, maxval=1.0) # execute generator g_net,_ = generate(z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=False, reuse=False) # execute discriminator e_net,_, _ = encode(g_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, is_train=False, reuse=False) d_net,_ = decode(e_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel,is_train=False, reuse=False) g_img=tf.clip_by_value((g_net + 1)*127.5, 0, 255) d_img=tf.clip_by_value((d_net + 1)*127.5, 0, 255) # start session sess = tf.InteractiveSession() init = tf.global_variables_initializer() sess.run(init) # init directories checkpoint_dir = os.path.join(conf.log_dir,conf.curr_time) if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) #saver = tf.train.import_meta_graph(npz_path+'began2_model.ckpt.meta') #saver.restore(sess, tf.train.latest_checkpoint(npz_path)) saver = tf.train.Saver() saver.restore(sess, os.path.join(conf.load_dir, conf.ckpt_nm)) #load real image data_files = glob(os.path.join(conf.data_dir,conf.dataset, "*")) shuffle(data_files) x_fix = data_files[0:conf.n_batch] x_fix=[get_image(f, conf.n_img_pix, is_crop=conf.is_crop, resize_w=conf.n_img_out_pix, is_grayscale = conf.is_gray) for f in x_fix] x_fix = np.array(x_fix).astype(np.float32) if(conf.is_gray == 1): s,h,w = x_fix.shape x_fix = x_fix.reshape(s,h, w,n_channel ) n_loop = 1 def getRealAR(): # run ae x_im =sess.run(d_img,feed_dict={g_net:x_fix}) save_images(x_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, 'anal_AE_X.png')) def getRandomG(): f_g = open(checkpoint_dir+ '/g_img.csv', 'a') # generate images from generator and ae for i in range(5): z_test =np.random.uniform(low=-1, high=1, size=(conf.n_batch, 64)).astype(np.float32) g_im =sess.run(g_img,feed_dict={z:z_test}) save_images(g_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_G.png')) # g_im = g_im/127.5 - 1. # ae_g_im =sess.run(d_img,feed_dict={g_net:g_im}) # save_images(ae_g_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_AE_G.png')) for j in range(g_im.shape[0]): f_g.write(str(g_im[j].tolist()).replace("[", "").replace("]", "")+ '\n') f_g.close() def getFixedG(f_in): l_z = list() with open(f_in,'r') as file: for line in file: l_z.append(np.fromstring(line, dtype=float, sep=',')) file.close() n_loop = int(len(l_z)/64) l_z = np.asarray(l_z) for i in range(n_loop): fr = 64*i to = 64*(i+1) z_test =l_z[fr:to] g_im =sess.run(g_img,feed_dict={z:z_test}) save_images(g_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, '_anal_fix_G.png')) #g_im = g_im/127.5 - 1. #ae_g_im =sess.run(d_img,feed_dict={g_net:g_im}) #save_images(ae_g_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_AE_G.png')) def getRandomAE(): # generate images from discriminator and ae for i in range(n_loop): z_test =np.random.uniform(low=-1, high=1, size=(conf.n_batch, conf.n_img_out_pix, conf.n_img_out_pix,n_channel)).astype(np.float32) d_im =sess.run(d_img,feed_dict={g_net:z_test}) save_images(d_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_D.png')) def saveFeatures(): # get latent value from real images (10*n_batch) for i in range(n_loop): shuffle(data_files) f_test = data_files[0:conf.n_batch] x_test=[get_image(f, conf.n_img_pix, is_crop=conf.is_crop, resize_w=conf.n_img_out_pix, is_grayscale = conf.is_gray) for f in f_test] x_test = np.array(x_test).astype(np.float32) if(conf.is_gray == 1): s,h,w = x_test.shape x_test = x_test.reshape(s,h, w,n_channel ) latent =sess.run(e_net,feed_dict={g_net:x_test}) f_latent = open(checkpoint_dir+ '/latent.csv', 'a') for k in range(latent.shape[0]): f_latent.write(str(latent[k].tolist()).replace("[", "").replace("]", "")+ '\n') f_latent.close() def getFeatures(): f_path=checkpoint_dir+'/latent.csv'#'C:/samples/img_download/wheels/wheeldesign/output/began2_anal/17-11-28-14-52/latent.csv' data = pd.read_csv(f_path) n_latent = data.shape[1] mean = [None]*n_latent std = [None]*n_latent for i in range(n_latent): #i+=1 latent = np.array(data.iloc[:, i:i+1]) mean[i] = np.mean(latent) std[i] = np.std(latent) plt.show() return mean, std def generateFeature(mean, std): z_size = len(mean) feature = [None]*z_size for i in range(z_size): feature[i] = np.random.normal(loc=mean[i], scale=std[i], size=z_size*n_loop) return feature def generateImage(feature): feature = np.array(feature) idx=0 for i in range(n_loop): f_net = feature[:,idx:idx+64] f_img =sess.run(d_img,feed_dict={e_net:f_net}) save_images(f_img, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_G_df.png')) idx+=64 def getDiscMeanFeature(mean): mean = np.array(mean) mean = mean-2 m_net = [None]*64 for i in range(64): m_net[i] = mean +1/63 *i d_mnfd =sess.run(d_img,feed_dict={e_net:m_net}) save_images(d_mnfd, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, 'anal_D_Mean_df.png')) #getFixedG(conf.log_dir+'anal/g_df/z.csv') #getRealAR() getRandomG() #getRandomAE() #saveFeatures() #z_mean, z_std = getFeatures() #z_feature = generateFeature(z_mean, z_std) #shuffle(z_feature) #generateImage(z_feature) #getDiscMeanFeature(z_mean) sess.close()
batch_gen = dataset.batches(batch_size, window_size, stride_size) for iteration, (events, controls) in enumerate(batch_gen): events = torch.LongTensor(events).to(device) assert events.shape[0] == window_size if np.random.random() < control_ratio: controls = torch.FloatTensor(controls).to(device) assert controls.shape[0] == window_size else: controls = None init = torch.randn(batch_size, model.init_dim).to(device) outputs = model.generate(init, window_size, events=events[:-1], controls=controls, teacher_forcing_ratio=teacher_forcing_ratio, output_type='logit') assert outputs.shape[:2] == events.shape[:2] loss = loss_function(outputs.view(-1, event_dim), events.view(-1)) model.zero_grad() loss.backward() norm = utils.compute_gradient_norm(model.parameters()) nn.utils.clip_grad_norm_(model.parameters(), 1.0) optimizer.step() print(f'iter {iteration}, loss: {loss.item()}') if time.time() - last_saving_time > saving_interval:
from model import generate, train from preprocess import preprocess csv_path = "data/tai_reviews.csv" txt_path = "data/tai_reviews.txt" textgenrnn_weight_path = "data/tai_reviews_weight.hdf5" textgenrnn_output_path = "data/tai_reviews_textgenrcnn.txt" num_epochs = 20 # number of training epochs num_sentences = 20 # number of sentences to be generated after training preprocess(csv_path, txt_path) train(txt_path=txt_path, weight_path=textgenrnn_weight_path, num_epochs=num_epochs) generate( weight_path=textgenrnn_weight_path, output_path=textgenrnn_output_path, num_sentences=num_sentences, )
eva_data=valid_batch_data, result_ptr=result_ptr) print("evaluation phase completed") if cfg['g']: if not cfg['t']: if len(cfg['sess_path']) == 0: print('no model sess path specified. Can not preceed.') sys.exit(1) else: result_path = cfg['sess_path'] # get id2word_dict tf.reset_default_graph() contin_path = "data/sentences.continuation" contin_text = load_data(contin_path) contin_batch_data = get_batch_data(contin_text, word2id_dict=word2id_dict, batch_size=cfg['batch_size'], max_length=cfg['max_length']) pepfile_path = os.path.join(result_path, 'continuation.txt') result_ptr = open(pepfile_path, 'w') print("start generating phase") generate(sess_path=result_path, contin_data=contin_batch_data, result_ptr=result_ptr, id2word_dict=id2word_dict, cfg=cfg) print("generating phase completed")
import tensorflow as tf from MNIST_data import input_data import tensorflow.contrib.slim as slim import model as model batch_size = 300 import global_var logdir_path = global_var.logdir_path #存储地址,读者自行设定 with tf.Graph().as_default(): mnist = input_data.read_data_sets('MNIST_data', one_hot=True) x_input = tf.placeholder(tf.float32, [None, 28, 28, 1]) Gz = model.generate(batch_size) #生成图片 Dx = model.discriminate(x_input) #判断真实的图片 Dg = model.discriminate(Gz, reuse=True) #判断图片的真假 #对生成的图像是真为判定 d_loss_real = tf.reduce_mean( slim.losses.sigmoid_cross_entropy(multi_class_labels=tf.ones_like(Dx), logits=Dx)) #对生成的图像是假为判定 d_loss_fake = tf.reduce_mean( slim.losses.sigmoid_cross_entropy(multi_class_labels=tf.zeros_like(Dg), logits=Dg)) d_loss = d_loss_real + d_loss_fake #对生成器的结果进行判定 g_loss = tf.reduce_mean( slim.losses.sigmoid_cross_entropy(multi_class_labels=tf.ones_like(Dg),
]] user_input = "" """for i in range(0, 5): #sets up grid for j in range(0, 5): grid.append(["0"])""" grid[0][2] = 1 grid[1][2] = 1 grid[2][2] = 1 grid[1][0] = 1 grid[2][1] = 1 #while(user_input != 1): for i in range(0, len(grid)): #prints grid print(grid[i]) grid = model.generate(grid) for i in range(0, len(grid)): #prints grid print(grid[i]) grid = model.generate(grid) for i in range(0, len(grid)): #prints grid print(grid[i]) grid = model.generate(grid) for i in range(0, len(grid)): #prints grid print(grid[i])
def test_live_cell_with_two_neighbours(self): c = {"status": '1', "cell_neighbours": 2} model.generate(c) self.assertEqual(c["status"], '1')
def test_dead_cell_with_three_neighbours(self): c = {"status": '0', "cell_neighbours": 3} model.generate(c) self.assertEqual(c["status"], '1')
def test_live_cell_with_one_neighbour(self): c = {"status": '1', "cell_neighbours": 1} model.generate(c) self.assertEqual(c["status"], '0')
def main(conf): logger = logging.getLogger("desc") logger.setLevel(logging.INFO) fileHandler = logging.FileHandler(os.path.join(base_dir, 'log.txt')) logger.addHandler(fileHandler) #streamHandler = logging.StreamHandler() #logger.addHandler(streamHandler) if conf.is_gray: n_channel = 1 else: n_channel = 3 # init directories checkpoint_dir = os.path.join(base_dir, conf.curr_time) if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) ##========================= DEFINE MODEL ===========================## #z = tf.random_uniform(conf.n_batch, conf.n_z), minval=-1.0, maxval=1.0) z = readz(os.path.join(base_dir, 'z.csv'), conf.n_batch) x_net = tf.placeholder( tf.float32, [conf.n_batch, conf.n_img_pix, conf.n_img_pix, n_channel], name='real_images') k_t = tf.Variable(0., trainable=False, name='k_t') # execute generator g_net, g_vars, g_conv = generate(z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=True, reuse=False) # execute discriminator e_g_net, enc_vars, e_g_conv = encode(g_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, is_train=True, reuse=False) d_g_net, dec_vars, d_g_conv = decode(e_g_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=True, reuse=False) e_x_net, _, e_x_conv = encode(x_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, is_train=True, reuse=True) d_x_net, _, d_x_conv = decode(e_x_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=True, reuse=True) g_img = tf.clip_by_value((g_net + 1) * 127.5, 0, 255) #x_img=tf.clip_by_value((x_net + 1)*127.5, 0, 255) d_g_img = tf.clip_by_value((d_g_net + 1) * 127.5, 0, 255) d_x_img = tf.clip_by_value((d_x_net + 1) * 127.5, 0, 255) d_vars = enc_vars + dec_vars d_loss_g = tf.reduce_mean(tf.abs(d_g_net - g_net)) d_loss_x = tf.reduce_mean(tf.abs(d_x_net - x_net)) d_loss = d_loss_x - k_t * d_loss_g g_loss = tf.reduce_mean(tf.abs(d_g_net - g_net)) d_loss_prev = d_loss g_loss_prev = g_loss k_t_prev = k_t g_optim = tf.train.AdamOptimizer(conf.g_lr).minimize(g_loss, var_list=g_vars) d_optim = tf.train.AdamOptimizer(conf.d_lr).minimize(d_loss, var_list=d_vars) balance = conf.gamma * d_loss_x - g_loss measure = d_loss_x + tf.abs(balance) with tf.control_dependencies([d_optim, g_optim]): k_update = tf.assign( k_t, tf.clip_by_value(k_t + conf.lambda_k * balance, 0, 1)) # start session sess = tf.InteractiveSession() init = tf.global_variables_initializer() sess.run(init) loadWeight(sess, conf) x_fix = readx(os.path.join(base_dir, 'x.csv'), conf.n_batch) x_fix = x_fix.reshape(conf.n_batch, conf.n_conv_hidden, conf.n_img_out_pix, n_channel) n_loop = 2 for itr in range(n_loop): fetch_dict = { "kupdate": k_update, "m": measure, "b": balance, 'gnet': g_net, 'dgnet': d_g_net, 'dxnet': d_x_net, 'xnet': x_net, 'gconv': g_conv, 'egconv': e_g_conv, 'dgconv': d_g_conv, 'exconv': e_x_conv, 'dxconv': d_x_conv, "dlossx": d_loss_x, "gloss": g_loss, "dloss": d_loss, "kt": k_t, 'gimg': g_img, 'dgimg': d_g_img, 'dximg': d_x_img, } result = sess.run(fetch_dict, feed_dict={x_net: x_fix}) logger.info('measure: ' + str(result['m'])) logger.info('balance: ' + str(result['b'])) logger.info('gloss: ' + str(result['gloss'])) logger.info('dloss: ' + str(result['dloss'])) logger.info('dlossx: ' + str(result['dlossx'])) logger.info('k_t: ' + str(result['kt'])) if itr == 0: gconv = result['gconv'] for i in range(len(gconv)): conv = np.clip((gconv[i] + 1) * 127.5, 0, 255) s, h, w, c = conv.shape for j in range(c): c_img = conv[:, :, :, j:j + 1] save_image( c_img, os.path.join( checkpoint_dir, 'gen_' + str(i) + '_' + str(j) + '_' + str(h) + '_conv.png')) dgconv = result['dgconv'] for i in range(len(dgconv)): conv = np.clip((dgconv[i] + 1) * 127.5, 0, 255) s, h, w, c = conv.shape for j in range(c): c_img = conv[:, :, :, j:j + 1] save_image( c_img, os.path.join( checkpoint_dir, 'dec_g_' + str(i) + '_' + str(j) + '_' + str(h) + '_conv.png')) dxconv = result['dxconv'] for i in range(len(dxconv)): conv = np.clip((dxconv[i] + 1) * 127.5, 0, 255) s, h, w, c = conv.shape for j in range(c): c_img = conv[:, :, :, j:j + 1] save_image( c_img, os.path.join( checkpoint_dir, 'dec_x_' + str(i) + '_' + str(j) + '_' + str(h) + '_conv.png')) exconv = result['exconv'] for i in range(len(exconv)): conv = np.clip((exconv[i] + 1) * 127.5, 0, 255) s, h, w, c = conv.shape for j in range(c): c_img = conv[:, :, :, j:j + 1] save_image( c_img, os.path.join( checkpoint_dir, 'enc_x_' + str(i) + '_' + str(j) + '_' + str(h) + '_conv.png')) egconv = result['egconv'] for i in range(len(egconv)): conv = np.clip((egconv[i] + 1) * 127.5, 0, 255) s, h, w, c = conv.shape for j in range(c): c_img = conv[:, :, :, j:j + 1] save_image( c_img, os.path.join( checkpoint_dir, 'enc_g_' + str(i) + '_' + str(j) + '_' + str(h) + '_conv.png')) gnet = result['gnet'] dgnet = result['dgnet'] dxnet = result['dxnet'] xnet = result['xnet'] for i in range(conf.n_batch): logger.info( 'g_net: ' + str(gnet[i].tolist()).replace("[", "").replace("]", "")) logger.info( 'd_g_net: ' + str(dgnet[i].tolist()).replace("[", "").replace("]", "")) logger.info( 'x_net: ' + str(xnet[i].tolist()).replace("[", "").replace("]", "")) logger.info( 'd_x_net: ' + str(dxnet[i].tolist()).replace("[", "").replace("]", "")) gimg = result['gimg'] dgimg = result['dgimg'] dximg = result['dximg'] save_image(gimg, os.path.join(checkpoint_dir, str(itr) + '_final_g_img.png')) save_image( dgimg, os.path.join(checkpoint_dir, str(itr) + '_final_d_g_img.png')) save_image( dximg, os.path.join(checkpoint_dir, str(itr) + '_final_d_x_img.png')) sess.close()
* numpy.random.randn(*theta.shape)) def update_theta(theta, p, eps): return theta + p * eps p = update_p(p, theta, x, eps) theta = update_theta(theta, p, eps) return p, theta theta1_all = numpy.empty((args.epoch * n_batch,), dtype=numpy.float32) theta2_all = numpy.empty((args.epoch * n_batch,), dtype=numpy.float32) ssg = stepsize.StepSizeGenerator(args.epoch, args.eps_start, args.eps_end) theta = model.sample_from_prior() p = numpy.random.randn(*theta.shape) x = model.generate(args.N, args.theta1, args.theta2) for epoch in six.moves.range(args.epoch): perm = numpy.random.permutation(args.N) for i in six.moves.range(0, args.N, args.batchsize): if args.initialize_moment: p = numpy.random.randn(*theta.shape) for l in six.moves.range(args.L): p, theta = update( p, theta, x[perm][i: i + args.batchsize], ssg(epoch)) theta1_all[epoch * n_batch + i // args.batchsize] = theta[0] theta2_all[epoch * n_batch + i // args.batchsize] = theta[1] if i == 0: print(epoch, theta, theta[0] * 2 + theta[1]) fig, axes = pyplot.subplots(ncols=1, nrows=1)
yaml.dump(model_dict, yamlfile) if docopt_dict['generate']: model_name = docopt_dict['--model-name'] model_dir = os.path.abspath(docopt_dict['--dir']) + '/' config_file = model_dir + model_name + '_config.yaml' try: with open(config_file, 'r') as yamlfile: old_model_dict = yaml.load(yamlfile) except (IOError, FileNotFoundError) as e: print('Cannot find {}'.format(config_file)) raise e model_dict = _arg_parse(docopt_dict, old_model_dict) #Must be run through same transformation as raw text seed_text = tf_parser.to_ascii(model_dict['seed']) model.generate(model_dict, seed_text) #Update configuration file with open(model_dict['config-file'], 'w') as yamlfile: yaml.dump(model_dict, yamlfile)
import model import utils import random import unidecode import string import torch import torch.nn as nn from settings import INPUT_PATH, INPUT_SIZE, HIDDEN_SIZE, OUTPUT_SIZE, EPOCHS, NUM_LAYERS, LEARNING_RATE, CHUNK_LENGTH, PREDICT_LENGTH, TEMPERATURE, PRINT_EVERY input_string = unidecode.unidecode(open(INPUT_PATH, 'r').read()) network = model.RNN(INPUT_SIZE, HIDDEN_SIZE, OUTPUT_SIZE, NUM_LAYERS) optimizer = torch.optim.Adam(network.parameters(), lr=LEARNING_RATE) criterion = nn.CrossEntropyLoss() for epoch in range(EPOCHS): input_tensor, target_tensor = utils.random_training_set(input_string, CHUNK_LENGTH) loss = model.train(network, input_tensor, target_tensor, CHUNK_LENGTH, optimizer, criterion) if epoch % PRINT_EVERY == 0: print(loss) print(model.generate(network, random.choice(string.ascii_uppercase), PREDICT_LENGTH, TEMPERATURE), '\n') with open(OUTPUT_PATH, 'a') as file: for i in range(10): file.write(model.generate(network, random.choice(string.ascii_uppercase), 200, TEMPERATURE))