<jupyter_start><jupyter_text>Training tensor networks to approximate the distribution given by a datasetSimple notebook demonstrating how to train a tensor network to learn the distribution of a dataset. First import the dependencies and a tensor network model.<jupyter_code>import numpy as np
import pickle
from tensornetworks.PositiveMPS import PositiveMPS
from tensornetworks.RealBorn import RealBorn
from tensornetworks.ComplexBorn import ComplexBorn
from tensornetworks.RealLPS import RealLPS
from tensornetworks.ComplexLPS import ComplexLPS<jupyter_output><empty_output><jupyter_text>Now load a dataset. A dataset needs to be an integer numpy array, with each row corresponding to a training example and each column to a categorical variable taking values in 0 to d-1.<jupyter_code>with open('datasets/lymphography', 'rb') as f:
        a=pickle.load(f)
X=a[0]
X=X.astype(int)<jupyter_output><empty_output><jupyter_text>Create a tensor network model (here we use a real Born machine) with bond dimension 2. Specify a learning rate, size of batch and number of epochs of training. Use 'verbose=True' to print the details during training.<jupyter_code>mps = RealBorn(D=2, learning_rate=0.2, batch_size=20, n_iter=30, verbose=True)<jupyter_output><empty_output><jupyter_text>Fit the model to the data<jupyter_code>mps.fit(X)<jupyter_output>Iteration 1, likelihood = 26.820, time = 0.24s
Iteration 2, likelihood = 21.811, time = 0.23s
Iteration 3, likelihood = 19.467, time = 0.27s
Iteration 4, likelihood = 18.320, time = 0.25s
Iteration 5, likelihood = 17.542, time = 0.23s
Iteration 6, likelihood = 17.621, time = 0.20s
Iteration 7, likelihood = 17.561, time = 0.21s
Iteration 8, likelihood = 17.001, time = 0.21s
Iteration 9, likelihood = 16.481, time = 0.21s
Iteration 10, likelihood = 16.209, time = 0.23s
Iteration 11, likelihood = 15.964, time = 0.21s
Iteration 12, likelihood = 15.826, time = 0.22s
Iteration 13, likelihood = 15.704, time = 0.21s
Iteration 14, likelihood = 15.628, time = 0.19s
Iteration 15, likelihood = 15.411, time = 0.19s
Iteration 16, likelihood = 15.338, time = 0.18s
Iteration 17, likelihood = 15.296, time = 0.23s
Iteration 18, likelihood = 15.252, time = 0.18s
Iteration 19, likelihood = 15.222, time = 0.21s
Iteration 20, likelihood = 15.209, time = 0.24s
Iteration 21, likelihood = 15.149, time = 0.23s
I[...]<jupyter_text>Finally evaluate the negative log-likelihood of the fitted model.<jupyter_code>mps.likelihood(X)<jupyter_output><empty_output><jupyter_text>Now create a tensor network model which is a complex LPS with bond dimension 2 and purification dimension of 2.<jupyter_code>mps2 = ComplexLPS(D=2, learning_rate=0.5, batch_size=20, n_iter=30, verbose=True, mu=2)<jupyter_output><empty_output><jupyter_text>Fit the model to the data<jupyter_code>mps2.fit(X)<jupyter_output>Iteration 1, likelihood = 30.569, time = 0.24s
Iteration 2, likelihood = 25.780, time = 0.42s
Iteration 3, likelihood = 22.945, time = 0.44s
Iteration 4, likelihood = 21.134, time = 0.37s
Iteration 5, likelihood = 19.839, time = 0.39s
Iteration 6, likelihood = 18.927, time = 0.47s
Iteration 7, likelihood = 18.204, time = 0.43s
Iteration 8, likelihood = 17.725, time = 0.41s
Iteration 9, likelihood = 17.249, time = 0.39s
Iteration 10, likelihood = 16.887, time = 0.45s
Iteration 11, likelihood = 16.615, time = 0.45s
Iteration 12, likelihood = 16.409, time = 0.41s
Iteration 13, likelihood = 16.209, time = 0.55s
Iteration 14, likelihood = 16.068, time = 0.45s
Iteration 15, likelihood = 15.951, time = 0.67s
Iteration 16, likelihood = 15.867, time = 0.52s
Iteration 17, likelihood = 15.798, time = 0.52s
Iteration 18, likelihood = 15.719, time = 0.58s
Iteration 19, likelihood = 15.627, time = 0.49s
Iteration 20, likelihood = 15.558, time = 0.56s
Iteration 21, likelihood = 15.512, time = 0.59s
I[...]<jupyter_text>Finally evaluate the negative log-likelihood of the fitted model.<jupyter_code>mps2.likelihood(X)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10302267002518892, 'fraction_numerical': 0.10730478589420656, 'mean_word_length': 5.062595419847328, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '361751', 'license': 'MIT', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1342, 'n_words': 444, 'path': 'fitdataset.ipynb', 'repo_name': 'postylem/tensor_network_project', 'size': '3970'}