<jupyter_start><jupyter_text>import external libraries<jupyter_code>import torch
torch.arange(10)
torch.arange(10, device=
torch.cuda.de
import numpy as np
import scipy.signal
import tensorly as tl
import matplotlib.pyplot as plt

import gc
import time<jupyter_output><empty_output><jupyter_text>import internal libraries<jupyter_code>import sys
sys.path.append(r'/media/rich/Home_Linux_partition/github_repos')

%load_ext autoreload
%autoreload 2
import basic_neural_processing_modules as bnpm
from basic_neural_processing_modules import torch_helpers, h5_handling, timeSeries, similarity, math_functions, misc, decomposition, linear_regression, pickle_helpers, cupy_helpers<jupyter_output><empty_output><jupyter_text>make synthetic latents<jupyter_code>X_dims_fake = [5000, 200077]
dtype = torch.float32

latents = [
          torch.vstack([torch.sin(torch.linspace(0, 140, X_dims_fake[0]))*2,
                        torch.cos(torch.linspace(2,19,X_dims_fake[0]))*2,
                        torch.linspace(0,1,X_dims_fake[0])*2,
                        torch.cos(torch.linspace(0,17,X_dims_fake[0])) >0]).T*1,
          torch.tensor(scipy.signal.savgol_filter(np.random.rand(X_dims_fake[1], 4), 15, 3, axis=0))-0.5,
        ]

for ii, latent in enumerate(latents):
    latents[ii] = latent.type(dtype)<jupyter_output><empty_output><jupyter_text>mix latents into data<jupyter_code>data_mix = torch.einsum('ij,jk->ik', latents[0], latents[1].T).T

print('data_mix size:')
torch_helpers.tensor_sizeOnDisk(data_mix);<jupyter_output>data_mix size:
Device: cpu, Shape: torch.Size([200077, 5000]), Size: 4.00154 GB<jupyter_text>show latents and mixed data<jupyter_code>plt.figure()
plt.plot(latents[0])

plt.figure()
plt.plot(latents[1])

plt.figure()
plt.imshow(data_mix[:1000,:3000], aspect='auto')<jupyter_output><empty_output><jupyter_text>Basic PCA<jupyter_code>DEVICE = torch_helpers.set_device(use_GPU=True, verbose=False)
X = torch.as_tensor(data_mix, dtype=torch.float32, device=DEVICE)
components_raw, scores_raw, singVals, EVR = decomposition.torch_pca(   X,
                                                               device=DEVICE,
                                                               mean_sub=True,
                                                               zscore=False,
                                                               rank=None,
                                                               return_cpu=True,
                                                               return_numpy=False)<jupyter_output><empty_output><jupyter_text>torch_helpers.show_all_tensors(globals())<jupyter_code>torch.cuda.empty_cache()
gc.collect()
torch.cuda.empty_cache()
gc.collect()
torch.cuda.empty_cache()
gc.collect()<jupyter_output><empty_output><jupyter_text>plot PCs<jupyter_code>plt.figure()
plt.plot(components_raw[:,:5] * EVR[:5])

plt.figure()
plt.plot(EVR)<jupyter_output><empty_output><jupyter_text>Incremental PCA<jupyter_code>from torch.utils.data import Dataset, DataLoader
import cuml
DEVICE = torch_helpers.set_device(use_GPU=False)
cupy_helpers.set_device()
dataset = decomposition.ipca_dataset(X, 
                                 mean_sub=True,
                                 zscore=False,
                                 preprocess_sample_method='random',
                                 preprocess_sample_num=10000,
                                 device='cpu',
                                 dtype=torch.float32)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=64, drop_last=True, shuffle=False, num_workers=0, pin_memory=False)<jupyter_output><empty_output><jupyter_text>for ii,val in enumerate(dataset):    print(val[0])    if ii == 0:        break<jupyter_code>cuml_kwargs = {
                "handle": cuml.Handle(),
                "n_components": 20,
                "whiten": False,
                "copy": False,
                "batch_size": None,
                "verbose": True,
                "output_type": None
}

sk_kwargs = {
                "n_components": 20,
                "whiten": False,
                "copy": False,
                "batch_size": None,
}

tic = time.time()
components, EVR, ipca = decomposition.incremental_pca(dataloader,
                                method='cuml',
                                method_kwargs=cuml_kwargs,
                                return_cpu=True)
print(time.time() - tic)
plt.figure()
plt.plot(components[:4].T)<jupyter_output><empty_output><jupyter_text>Compare basic and incremental outputs<jupyter_code>%matplotlib notebook

fig, axs = plt.subplots(5)
for ii in range(len(axs)):
    axs[ii].plot(components[ii])
    axs[ii].plot(components_raw[:,ii])
scores = decomposition.ipca_transform(dataloader, components)
plt.figure()
plt.plot(scores[:1000,0])
plt.plot(scores_raw[:1000,0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11486208316179498, 'fraction_numerical': 0.02202552490736929, 'mean_word_length': 2.6782740348221044, 'pattern_counts': {'":': 11, '<': 44, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '336957', 'license': 'MIT', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1566, 'n_words': 223, 'path': 'demos/demo__incremental_pca.ipynb', 'repo_name': 'akshay-jaggi/basic_neural_processing_modules', 'size': '4858'}