dabench.model ============= .. py:module:: dabench.model .. autoapi-nested-parse:: Model classes Classes ------- .. autoapisummary:: dabench.model.Model dabench.model.RCModel Package Contents ---------------- .. py:class:: Model(system_dim = None, delta_t = None, model_obj = None) Base for Model objects :param system_dim: system dimension :param delta_t: the timestep of the model (assumed uniform) :param model_obj: underlying model object, e.g. pytorch neural network. .. py:class:: RCModel(system_dim, input_dim, reservoir_dim=512, sparsity=0.99, sparse_adj_matrix=False, sigma=0.5, sigma_bias=0, leak_rate=1.0, spectral_radius=0.9, tikhonov_parameter=0, readout_method='linear', random_seed=1, **kwargs) A simple Reservoir Computing data-driven model :param system_dim: Dimension of reservoir output. :type system_dim: int :param input_dim: Dimension of reservoir input signal. :type input_dim: int :param reservoir_dim: Dimension of reservoir state. Default: 512. :type reservoir_dim: int :param sparsity: the percentage of zero-valued entries in the adjacency matrix (A). Default: 0.99. :type sparsity: float :param sparse_adj_matrix: If True, A is computed using scipy sparse. :type sparse_adj_matrix: bool :param sigma: Scaling of the input weight matrix. Default: 0.5. :type sigma: float :param sigma_bias: Bias term for sigma. Default: 0. :type sigma_bias: float :param leak_rate: ``(1-leak_rate)`` of the reservoir state at the previous time step is incorporated during timestep update. Default: 1. :type leak_rate: float :param spectral_radius: Scaling of the reservoir adjacency matrix. Default: 0.9. :type spectral_radius: float :param tikhonov_parameter: Regularization parameter in the linear solving, penalizing amplitude of weight matrix elements. Default: 0.0. :type tikhonov_parameter: float :param readout_method: How to handle reservoir state elements during readout. One of 'linear', 'biased', or 'quadratic'. Default: 'linear'. :type readout_method: str :param random_seed: Random seed for random number generation. Default is 1. :type random_seed: int :param s: Model states over entire time series. :type s: ndarray :param s_last: Last :type s_last: ndarray :param ybar: y.T @ st, set in _compute_Wout. :type ybar: ndarray :param sbar: st.T @ st, set in _compute_Wout. :type sbar: ndarray :param A: reservoir adjacency weight matrix, set in ``.weights_init()``. :type A: ndarray :param Win: reservoir input weight matrix, set in ``.weights_init()``. :type Win: ndarray :param Wout: trained output weight matrix, set in ``.train()``. :type Wout: ndarray .. py:method:: generate(state_vec, A=None, Win=None, r0=None) generate reservoir time series from input signal u :param u: (time_dimension, system_dimension), input signal to reservoir :type u: array_like :param A: (reservoir_dim, reservoir_dim), reservoir adjacency matrix :type A: array_like, optional :param Win: (reservoir_dim, system_dimension), reservoir input weight matrix :type Win: array_like, optional :param r0: (reservoir_dim,) initial reservoir state :type r0: array_like, optional :param save_states: If True, saves reservoir states as self.states. If False, returns states. Default: False. :type save_states: bool :returns: Reservoirs state, size (time_dim, reservoir_dim) .. py:method:: load_weights(pkl_path) Load RC reservoir weights from pkl file. :param pkl_path: Filepath with save weight matrix. :type pkl_path: str .. py:method:: predict(state_vec, delta_t, initial_index=0, n_steps=100, spinup_steps=0, r0=None, keep_spinup=True) Compute the prediction phase of the RC :param dataobj: data object containing the initial conditions :type dataobj: Data :param initial_index: time index of initial conditions in the data object 'values' :type initial_index: int, optional :param n_steps: number of steps to conduct the prediction :type n_steps: int, optional :param spinup_steps: number of steps before the initial_index to use for spinning up the reservoir state :type spinup_steps: int, optional :param r0: initial reservoir state :type r0: array_like, optional :returns: Data object covering prediction period .. py:method:: readout(rt, Wout=None, utm1=None) use Wout to map reservoir state to output :param rt: 1D or 2D with dims: (Nr,) or (Ntime, Nr) reservoir state, either passed as single time snapshot, or as matrix, with reservoir dimension as last index :type rt: array_like :param utm1: 1D or 2D with dims: (Nu,) or (Ntime, Nu) u(t-1) for r(t), only used if readout_method = 'biased', then Wout*[1, u(t-1), r(t)]=u(t) :type utm1: array_like :returns: 1D or 2D array with dims(Nout,) or (Ntime, Nout) depending on shape of input array .. todo:: generalize similar to DiffRC .. py:method:: save_weights(pkl_path) Save RC reservoir weights as pkl file. :param pkl_path: Filepath for saving with .pkl extension :type pkl_path: str .. py:method:: train(data_obj, update_Wout=True) Train the localized RC model :param dataobj: Data object containing training data :type dataobj: Data :param update_Wout: if True, update Wout, otherwise initialize it by rewriting the ybar and sbar matrices :type update_Wout: bool Sets Attributes: Wout (array_like): Trained output weight matrix .. py:method:: update(r, u, A=None, Win=None) Update reservoir state with input signal and previous state :param r: (reservoir_dim,) Previous reservoir state :type r: array_like :param u: (input_dimension,) input signal :type u: array_like :param A: (reservoir_dim, reservoir_dim), reservoir adjacency matrix. If None, uses self.A. Default is None. :type A: array_like, optional :param Win: (reservoir_dim, input_dimension), reservoir input weight matrix. If None, uses self.Win. Default is None :type Win: array_like, optional :returns: Reservoir state at next time step, of size (reservoir_dim,) .. py:method:: weights_init() Initialize the weight matrices .. rubric:: Notes Generate the random adjacency (A) and input weight matrices (Win) with sparsity determined by ``sparsity`` attribute, scaled by ``spectral_radius`` and ``sigma`` parameters, respectively. If sparse_adj_matrix is True, then use scipy sparse matrices for computational speed up. Sets Attributes: A (array_like): (reservoir_dim, reservoir_dim), reservoir adjacency matrix Win (array_like): (reservoir_dim, input_dim), reservoir input weight matrix Adense (array_like): stores dense version of A if A is specified as sparse format.