def supervised_separation(self, input_data, f, iter=50, adaptive_alpha=0, additional_basis=0): self.f=f self.time_vec=input_data[:,0:1] self.adaptive_alpha=adaptive_alpha self.additional_basis=additional_basis input_data=input_data[:,1:].T baseline=input_data.min() input_data=input_data-baseline # Modify the input data based on the feature length data=pcnmf(feature_length=self.feature_length).matrix_conv(input_data) # Check the learning rate (adaptive_alpha) for each source (adaptive NMF) if isinstance(adaptive_alpha, int) or isinstance(adaptive_alpha, float): adaptive_alpha = np.ones(self.source_num)*adaptive_alpha else: if len(adaptive_alpha) != self.source_num: print("Error: The model has " +str(self.source_num) +" sources. Please specify adaptive_alpha for every source") return # Add additional basis for feature learning (semi-supervised NMF) if additional_basis>0: self.W_cluster=np.append(self.W_cluster, np.ones(additional_basis)*self.source_num) self.source_num=self.source_num+1 self.basis_num=self.basis_num+additional_basis adaptive_alpha = np.append(adaptive_alpha, 1) # supervised NMF W, H = NMF_init(data, self.basis_num, init='random') W[:, 0:self.basis_num-additional_basis] = self.W violation=0 Ht=H.T Ht = check_array(H.T, order='C') X = check_array(data, accept_sparse='csr') W = np.array(W) for run in range(iter): prev_W = np.array(W) # update H violation += basis_update(X.T, W=Ht, Ht=W, l1_reg=0, l2_reg=0, shuffle=False, random_state=None) # update W violation += basis_update(X, W=W, Ht=Ht, l1_reg=0, l2_reg=0, shuffle=False, random_state=None) for i in range(self.source_num): index = np.where(self.W_cluster == i) if adaptive_alpha[i] == 0: W[:,index] = prev_W[:,index] else: W[:,index] = prev_W[:,index]*(1-adaptive_alpha[i]) + W[:,index]*adaptive_alpha[i] self.W=W self.H=Ht.T self.nmf_output(input_data, self.time_vec, baseline)
def supervised_separation(self, input_data, f, iter=50): self.f = f self.time_vec = input_data[:, 0:1] input_data = input_data[:, 1:].T baseline = input_data.min() input_data = input_data - baseline # Modify the input data based the feature width print('Supervised NMF') data = self.matrix_conv(input_data) # supervised NMF W, H = NMF_init(data, self.basis_num, init='random') violation = 0 Ht = H.T Ht = check_array(H.T, order='C') X = check_array(data, accept_sparse='csr') print('Learning temporal activitations...') for run in range(iter): W = self.W violation += basis_update(X.T, W=Ht, Ht=W, l1_reg=0, l2_reg=0, shuffle=False, random_state=None) self.H = Ht.T self.pcnmf_output(input_data, self.time_vec, baseline) self.time_vec = self.time_vec[:, 0]