def make_obama(ngram_order=3, select=None): sys.stderr.write('assembling obama...\n') model_fname = make_model_fname(ngram_order) model_loc = os.path.join(LANG_MODEL_DIR, model_fname) # if not os.path.exists(model_loc): # print model_loc # raise Exception('no {order}-gram model found for Obama)'.format( # order=ngram_order)) build_model(ngram_order) obama = Obama(model_loc) sys.stderr.write('loaded.\n') return obama
def model_build(df_train, opts, data, bootstrap=False, num_runs=1, sample_size='all', probab=False): """Generate the model. xdata.joblib should already exist. To get more control over the model, run build_model directly. Args: df_train (pd.DataFrame or str, optional): Training subset of df. Can be set to a str to use whole df. To get full df to subset, use load_df(). Defaults to 'all'. opts (dict): Options dict. data (list): Embeddings-realted data. bootstrap (bool, optional): Whether to sample with replacement or not. Defaults to False. num_runs (int, optional): Number of runs to aggregate. Defaults to 1. sample_size (int, str, optional): Size of training set for each run, sampled from the training set. Defaults to 'all'. probab (bool, optional): Whether to calculate class probability. """ label = opts['label'] cosine = opts['cosine'] cval = opts['cval'] original_dict = data[0] embed_dict = data[3] df = df_train sz = len(df) if isinstance(sample_size, str) else sample_size for n in range(num_runs): print('----- Training ' + label + '_' + str(n) + ' -----') boot_sample = np.random.choice(df.index, size=sz, replace=bootstrap) build_model(df, embed_dict, cval=cval, label=label, nrun=str(n), sample=boot_sample, proba=probab, cosine=cosine, original_dict=original_dict)
def load(path, args, compile=False): #hàm để taỉ model model = build_model(args) model.load_weights(path) sgd = optimizer(args) if compile: model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) return model
def main(): # Parse input data args = parsing_inputs() # Obtain the dataloaders and a dictionary class_to_idx we will use during prediction trainloader, validloader, testloader, class_to_idx = load_data(args) # Now we download the model based on the input and select the device we will train it on possible_inputs = {'vgg16': 25088, 'alexnet': 9216} model, device = build_model(possible_inputs, args) # The next step is to define the criterion and train the model criterion = nn.NLLLoss() train(model, device, args, trainloader, validloader, criterion) # We then perform a validation test on new unseen data with torch.no_grad(): validation_test(model, testloader, device, criterion) # Finally we save the checkpoint save_check(args, model, class_to_idx, possible_inputs)
# print('epochs = {!r}'.format(parse_results.epochs)) # print('gpu = {!r}'.format(parse_results.gpu)) data_dir = parse_results.data_directory save_dir = parse_results.save_dir arch = parse_results.arch learning_rate = float(parse_results.learning_rate) hidden_units = int(parse_results.hidden_units) epochs = int(parse_results.epochs) gpu = parse_results.gpu # Load and preprocess data image_datasets, train_loader, valid_loader, test_loader = preproc(data_dir) # Building and training the classifier model_init = build_model(arch, hidden_units) model, optimizer, criterion = train_model(model_init, train_loader, valid_loader, learning_rate, epochs, gpu) # Save the checkpoint model.to('cpu') model.class_to_idx = image_datasets['train'].class_to_idx checkpoint = { 'model': model, 'state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict, 'criterion': criterion, 'epochs': epochs, 'class_to_idx': model.class_to_idx }
ax = plt.gca() ax.set_xticks(np.arange(0.5, nrows, 1)) ax.set_yticks(np.arange(0.5, ncols, 1)) ax.set_xticklabels([]) ax.set_yticklabels([]) canvas = np.copy(qmaze.maze) for row,col in qmaze.visited: canvas[row,col] = 0.6 rat_row, rat_col, _ = qmaze.state canvas[rat_row, rat_col] = 0.3 # rat cell canvas[nrows-1, ncols-1] = 0.9 # cheese cell img = plt.imshow(canvas, interpolation='none', cmap='gray') plt.show() maze = np.array([ [ 1., 0., 1., 1., 1., 1., 1.], [ 1., 1., 1., 0., 0., 1., 0.], [ 0., 0., 0., 1., 1., 1., 0.], [ 1., 1., 1., 1., 0., 0., 1.], [ 1., 0., 0., 0., 1., 1., 1.], [ 1., 0., 1., 1., 1., 1., 1.], [ 1., 1., 1., 0., 1., 1., 1.] ]) model = train_model.build_model(maze) qmaze=Qmaze(maze) train_model.qtrain(model, maze, epochs=1000, max_memory=8*maze.size, data_size=32) train_model.play_game(model, qmaze, (0,0)) show(qmaze)
elif "questions.txt" in os.listdir(): notes = open("questions.txt").readlines() else: print("Need answers.txt or questions.txt to select random starting note sequence. Exiting... ") print("Usage: create_dataset.py path_to_midi_files") note_sequence = random.choice(notes) note_sequence = note_sequence.rstrip() return note_sequence if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: " + sys.argv[0] + " model_name\n") sys.exit(1) model_name = sys.argv[1] if model_name not in os.listdir(): print("Must run train_model.py before generating. Exiting...") print("Usage: train_model.py model_name") sys.exit(1) x, y, ctable, chars = md.prepare_dataset() model = md.build_model(len(chars)) model.load_weights(model_name) note_sequence = select_random_note_sequence() new_song = generate_long(note_sequence, ctable, model, 20) music_objects=create_music_objects(new_song) write_to_file(music_objects, "test.midi") # hard coded midi name