def validate_solutions(solutions): ''' Prints the validation for each solution in 'solutions' Parameters ---------- solutions : list A list with the results of the TS. This list is a list of dicts under the following form: { 'time': float, # The time wasted in the search 'best': list # The best solution found in the search, 'total_i': int, # The number of the final iteration 'i_best': int, # The iteration where the best solution was found 'fault': list, # The list with the faulted lines 'i_local': int, # The parameter of the max local search 'itm': int, # The parameter of the max iteration to reset the TS 'max_i': int, # The parameter of the max iteration set in the TS } ''' for i, sol in enumerate(solutions): print('Search #%d:' % i, ev.validate(sol['best']))
def evaluate_solutions(results): ''' Evaluates the solutions returned by TS Parameters ---------- results : list A list with the results of the TS. This list is a list of dicts under the following form: { 'time': float, # The time wasted in the search 'best': list # The best solution found in the search, 'total_i': int, # The number of the final iteration 'i_best': int, # The iteration where the best solution was found 'fault': list, # The list with the faulted lines 'i_local': int, # The parameter of the max local search 'itm': int, # The parameter of the max iteration to reset the TS 'max_i': int, # The parameter of the max iteration set in the TS } Raises ------ Exception In case the solution don't match any supported network model. ''' for i, res in enumerate(results): print(color.blue('\nSEARCH #%d ------------' % i)) validation = ev.validate(res['best']) if validation: if len(res['best']) == len(default_topologies['10-bus']): default = default_topologies['10-bus'].copy() elif len(res['best']) == len(default_topologies['16-bus']): default = default_topologies['16-bus'].copy() elif len(res['best']) == len(default_topologies['33-bus']): default = default_topologies['33-bus'].copy() elif len(res['best']) == len(default_topologies['119-bus']): default = default_topologies['119-bus'].copy() else: raise (Exception('No such model to this solution.')) faulted = default.copy() for f in res['fault']: faulted[f] = 0 value = ev.value(res['best']) zeros = list() for j, element in enumerate(default): if element == 0: zeros.append(j) ones = list() for j, element in enumerate(default): if element == 1: ones.append(j) opened_switches = list() for j, element in enumerate(res['best']): if element == 0 and j not in zeros: opened_switches.append(j) closed_switches = list() for j, element in enumerate(res['best']): if element == 1 and j not in ones: closed_switches.append(j) print(color.green('VALIDATION: %s' % validation, 'b')) print('VALUE:', value) print('NAB:', len(ev.unsupplied_buses(faulted))) print( 'NRB:', value - (ev.value(default) - len(ev.unsupplied_buses(faulted)))) print('OPENED SW:', opened_switches) print('CLOSED SW:', closed_switches) print('TIME:', res['time']) else: print(color.red('VALIDATION: %s' % validation, 'b'))
num_epochs = args.num_epochs batch_size = len(train)/num_batches print('batch_size {}'.format(batch_size)) for i in range(num_epochs): epoch_cost = 0.0 for j in range(num_batches+1): batch = train[j*batch_size:(j+1)*batch_size] start = time.time() cost = d.train(batch, args.wrong_ans) print ("epoch: {} batch: {} cost: {} time: {}".format(i, j, cost, time.time()-start)) epoch_cost += cost print ('done with epoch {}, epoch cost = {}'.format(i, epoch_cost)) # reset adagrad weights if i % args.adagrad_reset == 0 and i != 0: d.reset_weights() if i % args.do_val == 0: validate(lr, dd, d, corpus) validate(lr, dd, d, corpus) if args.save is not None: d.save(args.save, dd.answers)
if i % 10 == 0: current = time.time() logging.debug(f"Batch time {(current - last) / 10}") print(f"Epoch {epoch}: batch: {i} / {num_batches}") logging.debug(f"EPOCH {epoch}: BATCH: {i} / {num_batches}") last = current epoch_end = time.time() print(f"Epoch {epoch}, loss {loss_train / len(train_loader)}") logging.info(f"Epoch {epoch}, loss {loss_train / len(train_loader)}") logging.info(f"Train accuracy: {(correct/total):.2f}") print(f"Train accuracy: {(correct/total):.2f}") # if epoch % 5 == 0: if epoch % epochs_per_validation == 0: val_accuracy, val_loss = validate(model, loss_func, validate_loader, device, epoch) metrics["validate_accuracy"].append(val_accuracy) metrics["validate_loss"].append(val_loss) checkpoint_path = f"{args.checkpoint_dir}/{chosen_model}-{epoch}.pt" torch.save({ 'epoch': epoch, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimiser.state_dict(), "model_name": chosen_model, }, checkpoint_path) metrics["train_loss"].append(loss_train / len(train_loader)) metrics["train_accuracy"].append(correct / total) metrics["train_epoch_time"].append(epoch_end - epoch_start) print_batch_predictions(model, validate_loader, classes, device)
num_batches = args.num_batches num_epochs = args.num_epochs batch_size = len(train) / num_batches print('batch_size {}'.format(batch_size)) for i in range(num_epochs): epoch_cost = 0.0 for j in range(num_batches + 1): batch = train[j * batch_size:(j + 1) * batch_size] start = time.time() cost = d.train(batch, args.wrong_ans) print("epoch: {} batch: {} cost: {} time: {}".format( i, j, cost, time.time() - start)) epoch_cost += cost print('done with epoch {}, epoch cost = {}'.format(i, epoch_cost)) # reset adagrad weights if i % args.adagrad_reset == 0 and i != 0: d.reset_weights() if i % args.do_val == 0: validate(lr, dd, d, corpus) validate(lr, dd, d, corpus) if args.save is not None: d.save(args.save, dd.answers)
def train(ckpt, manager, model, optimizer, word2id, id2word, epochs): ''' Trains the model by unrolling the RNN, writes summary files for logging/plotting, stores the latest model to a checkpoint so that training can be resumed at a later point in time. Arguments: - ckpt: Checkpoint object, used for storing and restoring the latest model and optimizer parameters - manager: Coordinates the storing and restoring of the latest model and optimizer parameters - model: Language model that will be trained - optimizer: Optimizer that will be assigned its old values when resuming training - word2id: vocabulary lookup table that maps words to indices - id2word: vocabulary lookup table that maps indices to words ''' # Build Training and Validation Dataset ds_train = build_dataset(PATH_TRAIN, vocab=word2id) ds_train = ds_train.shuffle(SHUFFLE_BUFFER_SIZE) ds_train = ds_train.batch(BATCH_SIZE, drop_remainder=True) ds_train = ds_train.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) #ds_train = ds_train.take(5) # uncomment for demo purposes ds_valid = build_dataset(PATH_VALID, vocab=word2id) ds_valid = ds_valid.batch(BATCH_SIZE) ds_valid = ds_valid.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) #ds_valid = ds_valid.take(1) # uncomment for demo purposes # Define Train Metrics metrics_train = {} metrics_train['loss'] = tf.metrics.Mean(name='train_loss') metrics_train['accuracy'] = tf.metrics.SparseCategoricalAccuracy( name="train_accuracy") metrics_train['top5_accuracy'] = tf.metrics.SparseTopKCategoricalAccuracy( k=5, name="train_top5_accuracy") metrics_train['total_perplexity'] = Perplexity(name="train_perplexity") # Training Loop best_validation_score = 0 for epoch in range(epochs): print(f"Start Training of Epoch {epoch}") for sentence, labels, mask in ds_train: # sentence, labels, mask \in [batch_size, sentence_length-1] train_step(model=model, optimizer=optimizer, metrics=metrics_train, sentence=sentence, labels=labels, mask=mask, id2word=id2word) print(f"Start Validation of Epoch {epoch}") validation_score, _ = validate(model=model, dataset=ds_valid, id2word=id2word, step=optimizer.iterations) print(f"Validation Score {validation_score}") ckpt.step.assign_add(1) if validation_score > best_validation_score: # current validation score is better than previous -> store checkpoints best_validation_score = validation_score save_path = manager.save() print(f"Saved checkpoint for step {int(ckpt.step)}: {save_path}")