def rotate2full(self, mat, error=False): """ Rotate a prediction back into full-rank space (i.e. bin space) :param mat: Tensor in low-rank space :param error: Is this a tensor of uncertainties? [default = False] :return full_rank: Full rank prediction """ if self.unitCore is not None: tmp_mat = mat * self.coreStd else: tmp_mat = mat full_rank = tlb.to_numpy( tl.tenalg.multi_mode_dot(tl.tensor(tmp_mat), self.factors, modes=[0, 1], transpose=False)) if self.unitMat is not None: full_rank += self.pixMean if error: full_rank -= self.pixMean return full_rank
def basisCompute(self): """ PCA compression of simulation data. :return pca_basis: Matrix with columns as orthogonal basis vectors :return pca_weights: Coefficients of basis vectors at simulation points """ X = tl.tensor(self.unitMat) self.core, self.factors = tld.partial_tucker( X[:, :, :], modes=[0, 1], tol=self.tol, ranks=[self.user_dim, self.user_dim2]) self.core = tlb.to_numpy(self.core)
def assert_array_equal(a, b, *args, **kwargs): np.testing.assert_array_equal(T.to_numpy(a), T.to_numpy(b), *args, **kwargs)
def _tensor_to_numpy(x): if T.is_tensor(x): x = T.to_numpy(x) return x[0] if x.shape == (1, ) else x return x
for i, pattern in enumerate(patterns): # Generate the original image weight_img = gen_image(region=pattern, image_height=image_height, image_width=image_width) weight_img = T.tensor(weight_img) # Generate the labels y = T.dot(partial_tensor_to_vec(X, skip_begin=1), tensor_to_vec(weight_img)) # Plot the original weights ax = fig.add_subplot(n_rows, n_columns, i * n_columns + 1) ax.imshow(T.to_numpy(weight_img), cmap=plt.cm.OrRd, interpolation='nearest') ax.set_axis_off() if i == 0: ax.set_title('Original\nweights') for j, rank in enumerate(ranks): # Create a tensor Regressor estimator estimator = KruskalRegressor(weight_rank=rank, tol=10e-7, n_iter_max=100, reg_W=1, verbose=0)
def model_inference_category(option): option.folderName = str(input('Please type in the folder name:')) if not os.path.exists('./' + option.folderName): os.makedirs('./' + option.folderName) if option.model_name == '7': y_train_predicted = option.fitted_final.predict(option.X_train) y_test_predicted = option.fitted_final.predict(option.X_test) option.train_residual = y_train_predicted - option.y_train option.test_residual = y_test_predicted - option.y_test option.rmse_train_final = sqrt( mean_squared_error(option.y_train, y_train_predicted)) option.rmse_test_final = sqrt( mean_squared_error(option.y_test, y_test_predicted)) res = sum(np.square(option.y_train - y_train_predicted)) tot = sum(np.square(option.y_train - np.mean(option.y_train))) n = len(option.y_train) p = 1 option.rsquared = 1 - res / tot option.adj_rsquared = 1 - ((res / (n - p - 1)) / (tot / (n - 1))) for i in range(option.y_train.shape[1]): plt.figure(1) plt.title('Residual Plot for Resistance') plt.subplot(221) plt.plot(y_train_predicted[:, i], option.train_residual[:, i], 'ro') plt.xlabel('Predicted') plt.ylabel('Residual') plt.subplot(222) plt.plot(option.train_residual[:, i][0:-2], option.train_residual[:, i][1:-1], 'ro') plt.xlabel('Residual Lag 1') plt.ylabel('Residual') plt.subplot(223) plt.hist(option.train_residual[:, i], bins='auto') plt.xlabel('Residual') plt.ylabel('Frequency') plt.subplot(224) if (0 in np.nditer(np.std(option.train_residual[:, i]))): z = option.train_residual[:, i] else: z = (option.train_residual[:, i] - np.mean(option.train_residual[:, i])) / np.std( option.train_residual[:, i]) stats.probplot(z, dist="norm", plot=plt) plt.xlabel('Standard Quantile') plt.ylabel('Residual Quantile') plt.tight_layout() plt.savefig('./' + option.folderName + '/residual_plot' + str(i) + '.png') plt.close() plt.figure(2) plt.title('Parameter Tuning Plot') plt.scatter(option.candidate_lambda, option.corresponding_rmse_mean) plt.xlabel('Candidate Lambda') if option.model_name not in ['6', '13']: plt.ylabel('Root Mean Squared Error') else: plt.ylabel('Accuracy') plt.savefig('./' + option.folderName + '/parameter_tuning_plot.png') plt.close() elif option.model_name == '8': y_train_predicted = option.fitted_final.evaluate(option.X_train, option.y_train, batch_size=32, verbose=1, sample_weight=None) y_test_predicted = option.fitted_final.evaluate(option.X_test, option.y_test, batch_size=32, verbose=1, sample_weight=None) print() print("Loss = " + str(y_test_predicted[0])) print("Test Accuracy = " + str(y_test_predicted[1])) accuracy = option.fitted_final.history.history['acc'] val_accuracy = option.fitted_final.history.history['val_acc'] loss = option.fitted_final.history.history['loss'] val_loss = option.fitted_final.history.history['val_loss'] epochs = range(len(accuracy)) plt.plot(epochs, accuracy, 'bo', label='Training accuracy') plt.plot(epochs, val_accuracy, 'b', label='Validation accuracy') plt.title('Training and validation accuracy') plt.legend() plt.savefig('./' + option.folderName + '/accuracy_plot.png') plt.close() plt.figure() plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and validation loss') plt.legend() plt.savefig('./' + option.folderName + '/loss_plot.png') plt.close() elif option.model_name == '11': #true regression weight n_rows = 1 n_columns = option.max_rank + 1 fig = plt.figure() ax = fig.add_subplot(n_rows, n_columns, 1) ax.imshow(T.to_numpy(option.weight_img), cmap=plt.cm.OrRd, interpolation='nearest') ax.set_axis_off() ax.set_title('Original\nweights') y_train_predicted = option.fitted_final.predict(option.X_train) for j in range(option.max_rank): #Visualise the learned weights ax = fig.add_subplot(n_rows, n_columns, j + 2) ax.imshow(T.to_numpy(option.fitted_final_weights[j]), cmap=plt.cm.OrRd, interpolation='nearest') ax.set_axis_off() ax.set_title('Learned\nrank = {}'.format(j + 1)) plt.suptitle(option.tensorReg_name) plt.savefig('./' + option.folderName + '/Learned_regression_weights.png') plt.close() elif option.model_name == '12': Z_train_predicted = option.fitted_final.predict( option.X_train)['z_predicted'] Z_test_predicted = option.fitted_final.predict( option.X_test)['z_predicted'] option.Z_train = option.Z_train.T[0] option.Z_test = option.Z_test.T[0] print('Z_test' + str(type(option.Z_test)) + str(option.Z_test)) print('Z_test_predicted' + str(type(Z_test_predicted)) + str(Z_test_predicted)) option.train_residual = Z_train_predicted - option.Z_train option.test_residual = Z_test_predicted - option.Z_test option.rmse_train_final = sqrt( mean_squared_error(option.Z_train, Z_train_predicted)) option.rmse_test_final = sqrt( mean_squared_error(option.Z_test, Z_test_predicted)) res = sum(np.square(option.Z_train - Z_train_predicted)) tot = sum(np.square(option.Z_train - np.mean(option.Z_train))) n = len(option.Z_train) p = len(option.X_train[0]) option.rsquared = 1 - res / tot option.adj_rsquared = 1 - ((res / (n - p - 1)) / (tot / (n - 1))) plt.figure(1) plt.title('Residual Plot for Resistance') plt.subplot(221) plt.plot(Z_train_predicted, option.train_residual, 'ro') plt.xlabel('Predicted') plt.ylabel('Residual') plt.subplot(222) plt.plot(option.train_residual[0:-2], option.train_residual[1:-1], 'ro') plt.xlabel('Residual Lag 1') plt.ylabel('Residual') plt.subplot(223) plt.hist(option.train_residual, bins='auto') plt.xlabel('Residual') plt.ylabel('Frequency') plt.subplot(224) z = (option.train_residual - np.mean(option.train_residual)) / np.std( option.train_residual) stats.probplot(z, dist="norm", plot=plt) plt.xlabel('Standard Quantile') plt.ylabel('Residual Quantile') plt.tight_layout() plt.savefig('./' + option.folderName + '/residual_plot.png') plt.close() plt.figure(2) plt.title('Parameter Tuning Plot') plt.scatter(option.candidate_lambda, option.corresponding_rmse_mean) plt.xlabel('Candidate Lambda') plt.ylabel('Root Mean Squared Error') plt.savefig('./' + option.folderName + '/parameter_tuning_plot.png') plt.close() print("goodness-of-fit: " + str(option.adj_rsquared)) print("training rmse: " + str(option.rmse_train_final)) print("testing rmse: " + str(option.rmse_test_final))
def decomp_plot(edge_len=25, iterations=[1, 2, 3, 4], ranks=[1, 5, 25, 50, 125, 130, 150, 200], decomp='CP'): #Params print(ranks) #Generate random samples rng = check_random_state(7) X = T.tensor(rng.normal(size=(1000, edge_len, edge_len), loc=0, scale=1)) #For plotting n_rows = len(iterations) n_columns = len(ranks) + 1 fig = plt.figure() for i, _ in enumerate(iterations): #Generate tensor weight_img = X[i * edge_len:(i + 1) * edge_len, :, :] ax = fig.add_subplot(n_rows, n_columns, i * n_columns + 1) #Plot image corresponding to 3-D Tensor ax.imshow(T.to_numpy(np.sum(weight_img, axis=0)), cmap=plt.cm.OrRd, interpolation='nearest') ax.set_axis_off() if i == 0: ax.set_title('Original') for j, rank in enumerate(ranks): #Tensor decomposition, image_edge x rank (25x1, 25x5, 25x25 ...) if decomp == 'CP': #CP decomposition components = parafac(weight_img, rank=rank) ax = fig.add_subplot(n_rows, n_columns, i * n_columns + j + 2) # Aggregate the factors for visualization simg = np.sum(components[k] for k in range(len(components))) ax.imshow(T.to_numpy(simg), cmap=plt.cm.OrRd, interpolation='nearest') ax.text(.5, 2.0, '{:.2f}'.format( tensor_distance(kruskal_to_tensor(components), weight_img)), color='r') # ax.set_autoscaley_on(False) ax.set_axis_off() else: #Tucker decomposition components, f = tucker(weight_img, ranks=[3, 25, rank]) #print(components.shape) ax = fig.add_subplot(n_rows, n_columns, i * n_columns + j + 2) # Aggregate the factors for visualization simg = np.sum(components[k] for k in range(len(components))) ax.imshow(T.to_numpy(simg), cmap=plt.cm.OrRd, interpolation='nearest') ax.text(.5, 2.0, '{:.2f}'.format( tensor_distance(kruskal_to_tensor(components), weight_img)), color='r') # ax.set_autoscaley_on(False) ax.set_axis_off() if i == 0: ax.set_title('\n{}'.format(rank)) plt.suptitle('Tensor Decompositions') plt.show()