def test_optimizer(verbose=False): coef = Parameter(0) in_ = Input() model = Circuit([in_], [in_ * coef]) y_pred, y_true = Input(), Input() loss_function = Circuit([y_pred, y_true], [(y_pred - y_true) * (y_pred - y_true)]) optimizer = Optimizer(model, loss_function, verbose) np.random.seed(0) sample_size = 100 y = np.random.random(sample_size) X = np.ones(sample_size).reshape((-1, 1)) validation_y = np.random.random(sample_size) validation_X = np.ones(sample_size).reshape((-1, 1)) model = optimizer.fit(X, y, eval_set=(validation_X, validation_y), n_epochs=10, learning_rate=0.1, batch_size=20) assert abs(model([1])[0] - np.mean(y)) < 0.01
optimizer = Optimizer(model=GridStabilityPINN, x_train=x_train, y_train=y_train, maxiter=20) parser = argparse.ArgumentParser(description="Retrain selection") parser.add_argument( 'retrain', type=str, default='No', help="Train from scratch (Yes) or use pretrained weights (No)") args = parser.parse_args() if __name__ == '__main__': if args.retrain == 'Yes': optimizer.fit(2500) with open('NNWeights10x20-{}.pickle'.format(time.strftime("%H-%M-%S")), 'wb') as handle: pickle.dump(base_model.weights, handle) if args.retrain == 'No': weights = pickle.load(open("NNWeights10x20.pickle", "rb")) flattened_weights = np.concatenate( [np.array(w).flatten() for w in weights]) optimizer.set_weights(flattened_weights) K_test = 0.2 V_test = 0.4 xbounds = [0, 1] tbounds = [0, 1] plot_contour(base_model, N_P=100, save=False)