def run_offline_esn(outdir, hidden_size): outdir = pathlib.Path(outdir) 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) t1 = time.time() torsk.train_esn(model, dataset, outdir=outdir) t2 = time.time() print(f"ESN Training Time: {t2-t1} s")
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)
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
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)
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
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