<jupyter_start><jupyter_text>IntroductionExperiments using NPs for times series. Use just one time series  Load required packages<jupyter_code>import neural_process_var as nep
import numpy as np
import torch
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Create the training datasets<jupyter_code>x_np = np.arange(-4, 4, 0.1).reshape(-1, 1).astype(np.float32)
y_np = np.sin(x_np)

plt.plot(x_np, y_np)
plt.title('Tasks')
plt.ylabel('y = a sin(bx)')
plt.xlabel('x')
print('Sizes\n', ' x:', x_np.shape, '\n  y:', y_np.shape, '\nfor each task')
plt.show()<jupyter_output>Sizes
  x: (80, 1) 
  y: (80, 1) 
for each task<jupyter_text>Parameter initialization<jupyter_code>r_dim = 2
z_dim = 2
h_specs = [(8, torch.nn.ReLU()), (r_dim, None)]
g_specs = [(8, torch.nn.Sigmoid()), (y_np.shape[1], None)]
h_input_dim = x_np.shape[1] + y_np.shape[1]
g_input_dim = x_np.shape[1] + x_np.shape[0]*z_dim
h = nep.Encoder(h_input_dim, h_specs)
compute_zparams = nep.Zt_params(r_dim, z_dim)
g = nep.Decoder(g_input_dim, g_specs)
optimizer = torch.optim.Adam(params=list(g.parameters()) + list(h.parameters()) + list(compute_zparams.parameters()), lr=1e-3)
x, y = torch.from_numpy(x_np), torch.from_numpy(y_np)
x_t, y_t = x, y
xy_t = torch.cat([x_t, y_t], dim=1)
r = h(xy_t)
mu, D, B = compute_zparams(r)
R = nep.block(D, B)
mean = mu.reshape(z_dim * mu.shape[0], 1)
z = mean + torch.matmul(R, torch.randn_like(mean))
z.shape
xz = torch.cat([x_t, z.squeeze().expand(x_t.shape[0], -1)], dim=1)
a, b = g(xz)
a.squeeze()
epochs = 1000
losses = []
kl = []
nll = []


for epoch in range(epochs):
    optimizer.zero_grad()
    
    r = h(xy_t)
    mu, D, B = compute_zparams(r)
    R = nep.block(D, B)
    mean = mu.reshape(z_dim * mu.shape[0], 1)
    z = mean + torch.matmul(R, torch.randn_like(mean))
    xz = torch.cat([x_t, z.squeeze().expand(x_t.shape[0], -1)], dim=1)
    y_hat, y_var = g(xz)
    y_var = torch.ones_like(y_hat) * y_var
    mean_mc = y_hat.squeeze(-1) 
    std_mc =  y_var.squeeze(-1) 
    NLL = nep.neg_loglik(mean_mc, std_mc, y_t)
    loss = NLL
    losses.append(loss)
    loss.backward()
    optimizer.step()
plt.plot([loss for loss in losses if loss<10000])
for i in range(100):
    z = mean + torch.matmul(R, torch.randn_like(mean))
    xz = torch.cat([x_t, z.squeeze().expand(x_t.shape[0], -1)], dim=1)
    y_hat, y_var = g(xz)
    plt.plot(x_t.numpy(), y_hat.detach().numpy())
    plt.plot(x_t.numpy(), y_t.numpy())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17668269230769232, 'fraction_numerical': 0.02123397435897436, 'mean_word_length': 4.404761904761905, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '181594', 'license': 'Apache-2.0', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1120, 'n_words': 215, 'path': 'Code/Garbage collector/TemporalNeuralProcess.ipynb', 'repo_name': 'pietrolesci/master_thesis', 'size': '2496'}