alpha1_list = [] alpha2_list = [] for i in range(args.num_iteration): print('Iteration: %d' % i) print('alpha 1: %f' % alpha1) print('alpha 2: %f' % alpha2) fit(cnn, trainloader) phi = [] for _, (data) in enumerate(trainloader): features = cnn.flow(data).cpu().detach().numpy() phi.append(features[0, 2]) [a1, alpha, a2, a3, a4] = levinson(phi, nlags=2) alpha1 = alpha[0] alpha2 = alpha[1] alpha1_list.append(alpha1) alpha2_list.append(alpha2) print('\n') #------------------------------------ # plot the loss and alpha parameters t = np.arange(len(error_list)) # loss plt.figure(figsize=[14, 10]) plt.plot(t, error_list, 'r') plt.title('MSE Loss')
# extract latent features from trained network phi = [] for batch_idx, (data) in enumerate(trainloader): data = data.to(device) features = cnn.flow(data).cpu().detach().numpy() # add the first 2 features in the 1st trunk if batch_idx == 0: for i in range(len(features[0]) - 1): phi.append(features[0, i]) # add the last features (predicting y) in every trunk phi.append(features[0, -1]) # train alpha parameter with RLS [_, alpha, _, _, _] = levinson(phi, nlags=args.window_size - 1) alpha1 = alpha[0] alpha2 = alpha[1] alpha1_list.append(alpha1) alpha2_list.append(alpha2) # test MSE_test = 0 for batch_idx, (data) in enumerate(testloader): # pass test data into trained network data = data.to(device) features = cnn.flow(data) # calculate predicted y3 y1 = features[0, 0] y2 = features[0, 1]
data = data[:-2] data = data.to(device) features = cnn.flow(data).cpu().detach().numpy() # add the first 5 features in the 1st trunk if batch_idx == 0: for i in range(len(features[0]) - 1): phi.append(features[0, i]) # add the last features (predicting y) in every trunk phi.append(features[0, -1]) # train alpha parameter with RLS phi_even = phi[0::2] phi_odd = phi[1::2] [_, alpha_even, _, _, _] = levinson(phi_even, nlags=int(args.window_size / 2)) [_, alpha_odd, _, _, _] = levinson(phi_odd, nlags=int(args.window_size / 2)) # convert alpha to tensor alpha_odd = torch.from_numpy(alpha_odd).type(torch.FloatTensor) alpha_even = torch.from_numpy(alpha_even).type(torch.FloatTensor) # test MSE_test = 0 for batch_idx, (data) in enumerate(testloader): # pass test data into trained network data = data.to(device) x = data[-1].cpu().detach().numpy() data = data[:-2] features = cnn.flow(data)