Exemplo n.º 1
0
 def update_c_est(self, scale=1, regress=False):
     if regress:
         #For regression, shape M to (TxD), for each episode (so S x (TxD))
         #(reshape by going through each muscle and then to next)
         M_flat = np.reshape(self.M_stacked, (self.S, self.T * self.D))
         #Each column of predictor is one synergy, shape (TxD)
         #predictor has N columns
         W_flat = np.reshape(self.W_est_stacked,
                             (self.N, self.T * self.D)).T
         #loop through and do a regression for each episode
         for s in range(self.S):
             self.c_est[s, :], _ = scipy.optimize.nnls(W_flat, M_flat[s, :])
     else:
         mult_factor = np.zeros_like(self.c_est)
         for s in range(self.S):
             for i in range(self.N):
                 Theta_i_tis = self.Theta[
                     i,
                     util.t_shift_to_index(self.delays[s, i], self.T), :, :]
                 num = np.trace((self.M_stacked[s, :, :].T).dot(
                     self.W_est_spread).dot(Theta_i_tis))
                 denom = np.trace((self.H_stacked[s, :, :].T).dot(
                     self.W_est_spread.T).dot(
                         self.W_est_spread).dot(Theta_i_tis))
                 mult_factor[s, i] = num / denom
         self.c_est = self.c_est * scale * mult_factor
Exemplo n.º 2
0
 def update_H(self):
     H = np.zeros((self.S, self.N * self.T, 2 * self.T))
     for s in range(self.S):
         H[s,:,:] = np.sum(self.c_est[s,:][:,None,None]*\
             np.array([self.Theta[i,util.t_shift_to_index(
             self.delays[s,i], self.T),:,:] for i in range(self.N)]),axis=0)
     self.H_stacked = H
     self.H_spread = np.concatenate(H, axis=1)
Exemplo n.º 3
0
def construct_Theta(N, T):
    Theta = np.zeros(
        (N, 2 * T - 1, N * T, T))  #shape of each Theta_i(t) is N*T x T
    for i in range(1, N + 1):
        for t in range(1 - T, T):
            rows, columns = np.indices((N * T, T))
            to_fill = (rows + 1 - (i - 1) * T) == (columns + 1 - t)
            to_fill[0:(i - 1) * T, :] = 0.
            to_fill[i * T:, :] = 0.
            Theta[i - 1, util.t_shift_to_index(t, T), :, :] = to_fill
    return Theta
Exemplo n.º 4
0
def multiplicative_update_c(c_est, M, W_est, Theta, H, delays, scale=1):
    _, _, _, T = np.shape(Theta)
    S, N = np.shape(c_est)
    M = util.stack(M, T)
    # plt.figure(9)
    # plt.imshow(H)
    # print(np.shape(H),(N*T,T*S))
    H = util.stack(H, T)
    # print(np.shape(H),(S,N*T,T))
    # plt.figure(10)
    # plt.imshow(H[0,:,:])
    # plt.show()
    mult_factor = np.zeros_like(c_est)
    for s in range(S):
        for i in range(N):
            # print(delays[s,i])
            Theta_i_tis = Theta[i,
                                util.t_shift_to_index(delays[s, i], T), :, :]
            # print(np.sum(Theta_i_tis))
            # raw_input(' ')
            num = np.trace((M[s, :, :].T).dot(W_est).dot(Theta_i_tis))
            denom = np.trace(
                (H[s, :, :].T).dot(W_est.T).dot(W_est).dot(Theta_i_tis))
            # if denom==0:
            #     print(s,i)
            #     print('inf denom c update')
            #     plt.figure(55)
            #     plt.subplot(2,1,1)
            #     plt.imshow((H[s,:,:].T).dot(W_est.T).dot(W_est).dot(Theta_i_tis),
            #     interpolation='none')
            #     plt.subplot(2,1,2)
            #     plt.imshow((H[s,:,:]),interpolation='none')
            #     plt.show()
            #     raw_input(' ')
            mult_factor[s, i] = num / denom
    # print(mult_factor)
    c_est = c_est * scale * mult_factor
    return c_est
Exemplo n.º 5
0
                cmap='Greys_r',
                vmin=0,
                vmax=amp_max)
pltuls.strip_ticks(ax)
plt.text(0.65, 0.95, 'True M', transform=plt.gcf().transFigure)

Theta = np.zeros(
    (N, 2 * T - 1, N * T, T))  #shape of each Theta_i(t) is N*T x T

for i in range(1, N + 1):
    for t in range(1 - T, T):
        rows, columns = np.indices((N * T, T))
        to_fill = (rows + 1 - (i - 1) * T) == (columns + 1 - t)
        to_fill[0:(i - 1) * T, :] = 0.
        to_fill[i * T:, :] = 0.
        Theta[i - 1, util.t_shift_to_index(t, T), :, :] = to_fill
        # plt.figure(544)
        # plt.imshow(Theta[i-1,util.t_shift_to_index(t,T),:,:],interpolation='none')
        # raw_input(' ')

# util.test_Theta(Theta)

# error = np.inf

unexp_var_threshold = 1e-5
# R2_diff_threshold = 1e-7
# R2 = np.zeros(2)
SS_tot = substeps.compute_total_sum_squares(M)
print(SS_tot)
SS_res = substeps.compute_squared_error(
    W_est, c_est, np.zeros_like(c_est),