예제 #1
0
파일: numpy_esn.py 프로젝트: nmheim/torsk
    def predict(self, initial_input, initial_state, nr_predictions):
        self.timer.begin("predict")
        
        (T,H) = (nr_predictions,initial_state.shape[0])
        (M,N) = initial_input.shape
        inp   = to_bh(initial_input)
        state = to_bh(initial_state)

        states  = bh.empty((T,H),dtype=state.dtype)
        outputs = bh.empty((T,M,N),dtype=inp.dtype)

        for i in range(T):
            state = self.esn_cell.forward(inp, state)
            S = to_np(state)
            I = to_np(inp).reshape(-1)
            O = to_np(self.ones)
            #            ext_state = bh.concatenate([self.ones, inp.reshape(-1), state], axis=0)
            ext_state = to_bh(np.concatenate([O,I,S], axis=0))
            output = bh_dot(self.wout, ext_state).reshape((M,N))
            
            inp        = output
            outputs[i] = to_bh(output)
            states[i]  = to_bh(state)

        self.timer.end()
        return outputs, states
예제 #2
0
파일: sparse.py 프로젝트: nmheim/torsk
    def __init__(self, values, col_idx, nonzeros_per_row, dense_shape, timer=None):
        assert values.shape == col_idx.shape

        (m,n,n_nz) = (dense_shape[0],dense_shape[1],nonzeros_per_row)

        self.values  = to_bh(values).reshape((m,n_nz))
        self.col_idx = to_bh(col_idx).reshape((m,n_nz))
        self.nonzeros_per_row = nonzeros_per_row
        self.dense_shape = dense_shape
        self.timer = timer
예제 #3
0
    def forward(self, image, state):
        start_timer(self.timer,"forward")
        
        self.check_dtypes(image, state)

        input_stack = self.input_map(to_np(image))       # np
        x_input     = to_bh(bh.concatenate(input_stack)) # np -> bh
        x_state     = self.state_map(to_bh(state))       # bh
        start_timer(self.timer,"tanh")
        new_state   = bh.tanh(x_input+x_state)      # bh
        end_timer(self.timer) # /tanh
        end_timer(self.timer) # /forward
        return new_state
예제 #4
0
파일: numpy_esn.py 프로젝트: nmheim/torsk
    def _forward_states_only(self, inputs, state):
        self.timer.begin("forward_states_only")
        (T, H) = (inputs.shape[0], state.shape[0])

        inputs = to_bh(inputs)
        states = bh.empty((T,H),dtype=state.dtype)
        state  = to_bh(state)

        for i in range(T):
            state = self.esn_cell.forward(inputs[i], state)
            states[i] = to_bh(state)

        self.timer.end()
        return None, states
예제 #5
0
def _pseudo_inverse_lstsq(inputs, states, labels, timer=None):
    timer.begin("pseudo_inverse_lstsq")
    X = _extended_states(inputs, states)

    wout, _, _, s = lstsq(X.T, labels,timer)
    condition = s[0] / s[-1]
    
    wout, s =  to_bh(wout), to_bh(s)

    if(np.log2(np.abs(condition)) > 12):  # More than half of the bits in the data are lost
        logger.warning(
            f"Large condition number in pseudoinverse: {condition}"
            " losing more than half of the digits. Expect numerical blowup!")
        logger.warning(f"Largest and smallest singular values: {s[0]}  {s[-1]}")

    timer.end()
    return wout.T
예제 #6
0
def _pseudo_inverse_svd(inputs, states, labels, timer=None):
    timer.begin("pseudo_inverse_svd")
    X = _extended_states(inputs, states)
    U, s, Vh = svd(X,timer)
    scale = s[0]
    n = len(s[np.abs(s / scale) > 1e-4])  # Ensure condition number less than 10.000
    
    U, s, Vh = to_bh(U), to_bh(s), to_bh(Vh)
    L = labels.T

    v = Vh[:n, :].T
    uh = U[:, :n].T

    wout = bh_dot(bh_dot(L, v) / s[:n], uh)

    timer.end()
    return wout
예제 #7
0
def tikhonov(inputs, states, labels, beta):
    X = to_np(_extended_states(inputs, states))

    Id = np.eye(X.shape[0])
    A = np.dot(X, X.T) + beta + Id
    B = np.dot(X, labels)

    # Solve linear system instead of calculating inverse
    wout = np.linalg.solve(A, B)
    return to_bh(wout.T)
예제 #8
0
파일: numpy_esn.py 프로젝트: nmheim/torsk
    def __init__(self, input_size, hidden_size, spectral_radius,
                 in_weight_init, in_bias_init, density, dtype):

        self.input_size = input_size
        self.hidden_size = hidden_size
        self.dtype = bh.dtype(dtype)

        self.weight_ih = to_bh(np.random.uniform(
            low=-in_weight_init,
            high=in_weight_init,
            size=[hidden_size, input_size])).astype(dtype)

        self.weight_hh = dense_esn_reservoir(
            dim=hidden_size, spectral_radius=spectral_radius,
            density=density, symmetric=False)
        self.weight_hh = self.weight_hh.astype(dtype)

        self.bias_ih = to_bh(np.random.uniform(
            low=-in_bias_init,
            high=in_bias_init,
            size=[hidden_size])).astype(dtype)