<jupyter_start><jupyter_text>Some prelimary code to visualize tensor-tensor products, the t-SVD, and projections.<jupyter_code># import modules
import numpy as np
from data.mnist import load_tensor, init
import tensor.tensor_product_wrapper as tp
from utils.plotting_utils import montage_array, slice_subplots
import matplotlib.pyplot as plt
import similarity_metrics as sm

# init()  # download mnist data (only do this once); will be stored in tensor-fmri directory; takes some time to download<jupyter_output><empty_output><jupyter_text>Choose an available tensor-tensor product.  Options for this notebook include:* 'f' : facewise product (no transform along third dimension)* 't' : t-product (fast Fourier transform along third dimension)* 'c': c-product (discrete cosine transform along third dimension)<jupyter_code># choose product type {'f', 't', 'c'}
prod_type = 'f'<jupyter_output><empty_output><jupyter_text>Given a tensor-tensor product $\star_M$, the projection of a tensor $\mathcal{A}$onto the space spanned by the lateral slices of a tensor $\mathcal{U}$ is$$\mathcal{U} \star_M \mathcal{U}^\top \star_M \mathcal{A}$$<jupyter_code>def projection(A, U, prod_type):
    training_coeff = tp.ten_prod(tp.ten_tran(U, prod_type=prod_type), A, prod_type=prod_type)
    return tp.ten_prod(U, training_coeff, prod_type=prod_type)<jupyter_output><empty_output><jupyter_text>The [MNIST Dataset](http://yann.lecun.com/exdb/mnist/) is a set of handwritten digits from $0$ to $9$.These are $28\times 28$ grayscale images and historically have been the starting point formost image classification tasks.<jupyter_code># for reproducibility
np.random.seed(20)

# load data
num_classes = 4
training_data, training_labels, test_data, test_labels = load_tensor(num_per_class_train=100, num_per_class_test=9,
                                                                     num_classes=num_classes, path='')

# permute such that lateral slices are the images
training_data = training_data.transpose(0, 2, 1)
test_data = test_data.transpose(0, 2, 1)

# visualize data
for i in range(num_classes):
    slice_subplots(test_data[:, test_labels == i, :], axis=1, title='class ' + str(i))
    plt.show()

print(test_data.shape)
print(test_labels)
# form local t-svd
num_classes = len(np.unique(training_labels))
k = 2

U = []
for i in range(num_classes):
    u, _, _, _ = tp.ten_svd(training_data[:, training_labels == i, :], k, prod_type=prod_type)
    U.append(u)

# visualizations
for i in range(num_classes):
    slice_subplots(U[i], axis=1, title='basis elments for class ' + str(i))
    plt.show()

# compute results on training and test data
training_error = np.zeros([num_classes, training_data.shape[1]])
test_error = np.zeros([num_classes, test_data.shape[1]])
for i in range(num_classes):
    training_projection = projection(training_data, U[i], prod_type=prod_type)
    training_error[i, :] = sm.frobenius_metric(training_data, training_projection, axis=1)

    test_projection = projection(test_data, U[i], prod_type=prod_type)
    test_error[i, :] = sm.frobenius_metric(test_data, test_projection, axis=1)


# classification
training_predicted_classes = np.argmin(training_error, axis=0).reshape(-1)
test_predicted_classes = np.argmin(test_error, axis=0).reshape(-1)

# results
training_num_correct = np.sum(training_predicted_classes == training_labels)
training_accuracy = training_num_correct / training_data.shape[1]

test_num_correct = np.sum(test_predicted_classes == test_labels)
test_accuracy = test_num_correct / test_data.shape[1]

print('train accuracy = %0.2f' % (100 * training_accuracy))
print('test accuracy = %0.2f' % (100 * test_accuracy))

# plot results
plt.figure()
for i in range(num_classes):
    plt.semilogy(training_error[i, :], 'o', label=i)

plt.xlabel('image index')
plt.ylabel('distance score (lower is better)')
plt.legend()
plt.show()
# visualizations of projected tensors
for j in range(num_classes):
    for i in range(num_classes):
        A = projection(training_data[:, training_labels == j, :], U[i], prod_type=prod_type)
        slice_subplots(A[:, :4, :], axis=1, title='projection of class ' + str(j) + ' onto basis for class ' + str(i))
        plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1192443919716647, 'fraction_numerical': 0.01038961038961039, 'mean_word_length': 5.350824587706147, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '642041', 'license': 'MIT', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1403, 'n_words': 356, 'path': 'notebooks/TensorFMRI_MNISTNotebook.ipynb', 'repo_name': 'elizabethnewman/tensor-fmri', 'size': '4235'}