Example #1
0
def run_online_esn(outdir, hidden_size):
    params = esn_params(hidden_size)
    model = ESN(params)
    mackey_train, mackey_eval, mackey_test = mackey_train_eval_test()
    dataset = NumpyImageDataset(mackey_train[:, None, None], params)
    model, outputs, pred_labels = torsk.train_predict_esn(model,
                                                          dataset,
                                                          outdir,
                                                          steps=100,
                                                          step_length=5,
                                                          step_start=0)
Example #2
0
def test_run_1dmackey():
    bh.random.seed(0)

    params = torsk.default_params()
    params.input_map_specs = [{
        "type": "random_weights",
        "size": [1000],
        "input_scale": 1.
    }]
    params.spectral_radius = 1.5
    params.density = 0.05
    params.input_shape = [1, 1]
    params.train_length = 2200
    params.pred_length = 400
    params.transient_length = 200
    params.dtype = "float64"
    params.reservoir_representation = "dense"
    params.backend = "numpy"
    params.train_method = "pinv_svd"
    params.tikhonov_beta = 2.0
    params.debug = False
    params.imed_loss = False

    logger = logging.getLogger(__name__)
    level = "DEBUG" if params.debug else "INFO"
    logging.basicConfig(level=level)
    logging.getLogger("matplotlib").setLevel("INFO")

    model = ESN(params)

    mackey = mackey_sequence(N=3700)
    mackey = normalize(mackey) * 2 - 1
    mackey = mackey[:, bh.newaxis, bh.newaxis]
    dataset = ImageDataset(mackey, params, scale_images=False)

    model, outputs, pred_labels = torsk.train_predict_esn(model, dataset)

    # import matplotlib.pyplot as plt
    # plt.plot(bh.squeeze(outputs))
    # plt.plot(bh.squeeze(pred_labels))
    # plt.show()

    error = bh.abs(outputs - pred_labels)
    logger.info(error.mean())
    logger.info(error.max())
    assert error.mean() < 0.2
    assert error.max() < 1.1
Example #3
0
params.update(sys.argv[1:])

logger.info(params)

if params.backend == "numpy":
    logger.info("Running with NUMPY backend")
    from torsk.data.numpy_dataset import NumpyImageDataset as ImageDataset
    from torsk.models.numpy_esn import NumpyESN as ESN
else:
    logger.info("Running with TORCH backend")
    from torsk.data.torch_dataset import TorchImageDataset as ImageDataset
    from torsk.models.torch_esn import TorchESN as ESN

npypath = pathlib.Path(
    "/home/niklas/erda_save/Ocean/esn/Kuro_SSH_5daymean.npy")
images = np.load(npypath)[:, 90:190, 90:190]
images[images > 10000.] = 0.
images = resample2d_sequence(images, params.input_shape)
dataset = ImageDataset(images, params, scale_images=True)

logger.info("Building model ...")
model = ESN(params)

logger.info("Training + predicting ...")
model, outputs, pred_labels = torsk.train_predict_esn(
    model,
    dataset,
    "/home/niklas/erda_save/kuro_conv_5daymean",
    steps=1000,
    step_length=1)
Example #4
0
    from torsk.models.torch_esn import TorchESN as ESN

logger.info(params)

logger.info("Creating circle dataset ...")
t = np.arange(0, 200 * np.pi, 0.02 * np.pi)
x, y = np.sin(0.3 * t), np.cos(t)

center = np.array([y, x]).T
images = gauss2d_sequence(center, sigma=0.5, size=params.input_shape)
dataset = ImageDataset(images, params, scale_images=True)

logger.info("Building model ...")
model = ESN(params)

logger.info("Training + predicting ...")
model, outputs, pred_labels = torsk.train_predict_esn(model,
                                                      dataset,
                                                      "conv_output",
                                                      steps=1,
                                                      step_length=1,
                                                      step_start=0)

logger.info("Visualizing results ...")
if params.backend == "torch":
    pred_labels = pred_labels.squeeze().numpy()
    outputs = outputs.squeeze().numpy()

anim = animate_double_imshow(pred_labels, outputs)
plt.show()
Example #5
0
    from torsk.models.numpy_esn import NumpyESN as ESN
else:
    logger.info("Running with TORCH backend")
    from torsk.data.torch_dataset import TorchImageDataset as ImageDataset
    from torsk.models.torch_esn import TorchESN as ESN

logger.info(params)

logger.info("Creating circle dataset ...")
t = np.arange(0, 200 * np.pi, 0.02 * np.pi)
x, y = np.sin(0.3 * t), np.cos(t)

center = np.array([y, x]).T
images = gauss2d_sequence(center, sigma=0.5, size=params.input_shape)
dataset = ImageDataset(images, params)

logger.info("Building model ...")
model = ESN(params)

logger.info("Training + predicting ...")
model, outputs, pred_labels = torsk.train_predict_esn(
    model, dataset, outdir="rand_output", steps=1)

logger.info("Visualizing results ...")
if params.backend == "torch":
    pred_labels = pred_labels.squeeze().numpy()
    outputs = outputs.squeeze().numpy()

anim = animate_double_imshow(pred_labels, outputs)
plt.show()
Example #6
0
def test_lissajous(tmpdir):
    np.random.seed(0)

    params = torsk.default_params()
    params.input_map_specs = [{
        "type": "pixels",
        "size": [30, 30],
        "input_scale": 3.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [5, 5],
        "kernel_type": "gauss",
        "input_scale": 4.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [10, 10],
        "kernel_type": "gauss",
        "input_scale": 3.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [15, 15],
        "kernel_type": "gauss",
        "input_scale": 4.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [5, 5],
        "kernel_type": "random",
        "input_scale": 4.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [10, 10],
        "kernel_type": "random",
        "input_scale": 4.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [20, 20],
        "kernel_type": "random",
        "input_scale": 4.
    }, {
        "type": "dct",
        "size": [15, 15],
        "input_scale": 1.
    }, {
        "type": "gradient",
        "input_scale": 4.
    }, {
        "type": "gradient",
        "input_scale": 4.
    }]

    params.spectral_radius = 2.0
    params.density = 0.001
    params.input_shape = [30, 30]
    params.train_length = 1200
    params.pred_length = 300
    params.transient_length = 200
    params.dtype = "float64"
    params.reservoir_representation = "sparse"
    params.backend = "numpy"
    params.train_method = "pinv_svd"
    params.imed_loss = False
    params.tikhonov_beta = None
    params.debug = False

    logger = logging.getLogger(__name__)
    level = "DEBUG" if params.debug else "INFO"
    logging.basicConfig(level=level)
    logging.getLogger("matplotlib").setLevel("INFO")

    logger.info("Creating circle dataset ...")
    t = np.arange(0, 200 * np.pi, 0.02 * np.pi)
    x, y = np.sin(0.3 * t), np.cos(t)

    center = np.array([y, x]).T
    images = gauss2d_sequence(center, sigma=0.5, size=params.input_shape)
    dataset = ImageDataset(images, params, scale_images=True)

    logger.info("Building model ...")
    model = ESN(params)

    logger.info("Training + predicting ...")
    model, outputs, pred_labels = torsk.train_predict_esn(model, dataset)

    # logger.info("Visualizing results ...")
    # import matplotlib.pyplot as plt
    # from torsk.visualize import animate_double_imshow
    # anim = animate_double_imshow(pred_labels, outputs)
    # plt.show()

    error = np.abs(outputs - pred_labels)
    logger.info(error.mean())
    logger.info(error.max())
    assert error.mean() < 3e-4
    assert error.max() < 9e-3
Example #7
0
File: run.py Project: nmheim/torsk
if params.backend == "numpy":
    logger.info("Running with NUMPY backend")
    from torsk.data.numpy_dataset import NumpyImageDataset as ImageDataset
    from torsk.models.numpy_esn import NumpyESN as ESN
else:
    logger.info("Running with TORCH backend")
    from torsk.data.torch_dataset import TorchImageDataset as ImageDataset
    from torsk.models.torch_esn import TorchESN as ESN

logger.info("Building model ...")
model = ESN(params)

mackey, _ = mackey_anomaly_sequence(
    N=3700,
    anomaly_start=params.anomaly_start,
    anomaly_step=params.anomaly_step)
mackey = normalize(mackey) * 2 - 1
mackey = mackey[:, np.newaxis, np.newaxis]
dataset = ImageDataset(mackey, params, scale_images=False)

logger.info("Training + predicting ...")
model, outputs, pred_labels = torsk.train_predict_esn(
    model, dataset, "mackey_anomaly_output",
    steps=1, step_length=2)

plt.plot(pred_labels[:, 0, 0], label="Prediction")
plt.plot(outputs[:, 0, 0], label="Truth")
plt.legend()
plt.show()
Example #8
0
if params.backend == "numpy":
    logger.info("Running with NUMPY backend")
    from torsk.data.numpy_dataset import NumpyImageDataset as ImageDataset
    from torsk.models.numpy_esn import NumpyESN as ESN
else:
    logger.info("Running with TORCH backend")
    from torsk.data.torch_dataset import TorchImageDataset as ImageDataset
    from torsk.models.torch_esn import TorchESN as ESN

npzpath = pathlib.Path(
    "/mnt/data/torsk_experiments/aguhlas_SSH_3daymean_x1050:1550_y700:1000.npz"
)
images = np.load(npzpath)["SSH"][:]
images = resample2d_sequence(images, params.input_shape)
dataset = ImageDataset(images, params, scale_images=True)

prefix = "idx0"
if outdir.joinpath(f"{prefix}-model.pkl").exists():
    logger.info("Restoring model ...")
    model = torsk.load_model(outdir, prefix=prefix)
else:
    logger.info("Building model ...")
    model = ESN(params)

logger.info("Training + predicting ...")
model, outputs, pred_labels = torsk.train_predict_esn(model,
                                                      dataset,
                                                      outdir,
                                                      steps=1000,
                                                      step_length=1)
Example #9
0
if params.backend == "numpy":
    logger.info("Running with NUMPY backend")
    from torsk.data.numpy_dataset import NumpyImageDataset as ImageDataset
    from torsk.models.numpy_esn import NumpyESN as ESN
else:
    logger.info("Running with TORCH backend")
    from torsk.data.torch_dataset import TorchImageDataset as ImageDataset
    from torsk.models.torch_esn import TorchESN as ESN


npzpath = pathlib.Path(input_root+input_file)
images = np.load(npzpath)["SSH"][:]
images = resample2d_sequence(images[:,:,:], params.input_shape)
dataset = ImageDataset(images, params, scale_images=True)

prefix = "idx0"
model_path = outdir.joinpath(f"{prefix}-model.pkl")
print(model_path)
if model_path.exists():
    logger.info("Restoring model ...")
    model = torsk.load_model(outdir, prefix=prefix)
else:
    logger.info("Building model ...")
    model = ESN(params)

logger.info("Training + predicting ...")
model, outputs, pred_labels = torsk.train_predict_esn(
    model, dataset, outdir,
    steps=5*122, step_length=1,step_start=0)
Example #10
0
def test_kuro():
    np.random.seed(0)

    params = torsk.default_params()
    params.input_map_specs = [{
        "type": "pixels",
        "size": [30, 30],
        "input_scale": 3.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [5, 5],
        "kernel_type": "gauss",
        "input_scale": 2.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [10, 10],
        "kernel_type": "gauss",
        "input_scale": 1.5
    }, {
        "type": "conv",
        "mode": "same",
        "size": [15, 15],
        "kernel_type": "gauss",
        "input_scale": 1.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [5, 5],
        "kernel_type": "random",
        "input_scale": 1.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [10, 10],
        "kernel_type": "random",
        "input_scale": 1.
    }, {
        "type": "conv",
        "mode": "same",
        "size": [20, 20],
        "kernel_type": "random",
        "input_scale": 1.
    }, {
        "type": "dct",
        "size": [15, 15],
        "input_scale": 1.
    }, {
        "type": "gradient",
        "input_scale": 1.
    }, {
        "type": "gradient",
        "input_scale": 1.
    }]

    params.spectral_radius = 1.5
    params.density = 0.01
    params.input_shape = [30, 30]
    params.train_length = 12 * 73
    params.pred_length = 73
    params.transient_length = 3 * 73
    params.dtype = "float64"
    params.reservoir_representation = "sparse"
    params.backend = "numpy"
    params.train_method = "pinv_lstsq"
    params.tikhonov_beta = 3e1
    params.debug = False
    params.imed_loss = True
    params.imed_sigma = 1.0

    logger = logging.getLogger(__name__)
    level = "DEBUG" if params.debug else "INFO"
    logging.basicConfig(level=level)
    logging.getLogger("matplotlib").setLevel("INFO")

    images = np.load(pathlib.Path(__file__).parent / "kuro_test_sequence.npy")
    dataset = ImageDataset(images, params, scale_images=True)

    logger.info("Building model ...")
    model = ESN(params)

    logger.info("Training + predicting ...")
    model, outputs, pred_labels = torsk.train_predict_esn(model, dataset)

    # logger.info("Visualizing results ...")
    # import matplotlib.pyplot as plt
    # from torsk.visualize import animate_double_imshow
    # anim = animate_double_imshow(pred_labels, outputs)
    # plt.show()

    error = np.abs(outputs - pred_labels)
    logger.info(error.mean())
    logger.info(error.max())
    assert error.mean() < 0.2
    assert error.max() < 1.3