Exemplo n.º 1
0
class RosslerDataset(Dataset):
    def __init__(self, nb_samples, init_pos, delta_t, history, only_y,
                 fake_prop):
        super().__init__()
        self.nb_samples = nb_samples
        self.init_pos = init_pos
        self.delta_t = delta_t
        self.history = history
        self.only_y = only_y
        self.fake_n = int(fake_prop * nb_samples)
        self.mask = torch.ones((self.history + 2, 1))
        self.mask[-1, 0] = 0  # Set target to 0

        self.rm = RosslerMap(delta_t=delta_t)

        self.w, self.t = self.rm.full_traj(nb_samples, init_pos)
        self.w = torch.tensor(self.w).float()
        fake_w = 20 * torch.randn((self.fake_n, 3 - 2 * int(only_y)))
        self.w = torch.cat((self.w, fake_w), dim=0)

    def __len__(self):
        # Omit the last one because we do not know the next position
        return self.nb_samples - 1 - self.history + self.fake_n

    def __getitem__(self, idx):
        # if self.only_y:
        #     ret = self.w[idx : idx + self.history + 2, 1:2]
        # else:
        #     ret = self.w[idx : idx + self.history + 2]
        # if idx >= self.nb_samples - 1 - self.history:
        #     ret *= self.mask
        ret = self.w[idx:idx + self.history + 2]
        return ret
Exemplo n.º 2
0
    def __init__(self, nb_samples, init_pos, delta_t, history, only_y,
                 fake_prop):
        super().__init__()
        self.nb_samples = nb_samples
        self.init_pos = init_pos
        self.delta_t = delta_t
        self.history = history
        self.only_y = only_y
        self.fake_n = int(fake_prop * nb_samples)
        self.mask = torch.ones((self.history + 2, 1))
        self.mask[-1, 0] = 0  # Set target to 0

        self.rm = RosslerMap(delta_t=delta_t)

        self.w, self.t = self.rm.full_traj(nb_samples, init_pos)
        self.w = torch.tensor(self.w).float()
        fake_w = 20 * torch.randn((self.fake_n, 3 - 2 * int(only_y)))
        self.w = torch.cat((self.w, fake_w), dim=0)
Exemplo n.º 3
0
    def __init__(
            self,
            delta_t: float = 1e-2,
            n_iter: int = 1e7 + 1,
            init_pos=np.array([-5.75, -1.6, 0.02]),
            mean=None,
            std=None,
    ):
        super().__init__()
        if isinstance(init_pos, tuple) or isinstance(init_pos, list):
            init_pos = np.array(init_pos)
        rossler_map = RosslerMap(delta_t=delta_t)
        self.traj, _ = rossler_map.full_traj(n_iter, init_pos)

        self.mean = mean if mean is not None else self.traj.mean(axis=0)
        self.std = std if mean is not None else self.traj.std(axis=0)
        self.traj -= self.mean
        self.traj /= self.std
        self.traj_n_1 = torch.tensor(self.traj, dtype=torch.float)
        self.traj_n_2 = torch.tensor(self.traj.copy(), dtype=torch.float)
Exemplo n.º 4
0
def newton(f, jacob, x):
    # newton raphson method
    tol = 1
    while tol > 1e-5:
        # WARNING this is true for the jacobian of the continuous system!
        tol = x
        x = x-solve(jacob(x), f(v=x))
        tol = norm(tol-x)
    return x


if __name__ == '__main__':
    Niter = 100000
    delta_t = 1e-2
    ROSSLER_MAP = RosslerMap(delta_t=delta_t)
    INIT = np.array([-5.75, -1.6,  0.02])
    if not os.path.isfile('generated_traj.dat'):

        traj, t = ROSSLER_MAP.full_traj(Niter, INIT)
        torch.save(torch.Tensor(traj), 'generated_traj.dat')

    else:
        traj = torch.load('generated_traj.dat')
        t = np.linspace(0, Niter * delta_t, Niter)
    traj_to_plot = traj[:10000]
    traj_to_plot = traj_to_plot[[False if i %
                                 8 else True for i in range(len(traj_to_plot))]]

    traj2 = torch.load('traj.dat')
    print('traj2', traj2.shape, 'traj', traj.shape)
Exemplo n.º 5
0
def newton(f, jacob, x):
    #newton raphson method
    tol = 1
    while tol > 1e-5:
        #WARNING this is true for the jacobian of the continuous system!
        tol = x
        x = x - solve(jacob(x), f(v=x))
        tol = norm(tol - x)
    return x


if __name__ == '__main__':

    Niter = 100000
    delta_t = 1e-2
    ROSSLER_MAP = RosslerMap(delta_t=delta_t)
    INIT = np.array([-5.75, -1.6, 0.02])
    traj, t = ROSSLER_MAP.full_traj(Niter, INIT)

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot(traj[:, 0], traj[:, 1], traj[:, 2])

    fix_point = newton(ROSSLER_MAP.v_eq, ROSSLER_MAP.jacobian, INIT)

    error = norm(fix_point - ROSSLER_MAP.equilibrium())
    print("equilibrium state :", fix_point, ", error : ", error)

    lyap = lyapunov_exponent(traj,
                             ROSSLER_MAP.jacobian,
                             max_it=Niter,
Exemplo n.º 6
0
import torch.nn as nn

#Importing training data
df = pd.read_csv('data.csv')

#Generating uniformaly sampled initial values
x_random = np.random.uniform(low=df["x"].min(), high=df["x"].max(), size=5)
y_random = np.random.uniform(low=df["y"].min(), high=df["y"].max(), size=5)
z_random = np.random.uniform(low=df["z"].min(), high=df["z"].max(), size=5)

random_init = np.array((x_random, y_random, z_random))

#Preparing temporal series generator
params = (0.2, 0.2, 0.54)
delta_t = 1e-2
ROSSLER_MAP = RosslerMap(delta_t=delta_t)
n_preds = 3000
window = 50

#Loading models
model_lstm = torch.load("LSTM_new.pth")
model_lstm = model_lstm.float()
model_lstm.eval()

model_temp = torch.load("K_temporal.pth")
model_temp.eval()

LSTM_results = []
TEMP_results = []

Exemplo n.º 7
0
def generate_trajectory(Niter=100000,
                        delta_t=1e-2,
                        x_init=np.array([-5.75, -1.6, 0.02])):
    ROSSLER_MAP = RosslerMap(delta_t=delta_t)
    traj, t = ROSSLER_MAP.full_traj(Niter, x_init)
    return traj, t
            gradients = input_.grad
            J[i] = gradients.data

        return (J - np.eye(3)) / self.delta_t

    def save_traj(self, y, file):
        np.savetxt(file, y)
        #save the trajectory in y.dat file


if __name__ == '__main__':
    delta_t = 1e-2
    ROSSLER = Rossler_model(delta_t)

    y = ROSSLER.full_traj()
    ROSSLER_MAP = RosslerMap(delta_t=delta_t)
    INIT = np.array([-5.75, -1.6, 0.02])
    Niter = ROSSLER.nb_steps

    traj, speeds, jacobians, t = ROSSLER_MAP.full_traj(Niter, INIT)

    # result_loss = np.sum((traj[::100]-y)**2,axis = 1)
    # plt.plot(range(len(result_loss)),result_loss)
    # plt.show()

    y = np.stack(y)
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot(traj[:, 0], traj[:, 1], traj[:, 2], c='b')
    ax.plot(y[:, 0], y[:, 1], y[:, 2], c='r')
    plt.show()
Exemplo n.º 9
0
        A += torch.tensor([[0, 0, 0], [0, 0, 0], [0, 0, 1]]) * (target[0] - c)
        J = (A * delta_t).exp()

        return F.mse_loss(input, target,
                          reduction=self.reduction) + self.lambd * F.mse_loss(
                              J_hat, J, reduction=self.reduction)


if __name__ == '__main__':

    model = nn.Sequential(torch.nn.Linear(3, 100), torch.nn.Linear(100, 100),
                          torch.nn.Tanh(), torch.nn.Linear(100, 100),
                          torch.nn.Tanh(), torch.nn.Linear(100, 3))

    delta_t = 1e-2
    ROSSLER_MAP = RosslerMap(delta_t=delta_t)
    INIT = np.array([-5.75, -1.6, 0.02])

    trajs, t = ROSSLER_MAP.full_traj(100000, INIT)

    optimizer = torch.optim.Adam(model.parameters())

    #criterion = torch.nn.MSELoss()
    #criterion = l1_penalised(RosslerMap.a, RosslerMap.b, RosslerMap.c, lambd=1)

    x = torch.tensor(trajs[:-1])
    y = torch.tensor(trajs[1:])

    training_set = TensorDataset(x[:8000], y[:8000])

    #train(5, 64, criterion, optimizer, model, training_set)
Exemplo n.º 10
0
def main(args):
    wandb_logger = WandbLogger(project=args.project)

    checkpoint_callback = ModelCheckpoint(
        save_top_k=1,
        verbose=True,
        monitor="val_loss",
        mode="min",
    )

    datamodule = RosslerAttractorDataModule(
        n_iter_train=args.n_iter_train,
        n_iter_valid=args.n_iter_valid,
        n_iter_test=args.n_iter_test,
        init_pos_train=args.init_pos_train,
        init_pos_test=args.init_pos_train,
        init_pos_valid=args.init_pos_valid,
        batch_size=args.batch_size,
        delta_t=args.delta_t,
    )

    datamodule.setup()

    criterion = nn.L1Loss(reduction="mean")
    # use_cuda = False if args.gpus is None else True
    # criterion_2 = SoftDTW(use_cuda=use_cuda, gamma=0.1, normalize=True)
    criterion_2 = nn.MSELoss(reduction="mean")

    # checkpoint_path = "Data/checkpoints/model_dtw.ckpt"

    # model = DiscreteModel.load_from_checkpoint(checkpoint_path=checkpoint_path)
    # model.hparams.criterion_2 = criterion_2
    # model.configure_optimizers()
    # model.hparams.lr = args.lr

    model = DiscreteModel(
        criterion=criterion,
        criterion_2=criterion_2,
        lr=args.lr,
        delta_t=args.delta_t,
        mean=datamodule.dataset_train.mean,
        std=datamodule.dataset_train.std,
        hidden_size=15,
    )

    trainer = Trainer(
        gpus=args.gpus,
        logger=wandb_logger,
        max_epochs=args.epochs,
        callbacks=[checkpoint_callback],
        # auto_lr_find=True,
    )

    # trainer.tune(model=model, datamodule=datamodule)

    trainer.fit(model=model, datamodule=datamodule)
    trainer.test(model=model, datamodule=datamodule)

    # Tests

    TRAJECTORY_DUR = 1000
    nb_steps = int(TRAJECTORY_DUR // args.delta_t)

    checkpoint_path = Path(checkpoint_callback.best_model_path)
    save_dir_path = checkpoint_path.parent

    trained_model = DiscreteModel.load_from_checkpoint(
        checkpoint_path=checkpoint_path)
    trained_model.normalize = False

    true_model = RosslerMap(delta_t=args.delta_t)

    statstics_calculator = Statistics(wandb_logger)
    dynamics_calculator = Dynamics(wandb_logger, true_model, trained_model,
                                   nb_steps)

    # TRAIN set
    traj_pred, traj_true, time_list = compute_traj(trained_model, true_model,
                                                   args.init_pos_train,
                                                   nb_steps)
    np.save(os.path.join(save_dir_path, "traj_pred_train.npy"), traj_pred)
    np.save(os.path.join(save_dir_path, "traj_true_train.npy"), traj_true)
    np.save(os.path.join(save_dir_path, "time_list_train.npy"), time_list)

    statstics_calculator.add_traj(traj_true,
                                  traj_pred,
                                  time_list,
                                  prefix="train ")
    statstics_calculator.plot_all()
    dynamics_calculator.add_traj(traj_true, traj_pred)
    dynamics_calculator.plot_all()

    # VAL set
    # traj_pred, traj_true, time_list = compute_traj(
    #     trained_model, true_model, args.init_pos_valid, nb_steps
    # )
    # np.save(os.path.join(save_dir_path, "traj_pred_valid.npy"), traj_pred)
    # np.save(os.path.join(save_dir_path, "traj_true_valid.npy"), traj_true)
    # np.save(os.path.join(save_dir_path, "time_list_valid.npy"), time_list)

    # statstics_calculator.add_traj(traj_true, traj_pred, time_list, prefix="valid ")
    # statstics_calculator.plot_all()
    # dynamics_calculator.add_traj(traj_true, traj_pred)
    # dynamics_calculator.plot_all()

    # TEST set
    traj_pred, traj_true, time_list = compute_traj(trained_model, true_model,
                                                   args.init_pos_test,
                                                   nb_steps)
    np.save(os.path.join(save_dir_path, "traj_pred_test.npy"), traj_pred)
    np.save(os.path.join(save_dir_path, "traj_true_test.npy"), traj_true)
    np.save(os.path.join(save_dir_path, "time_list_test.npy"), time_list)

    statstics_calculator.add_traj(traj_true,
                                  traj_pred,
                                  time_list,
                                  prefix="test ")
    statstics_calculator.plot_all()
    dynamics_calculator.add_traj(traj_true, traj_pred)
    dynamics_calculator.plot_all()