<jupyter_start><jupyter_text>Ring2D  1. TrainingTo contine the analysis below, you will have to train the neuralCT mode on Ring2D distribution. This can be done by the line of command:```python ./variationMain.py -source 0```Also, we have a trained model saving in the **demo** folder. So, in the following, we're going to assume using this saving. However, you can change this by change the **rootFolder** variable.<jupyter_code>import numpy as np
import torch
from torch import nn
import flow
import source
import train
import utils
import math
import h5py

# Set gobal variables.

rootFolder = "./demo/variationModel_source_0_Shift_False_T_300_depthLevel_1_l8_M2_H128/"
device = torch.device("cpu")
dtype = torch.float<jupyter_output>/Users/lili/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters<jupyter_text>2. Read from saving<jupyter_code># Load paremeters.

with h5py.File(rootFolder+"/parameter.hdf5","r") as f:
    numFlow = int(np.array(f["numFlow"]))
    lossPlotStep = int(np.array(f["lossPlotStep"]))
    hidden = int(np.array(f["hidden"]))
    nlayers = int(np.array(f["nlayers"]))
    nmlp = int(np.array(f["nmlp"]))
    lr = int(np.array(f["lr"]))
    batchSize = int(np.array(f["batchSize"]))
    epochs = int(np.array(f["epochs"]))
    K = int(np.array(f["K"]))

# Build the target.

target = source.Ring2d().to(device).to(dtype)
n = target.nvars[0]//2

# Rebuild the model.

def innerBuilder(num):
    maskList = []
    for i in range(nlayers):
        if i %2==0:
            b = torch.zeros(num)
            i = torch.randperm(b.numel()).narrow(0, 0, b.numel() // 2)
            b.zero_()[i] = 1
            b=b.reshape(1,num)
        else:
            b = 1-b
        maskList.append(b)
    maskList = torch.cat(maskList,0).to(torch.float32)
    fl = flow.RNVP(maskList, [utils.SimpleMLPreshape([num]+[hidden]*nmlp+[num],[nn.Softplus()]*nmlp+[None]) for _ in range(nlayers)], [utils.SimpleMLPreshape([num]+[hidden]*nmlp+[num],[nn.Softplus()]*nmlp+[utils.ScalableTanh(num)]) for _ in range(nlayers)])
    return fl
from utils import flowBuilder
f = flowBuilder(n,numFlow,innerBuilder,1).to(device).to(dtype)

# Load saving.

import os
import glob
name = max(glob.iglob(rootFolder+"savings/"+'*.saving'), key=os.path.getctime)
print("load saving at "+name)
saved = torch.load(name,map_location=device)
f.load(saved);<jupyter_output>load saving at ./demo/variationModel_source_0_Shift_False_T_300_depthLevel_1_l8_M2_H128/savings/FlowNetSaving_epoch4990.saving<jupyter_text>3. AnalysisBecause this distribution is a ring, we presume the latent variables to be R and $\theta$. So, we plot the latent variable against calculated {R, $\theta$}.<jupyter_code># Calculate modes in the latent space.

d0 = f.layerList[0].elements[:n]
d1 = f.layerList[0].elements[n:]
omega = (1/(torch.exp(d0+d1)))

# Plot these modes

from matplotlib import pyplot as plt
t =f.sample(1000)[0]

z = f.forward(t)[0].detach().numpy()
r = torch.sqrt(t[:,0]**2+t[:,1]**2).detach().numpy()
theta = torch.atan2(t[:,1],t[:,0]).detach().numpy()

plt.figure()
plt.scatter(z[:,0],r)
plt.scatter(z[:,0],theta)
plt.title("Q1")

plt.figure()
plt.scatter(z[:,1],r)
plt.scatter(z[:,1],theta)
plt.title("Q2")

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15939115450890293, 'fraction_numerical': 0.023549684089603676, 'mean_word_length': 4.747524752475248, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '618730', 'license': 'Apache-2.0', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1294, 'n_words': 284, 'path': '1_Ringworld.ipynb', 'repo_name': 'jameswind/neuralCT', 'size': '3482'}