nu = train_loader.nu ny = train_loader.ny # Options for the solver # solver_options = nlsdp.make_stochastic_nlsdp_options(max_epochs=max_epochs, lr=5.0E-4, mu0=100, lr_decay=0.98) solver_options = nlsdp.make_stochastic_nlsdp_options( max_epochs=max_epochs, lr=lr, mu0=10, lr_decay=lr_decay, patience=patience) # Train an LSTM network name = "RNN_sub{:d}_val{:d}".format(subject, val_set) model = rnn.rnn(nu, lstm_width, ny, layers=layers) log, best_model = train.train_model_ipm(model, train_loader=train_loader, val_loader=val_loader, test_loader=test_loader, options=solver_options) test_and_save_model(name, best_model, train_loader, val_loader, test_loader, log, params=scaling_factors) # # for gamma in [0.5, 1, 1.5, 2.5, 5]:
def generate_model(nu, ny, batches, args, loader=None, solver="SCS"): r'Function to easily re-generate models for training on different data sets.' print('Creating model', args.model, ' width = ', args.width) if args.model == "cirnn": model = ciRNN.ciRNN(nu, args.width, ny, args.depth, nBatches=batches) model.init_l2(0.0, 1E-3) constraints = { "lmi": model.contraction_lmi(0, 1E-5), "inequality": None } elif args.model == "RobustRnn": model = RobustRnn.RobustRnn(nu, args.width, ny, args.res_size, nBatches=batches, method=args.multiplier, supply_rate=args.supply_rate) if args.supply_rate == "dl2_gain": print('\t supply rate: dl2 gamma = ', args.gamma) if args.init_type == 'n4sid': model.init_lipschitz_ss(gamma=args.gamma, loader=loader, solver=solver) else: model.initialize_lipschitz_LMI(gamma=args.gamma, eps=1E-3, init_var=args.init_var) constraints = { "lmi": model.lipschitz_LMI(gamma=args.gamma, eps=1E-5), "inequality": [model.multiplier_barrier] } elif args.supply_rate == "stable": print('\t supply rate: stable') if args.init_type == 'n4sid': model.init_stable_ss(loader) else: model.initialize_stable_LMI(eps=1E-3, init_var=args.init_var, obj=args.init_type) constraints = { "lmi": model.stable_LMI(eps=1E-5), "inequality": [model.multiplier_barrier] } elif args.model == "rnn": model = rnn.rnn(nu, args.width, ny, nBatches=batches) constraints = {"lmi": None, "inequality": None} elif args.model == "lstm": # only constraint is invertible E model = lstm.lstm(nu, args.width, ny, nBatches=batches) constraints = {"lmi": None, "inequality": None} elif args.model == 'dnb': model = dnb.dnbRNN(nu, args.width, ny, nBatches=batches) constraints = { "lmi": model.norm_ball_lmi(eps=0.001), "inequality": None } return model, constraints
model.load_state_dict(torch.load(path + name + ".params")) res = vary_amplitude(model) io.savemat('./results/msd/generalization/amp_' + name + '.mat', res) # # lstm print("Running tests on LSTM") name = 'lstm_w10_gamma0.0_n4' model = lstm.lstm(nu, width, ny, layers=1, nBatches=batches) model.load_state_dict(torch.load(path + name + ".params")) res = vary_amplitude(model) io.savemat('./results/msd/generalization/amp_' + name + '.mat', res) # rnn print("Running tests on RNN") name = 'rnn_w10_gamma0.0_n4' model = rnn.rnn(nu, width, ny, 1, nBatches=batches) model.load_state_dict(torch.load(path + name + ".params")) res = vary_amplitude(model) io.savemat('./results/msd/generalization/amp_' + name + '.mat', res) # cirnn print("Running tests on cirnn") name = 'cirnn_w10_gamma0.0_n4' model = ciRNN.ciRNN(nu, width, ny, 1, nBatches=100) model.load_state_dict(torch.load(path + name + ".params")) res = vary_amplitude(model) io.savemat('./results/msd/generalization/amp_' + name + '.mat', res) # srnn print("Running tests on srnn") name = 'dnb_w10_gamma0.0_n4'
data = new_data.copy() noise = np.random.rand(data.shape[0], data.shape[1], data.shape[2]) noise /= 10 # small amount of noise post_ground_truth = np.append(ground_truth[:, 1:, :], ground_truth[:, 0:1, :], axis=1) ########## LOAD MODEL ########## learning_rate = 1e-4 model = rnn(embedding_size=256, recurrent_dropout=0, single_timestep_elements=data[0].shape[-1], single_timestep_gt=post_ground_truth[0].shape[-1], learning_rate=learning_rate, loss='categorical_crossentropy') print(model.summary()) ########## CALLBACKS ########## callbacks = [] tqdm_callback = TQDMCallback(leave_inner=False, show_inner=False, outer_description="Training") callbacks.append(tqdm_callback) ########## TRAIN ##########