{ "cells": [ { "cell_type": "markdown", "id": "deb480bd", "metadata": {}, "source": [ "# Random tensors in the TT format\n", "\n", "`torchtt.randn(N, R, var)` returns a random tensor in the tensor-train (TT) format with shape `N` and TT rank `R`.\n", "The entries of the TT cores are independent and normally distributed, and they are scaled so that every entry of the full tensor has mean 0 and variance `var`.\n", "An entry of the full tensor is a sum of products of the core entries, so it is only approximately normally distributed.\n", "\n", "This notebook checks the variance for a few shapes and ranks by computing the full tensors.\n", "\n", "Imports:" ] }, { "cell_type": "code", "execution_count": 1, "id": "d914884d", "metadata": { "execution": { "iopub.execute_input": "2026-09-28T17:48:35.649221Z", "iopub.status.busy": "2026-09-28T17:48:35.649139Z", "iopub.status.idle": "2026-09-28T17:48:36.526101Z", "shell.execute_reply": "2026-09-28T17:48:36.525583Z" } }, "outputs": [], "source": [ "import torch as tn\n", "try:\n", " import torchtt as tntt\n", "except:\n", " print('Installing torchTT...')\n", " %pip install git+https://github.com/ion-g-ion/torchTT\n", " import torchtt as tntt" ] }, { "cell_type": "markdown", "id": "05f3f082", "metadata": {}, "source": [ "## Variance 1\n", "\n", "A 5-dimensional tensor of shape $30 \\times \\cdots \\times 30$ with TT rank $(1,8,16,16,8,1)$:" ] }, { "cell_type": "code", "execution_count": 2, "id": "157fb1f5", "metadata": { "execution": { "iopub.execute_input": "2026-09-28T17:48:36.527280Z", "iopub.status.busy": "2026-09-28T17:48:36.527152Z", "iopub.status.idle": "2026-09-28T17:48:36.568244Z", "shell.execute_reply": "2026-09-28T17:48:36.567668Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Var = 0.9535599014767242 (has to be comparable to 1.0)\n" ] } ], "source": [ "x = tntt.randn([30]*5, [1,8,16,16,8,1])\n", "x_full = x.full()\n", "print('Var = ', tn.std(x_full).numpy()**2, ' (has to be comparable to 1.0)')" ] }, { "cell_type": "markdown", "id": "e1ba5c77", "metadata": {}, "source": [ "## Variance 4\n", "\n", "The same shape and rank with `var = 4.0`:" ] }, { "cell_type": "code", "execution_count": 3, "id": "3b15a332", "metadata": { "execution": { "iopub.execute_input": "2026-09-28T17:48:36.569459Z", "iopub.status.busy": "2026-09-28T17:48:36.569356Z", "iopub.status.idle": "2026-09-28T17:48:36.627696Z", "shell.execute_reply": "2026-09-28T17:48:36.627265Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Var = 5.3232008276129 (has to be comparable to 4.0)\n" ] } ], "source": [ "x = tntt.randn([30]*5, [1,8,16,16,8,1], var = 4.0)\n", "x_full = x.full()\n", "print('Var = ', tn.std(x_full).numpy()**2, ' (has to be comparable to 4.0)')" ] }, { "cell_type": "markdown", "id": "10320b10", "metadata": {}, "source": [ "## Variance 0.01\n", "\n", "The same shape and rank with `var = 0.01`:" ] }, { "cell_type": "code", "execution_count": 4, "id": "afcda2f7", "metadata": { "execution": { "iopub.execute_input": "2026-09-28T17:48:36.629004Z", "iopub.status.busy": "2026-09-28T17:48:36.628907Z", "iopub.status.idle": "2026-09-28T17:48:36.681634Z", "shell.execute_reply": "2026-09-28T17:48:36.681212Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Var = 0.010857722210434208 (has to be comparable to 0.01)\n" ] } ], "source": [ "x = tntt.randn([30]*5, [1,8,16,16,8,1], var = 0.01)\n", "x_full = x.full()\n", "print('Var = ', tn.std(x_full).numpy()**2, ' (has to be comparable to 0.01)')" ] }, { "cell_type": "markdown", "id": "a256e7e4", "metadata": {}, "source": [ "## Longer train\n", "\n", "A 7-dimensional tensor of shape $10 \\times \\cdots \\times 10$ with all inner ranks equal to 4:" ] }, { "cell_type": "code", "execution_count": 5, "id": "ec8fd7b6", "metadata": { "execution": { "iopub.execute_input": "2026-09-28T17:48:36.683327Z", "iopub.status.busy": "2026-09-28T17:48:36.683222Z", "iopub.status.idle": "2026-09-28T17:48:36.716175Z", "shell.execute_reply": "2026-09-28T17:48:36.715670Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Var = 0.7857122980334933 (has to be comparable to 1.0)\n" ] } ], "source": [ "x = tntt.randn([10]*7, [1,4,4,4,4,4,4,1], var = 1.0)\n", "x_full = x.full()\n", "print('Var = ', tn.std(x_full).numpy()**2, ' (has to be comparable to 1.0)')" ] }, { "cell_type": "markdown", "id": "4144610e", "metadata": {}, "source": [ "## Remark\n", "\n", "The variance of the entries equals `var` in expectation, but the empirical variance of a single tensor fluctuates around it.\n", "All entries of a TT tensor are built from the same cores, so they are not independent, and the fluctuations are larger than for a dense tensor with independent entries." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }