def build(self): self._inputs() self._mean, self._log_sigma = self._generate_condition( self.mean_caption) # Sample conditioning from a Gaussian distribution parametrized by a Neural Network self.z = helper.sample(self._mean, self._log_sigma) z = tf.reshape(self.z, (self.batch_size, 5, self.cfg.emb.emb_dim)) def one_pass_over_the_image(image, range): # Encode the image emb = z[:, range, :] encoded = self._encoder(image, emb) # Decode the image reconstructed_hole = self._decoder(encoded) generated_image = helper.reconstructed_image( reconstructed_hole, self.true_image) return generated_image self.generated_images = tf.scan( lambda image, index: one_pass_over_the_image(image, index), elems=tf.range(5), initializer=self.cropped_image) self.generated_image = self.generated_images[-1] self.reconstructed_hole = self.generated_image[:, 16:48, 16:48, :] self._losses() # self._adversarial_loss() self._optimize() self._summaries()
def PrintResults(): for diversity in [0.5, 1.0, 1.2]: print('----- diversity:', diversity) generated = '' sentence = SENTENCE sentence = sentence.lower() generated += sentence for i in range(400): x = np.zeros((1, SEQUENCE_LENGTH, len(chars))) for t, char in enumerate(sentence): x[0, t, char_to_index[char]] = 1. predictions = model.predict(x, verbose=0)[0] next_index = helper.sample(predictions, diversity) next_char = indices_char[next_index] generated += next_char sentence = sentence[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() print() print()
def generate_text(seed_text, num_words): input_text= seed_text for _ in range(num_words): #tokenize text to ints int_text = helper.tokenize_punctuation(input_text) int_text = int_text.lower() int_text = int_text.split() int_text = np.array([word_to_int[word] for word in int_text], dtype=np.int32) #pad text if it is too short, pads with zeros at beginning of text, so shouldnt have too much noise added int_text = pad_sequences([int_text], maxlen=sequence_length) #predict next word: prediction = model.predict(int_text, verbose=0) output_word = int_to_word[helper.sample(prediction, temp=0.3)] #append to the result input_text += ' ' + output_word #convert tokenized punctuation and other characters back result = helper.untokenize_punctuation(input_text) return result
def infer(input_text): input_text = preprocess(input_text) print('input_text***********', input_text) generated = '' generated += input_text for i in range(400): x = np.zeros((1, SEQUENCE_LENGTH, len(chars))) for t, char in enumerate(input_text): x[0, t, char_to_index[char]] = 1. predictions = model.predict(x, verbose=0)[0] next_index = helper.sample(predictions, DIVERSITY) next_char = indices_char[next_index] generated += next_char input_text = input_text[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() return generated
def build(self): self._inputs() self._mean, self._log_sigma = self._generate_condition( self.mean_caption) # Sample conditioning from a Gaussian distribution parametrized by a Neural Network self.z = helper.sample(self._mean, self._log_sigma) # Encode the image self.z_vec = self._encoder(self.cropped_image, self.z) # Decode the image self.reconstructed_hole = self._decoder(self.z_vec) self.generated_image = helper.reconstructed_image( self.reconstructed_hole, self.true_image) self._losses() self._adversarial_loss() self._optimize() self._summaries()
def CPD_MWU(X, F, sketching_rates, lamb, eps, nu, rank, num_iterations=100): # Keep residual errors + res time error = [] res_time = 0 # Cache norm + Id + unfolding of X norm_x = norm(X) Id = np.eye(rank) X_unfold = [unfold(X, mode=0), unfold(X, mode=1), unfold(X, mode=2)] # Initialize weights weights = np.array([1] * len(sketching_rates)) / (len(sketching_rates)) # Randomly initialize A,B,C dim_1, dim_2, dim_3 = X.shape A, B, C = rand_init(dim_1, rank), rand_init(dim_2, rank), rand_init(dim_3, rank) # Append initialization residual error error.append(residual_error(X_unfold[0], norm_x, A, B, C)) # Run CPD_MWU for num iterations for i in range(num_iterations): # Select sketching rate with probability proportional to w_i s = sample(sketching_rates, weights) # Solve Ridge Regression for A,B,C A, B, C = update_factors(A, B, C, X_unfold, Id, lamb, s, rank) # Update weights if bern(eps) == 1 and len(sketching_rates) > 1: update_weights(A, B, C, X_unfold, Id, norm_x, lamb, weights, sketching_rates, rank, nu, eps) print("iteration:", i) start = time.time() error.append(residual_error(X_unfold[0], norm_x, A, B, C)) end = time.time() res_time += end - start return A, B, C, np.array(error), res_time
def searchfive(): print('$' * 10) import numpy as np from keras.models import load_model x = np.zeros((1, 40, 49)) # M = load_model("lyrical_lstm.h5") # M._make_predict_function() # print(M.predict(x, verbose=0)[0]) # print(predictor(x)) print(loaded_model.predict(x, verbose=0)[0]) print('^' * 10) content = flask.request.get_json(silent=True) input_text = content['search_text'] print(input_text, '#' * 10) input_text = ' '.join(five.get_sentences(input_text)) print('@' * 10, input_text, '@' * 10) return flask.jsonify({'generated': input_text}) # generated = lyric.infer(input_text) input_text = preprocess(input_text) generated = '' generated += input_text for i in range(400): x = np.zeros((1, SEQUENCE_LENGTH, len(chars))) for t, char in enumerate(input_text): x[0, t, char_to_index[char]] = 1. predictions = loaded_model.predict(x, verbose=0)[0] next_index = helper.sample(predictions, DIVERSITY) next_char = indices_char[next_index] generated += next_char input_text = input_text[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() return flask.jsonify({'generated': generated})
def generate(custom_seed): notes_in_bar = 17 # 16 notes plus a BAR seperator seq_len = 8 * notes_in_bar # 8 bars of notes bars = 32 * notes_in_bar # 32 bars worth of notes to generate by the model path = 'notes.txt' text = open(path).read() # opens the notes.txt file and prints the length print('corpus length:', len(text)) notes = text.split(' ') chars = set(notes) # creates a set of every unique note in the text file text = notes print('amount of notes:', len(text)) char_indices = dict((c, i) for i, c in enumerate( chars)) # maps every unique note to a value (key value pairs) next_pred = sorted( list(set(text)) ) # creates a sorted list of every unique note to draw upon for predicting the next in the sequence num_notes = len( char_indices ) # integer value of the total number of unique notes in the text file, printed to console print('total chars:', len(next_pred)) sentences = [ ] # two blank lists, one to contain all the note sequences, and the other to contain the note sequences that follow next_chars = [] for i in range(0, len(text) - seq_len, notes_in_bar): sentences.append( text[i:i + seq_len] ) # for loop to append the note sequences to the two lists as described above next_chars.append(text[i + seq_len]) print('nb sequences:', len(sentences)) # prints the number of note sequences print('Vectorisation...') X = np.zeros( (len(sentences), seq_len, num_notes), dtype=np.bool ) # 3-d array of dimensions, number of note sequences, by sequence length, by number of unique notes y = np.zeros( (len(sentences), num_notes), dtype=np.bool ) # 2-d target array of dimensions, number of note sequences, by number of unique notes for i, sentence in enumerate(sentences): for t, char in enumerate(sentence): X[i, t, char_indices[ char]] = 1 # double for loop to append both X and y with their respective values for one-hot encoded notes y[i, char_indices[next_chars[i]]] = 1 result_directory = 'results/' # directory to store the resulting text files batch_size = seq_len # feed sequences of 8 bars at a time into the model num_epochs = [ 1, 30, 60 ] # output new txt files of predictions after the number of training epochs listed counter = 1 # counter for number of training iterations start_index = random.randint(0, len(text) - seq_len - 1) # random value for seed if custom_seed: seed_path = 'seed.txt' # opens seed.txt if custom seed is selected, prints confirmation to console print("Custom seed text applied") else: print("Custom seed not selected. Random seed to be taken from dataset" ) # prints confirmation of random seed to console model = lstm( 128, seq_len, num_notes ) # creates the lstm model, default 128 units but can be increased for epoch in num_epochs: print() print('Iteration: ', counter, ' Number of epochs: ', epoch) model.fit( X, y, batch_size=batch_size, epochs=epoch ) # begins training by feeding note sequences and target sequences in 8 bar batches. print() print('Saving model after %d epochs...' % (epoch)) model.save( '%smodel_after_%d_epochs.h5' % (result_directory, epoch), overwrite=True) # saves the trained model to a .h5 file for reuse for temperature in [ 0.2, 0.4, 0.6, 0.8, 1.0, 1.2 ]: # temperature values for reweighting probability distributions for next note in sequence txt_file = '%siter_%d_epochs_%d_temperature_%4.2f.txt' % ( result_directory, counter, epoch, temperature ) # saves the text file with the counter, epochs and temperature to distinguish from one another with open(txt_file, 'w') as f: print() print('Saving temperature %4.2f to' % temperature, txt_file) f.write( 'temperature:%4.2f\n' % temperature ) # writes the temperature value to the beginning of the text file generated = [] if custom_seed: seed_sentence = open(seed_path).read( ) # if custom seed is selected, chooses this for seed seed = seed_sentence.split(' ') else: seed = text[ start_index:start_index + seq_len] # if no custom seed, takes random seed from dataset sentence = seed generated = generated + sentence # appends the seed to the start of the text file for i in range(bars): x = np.zeros( (1, seq_len, num_notes) ) # creates a new numpy array for dimensions 1, by 8 bars, by number of unique notes for j, char in enumerate(sentence): x[0, j, char_indices[ char]] = 1. # appends the seed sentence to the beginning of the numpy array x preds = model.predict( x, verbose=0 )[0] # generates the softmax probability distribution with respect to the seed and what the model has learned next_index = sample( preds, temperature ) # reweights the probability distribution by the temperature and chooses next note in sequence next_char = next_chars[ next_index] # finds the note value in the list of notes generated.append( next_char ) #appends the predicted note to the end of the already generated notes sentence = sentence[1:] sentence.append(next_char) sys.stdout.flush( ) # flushes the buffer, meaning everything in the buffer is written before proceeding to next step loop if custom_seed: f.write(sentence + '\n') else: f.write(' '.join(sentence) + '\n') f.write(' '.join(generated)) counter = counter + 1
"final.h5") # you can skip training by loading the trained weights for diversity in [0.2, 0.5, 1.0, 1.2]: print() print('----- diversity:', diversity) generated = '' # insert your 40-chars long string. OBS it needs to be exactly 40 chars! sentence = "The grass is green and my car is red lik" sentence = sentence.lower() generated += sentence print('----- Generating with seed: "' + sentence + '"') sys.stdout.write(generated) for i in range(400): x = np.zeros((1, SEQUENCE_LENGTH, len(chars))) for t, char in enumerate(sentence): x[0, t, char_to_index[char]] = 1. predictions = model.predict(x, verbose=0)[0] next_index = helper.sample(predictions, diversity) next_char = indices_char[next_index] generated += next_char sentence = sentence[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() print()
char_to_index, indices_char = helper.get_chars_index_dicts(CHARS) """ Load the model """ modelFile = Path(PATH_TO_MODEL) if modelFile.is_file(): model = load_model(PATH_TO_MODEL) """ GEN_LENGTH needs to be the same that was used when model was saved """ generated = '' sentence = CORPUS[0:GEN_LENGTH] sentence = sentence.lower() generated += sentence for z in range(50): for i in range(GEN_LENGTH): x = np.zeros((1, GEN_LENGTH, len(CHARS))) for t, char in enumerate(sentence): x[0, t, char_to_index[char]] = 1. predictions = model.predict(x, verbose=0)[0] next_index = helper.sample(predictions, DIVERSITY) next_char = indices_char[next_index] generated += next_char sentence = sentence[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() print()
def bras_CPD(F, X, rank, B, alpha, beta, num_iterations=100, max_time=None): # bookkeeping total_time = 0 res_error = [] time = [] # Cache norm start = timer() norm_x = norm(X) F_norm = [norm(F[0]), norm(F[1]), norm(F[2])] # Randomly initialize A,B,C dim_1, dim_2, dim_3 = X.shape A = [ rand_init(dim_1, rank), rand_init(dim_2, rank), rand_init(dim_3, rank) ] total_col = {0: dim_2 * dim_3, 1: dim_1 * dim_3, 2: dim_1 * dim_2} # Cache Unfoldings X_unfold = [unfold(X, mode=0), unfold(X, mode=1), unfold(X, mode=2)] # Finish timing initialization step end = timer() total_time += end - start # Append initialization residual error res_error.append(residual_error(X_unfold[0], norm_x, A[0], A[1], A[2])) time.append(total_time) # Run bras_CPD if max_time == None: for r in range(num_iterations): if (r + 1) % 5000 == 0: print("iteration:", r) # Time start step start = timer() # Randomly select mode n time update. n = sample(3) # Generate sketching indices idx = generate_sketch_indices(B, total_col[n]) # Update Factor matrix update_factor_bras(X_unfold, A, idx, n, rank, alpha) # Update learning rate alpha /= (r + 1)**beta # Time iteration step end = timer() total_time += end - start # Append error res_error.append( residual_error(X_unfold[0], norm_x, A[0], A[1], A[2])) else: r = 1 while total_time < max_time: # Time start step start = timer() # Randomly select mode n time update. n = sample(3) # Generate sketching indices idx = generate_sketch_indices(B, total_col[n]) # Update Factor matrix update_factor_bras(X_unfold, A, idx, n, rank, alpha) # Update learning rate alpha /= (r + 1)**beta r += 1 # Time iteration step end = timer() total_time += end - start # Append error res_error.append( residual_error(X_unfold[0], norm_x, A[0], A[1], A[2])) time.append(total_time) return total_time, res_error, time
'B', 'wjets', 'ttjets', 'ttjets_ht200', 'ttjets_ht200met100', 'ttjets_ht300met100', 'ttjets_ht400met120' ] if not 'loaded' in globals(): print 'loaded is not in globals' global loaded loaded = False if not loaded: ttjets_sl = helper.sample('ttjets_sl','../closureTest/ttjets_semi_closureOutput_SS.root' ) ttjets_fl = helper.sample('ttjets_fl','../closureTest/ttjets_full_closureOutput_SS.root' ) ttjets_ha = helper.sample('ttjets_ha','../closureTest/ttjets_hadronic_closureOutput_SS.root' ) singletop = helper.sample('singletop','../closureTest/singletop_closureOutput_SS.root' ) wjets = helper.sample('wjets' ,'../closureTest/wnjets_closureOutput_SS.root' ) rares = helper.sample('rares' ,'../closureTest/rares_closureOutput_SS.root') #qcdmuenr = helper.sample('qcdmuenr' ,'../closureTest/qcdmuenr_closureOutput_SS.root') # dyjets = helper.sample('dyjets' ,'../closureTest/dyjets_closureOutput.root' ) doublemu = helper.sample('doublemu' ,'../closureTest/doublemu_closureOutput_SS.root') doubleel = helper.sample('doubleel' ,'../closureTest/doubleel_closureOutput_SS.root') samples = [ rares, wjets, ttjets_fl, ttjets_sl, ttjets_ha, singletop, doublemu, doubleel] #samples = [ ttjets_sl] for sample in samples: for sr in SRs: sample.regions.append(helper.region(sample.name, sr))
seq_length=seq_length, device=device) # and make our data loaders # batch size is exactly 1 character by default, which is exactly what we need train_loader = DataLoader(train_data) validation_loader = DataLoader(validation_data) # Part 3: modelling # we create our model model = CharRNN(num_chars).to(device) # and the initial hidden state (a tensor of zeros) initial_state = model.init_hidden(batch_size, device) # we evaluate the capability of our model # a character to parameter ratio approaching 1 is optimal # too many parameters and the model may overfit # too few and the model may underfit char_param_ratio = len(text) / count_parameters(model) print("Character to model parameter ratio: %f\n" % char_param_ratio) # Part 4: training train(model, initial_state, train_loader=train_loader, validation_loader=validation_loader, epochs=100) # Part 5: evaluation print(sample(model, char2int))
net = CharRNN(chars, n_hidden=512, n_layers=2) print(net) n_seqs, n_steps = 128, 100 # you may change cuda to True if you plan on using a GPU! # also, if you do, please INCREASE the epochs to 25 helper.train(net, encoded, epochs=1, n_seqs=n_seqs, n_steps=n_steps, lr=0.001, cuda=False, print_every=10) print(helper.sample(net, 2000, prime='Anna', top_k=5, cuda=False)) # change the name, for saving multiple files model_name = 'rnn_1_epoch.net' checkpoint = { 'n_hidden': net.n_hidden, 'n_layers': net.n_layers, 'state_dict': net.state_dict(), 'tokens': net.chars } #Save trained model with open(model_name, 'wb') as f: torch.save(checkpoint, f)
#print(maxlen, maxWord,len(mh.char_indices),len(mh.word_indices)) Xchar,Xword,Xcon,dummy = helper.getCharAndWordNoState(recipes,contextVec,maxlen,maxWord,mh.char_indices,mh.word_indices,step=1,predict=True) newLength = (Xchar.shape[0])/4 inds = [(newLength*(divind+1))-1 for divind in range(0,batchSize)] #helper.checkExampleWords(Xword[inds[1]],mh.vocab) Xchar = Xchar[inds] Xword = Xword[inds] Xcon = Xcon[inds] preds = model.predict_on_batch([Xchar,Xword,Xcon])[0] for d,pred in enumerate(preds): #print(d,pred) next_index = helper.sample(pred, div[d]) next_char = mh.indices_char[next_index] if recipes[d][-1] != "$": recipes[d] += next_char dropIt = [(r[-1] == '$')*1. for r in recipes] if int(np.sum(dropIt)) == batchSize: break with open("../somerecipesfriend.txt",'a') as f: for d,rec in enumerate(recipes): print("with diversity:",div[d],"\n\n",rec,'\n\n') f.write("with diversity:"+str(div[d])+"\n\n"+rec+'\n\n')