<jupyter_start><jupyter_text>Getting the embeddings> This notebook gets the embeddings (or latent space) from a multivariate time series given by a encoder (e.g., autoencoder)<jupyter_code>from dvats.all import *
from tsai.data.preparation import SlidingWindow
from fastcore.all import *
import wandb
wandb_api = wandb.Api()
from yaml import load, FullLoader<jupyter_output><empty_output><jupyter_text>Config parameters Put here everything that could be needed if this notebook<jupyter_code>config = AttrDict(
    use_wandb = False, # Whether to use or not wandb for experiment tracking
    wandb_group = 'embeddings', # Whether to group this run in a wandb group
    wandb_entity = os.environ['WANDB_ENTITY'], # The entity to use for wandb
    wandb_project = os.environ['WANDB_PROJECT'], # The project to use for wandb
    enc_artifact = 'deepvats/deepvats/mvp:v1', # Name:version of the encoder artifact
    input_ar = None, # If none, the validation set used to train enc_artifact is used
    cpu = False
)<jupyter_output><empty_output><jupyter_text>Run<jupyter_code>run = wandb.init(entity=config.wandb_entity,
                    project=config.wandb_project if config.use_wandb else 'work-nbs', 
                    group=config.wandb_group,
                    job_type='embeddings', 
                    mode='online' if config.use_wandb else 'disabled',
                    anonymous = 'never' if config.use_wandb else 'must',
                    config=config,
                    #id = 'embeddingsProvider',
                    resume='allow')
# Botch to use artifacts offline
artifacts_gettr = run.use_artifact if config.use_wandb else wandb_api.artifact<jupyter_output><empty_output><jupyter_text>Restore the encoder model and its associated configuration<jupyter_code>enc_artifact = artifacts_gettr(config.enc_artifact, type='learner')
# TODO: This only works when you run it two timeS! WTF?
try:
    enc_learner = enc_artifact.to_obj()
except:
    enc_learner = enc_artifact.to_obj()
enc_learner<jupyter_output><empty_output><jupyter_text>Restore the dataset artifact used for training the encoder. Even if we do not compute the dimensionality reduction over this dataset, we need to know the metadata of the encoder training set, to check that it matches with the dataset that we want to reduce.<jupyter_code>enc_run = enc_artifact.logged_by()
enc_artifact_train = artifacts_gettr(enc_run.config['train_artifact'], type='dataset')
enc_artifact_train.name<jupyter_output><empty_output><jupyter_text>Now we specify the dataset artifact that we want to get the embeddings from. If no artifact is defined, the artifact to reduce will be the one used for validate the encoder.<jupyter_code>input_ar_name = ifnone(config.input_ar, 
                       f'{enc_artifact_train.entity}/{enc_artifact_train.project}/{enc_artifact_train.name}')
wandb.config.update({'input_ar': input_ar_name}, allow_val_change=True)
input_ar = artifacts_gettr(input_ar_name)
input_ar.name
df = input_ar.to_df()
df
df.shape
enc_input, _ = SlidingWindow(window_len=enc_run.config['w'], 
                             stride=enc_run.config['stride'], 
                             get_y=[])(df)
enc_input.shape
embs = get_enc_embs(enc_input, enc_learner, cpu=config.cpu, to_numpy=True)
if config.use_wandb: 
    run.log_artifact(ReferenceArtifact(embs, 'embeddings', metadata=dict(run.config)), 
                     aliases=f'run-{run.project}-{run.id}')
run.finish()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08806655192197362, 'fraction_numerical': 0.00028686173264486515, 'mean_word_length': 4.120411160058737, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '270836', 'license': 'Apache-2.0', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1069, 'n_words': 283, 'path': 'nbs_pipeline/03_embeddings.ipynb', 'repo_name': 'vrodriguezf/deepvats', 'size': '3486'}