<jupyter_start><jupyter_text>Section 4 - Computer vision-based machine learning  Mini-project 4: Introduction to `SciNet` architecture  Dr. <NAME> (<EMAIL>)  High Energy Physics Group 523 Blackett Lab  The markdown comments were added by <NAME> (<EMAIL>) * [Original repository](https://github.com/fd17/SciNet_PyTorch)* [Reference](https://arxiv.org/abs/1807.10300)<jupyter_code>import torch
import numpy as np
from models import SciNet
from utils import pendulum
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Load trained model
scinet = SciNet(50,1,3,64)
scinet.load_state_dict(torch.load("trained_models/scinet1.dat"))
# Set resolution for plots
size = 20

# Initialize array for latent layer information
neuron_activation = [np.zeros((size,size)),np.zeros((size,size)),np.zeros((size,size))]

# Set pendulum parameters
tmax = 10
A0 = 1
delta0 = 0
m = 1
N_SAMPLE = 50
for ik, k in enumerate(np.linspace(5,10,size)):
    for ib, b in enumerate(np.linspace(0.5,1,size)):

        tprime = np.random.uniform(0,tmax)
        question = tprime
        answer = pendulum(tprime,A0,delta0,k,b,m)
        if answer == None:
            continue
        x = np.linspace(0,tmax,50)
        t_arr = np.linspace(0,tmax,N_SAMPLE)
        x = pendulum(t_arr,A0,delta0,k,b,m)
        combined_inputs = np.append(x, question)
        results = scinet.forward(torch.Tensor([combined_inputs]))

        latent_layer = scinet.mu.detach().numpy()[0]
        
        neuron_activation[0][ik,ib] = latent_layer[0]
        neuron_activation[1][ik,ib] = latent_layer[1]
        neuron_activation[2][ik,ib] = latent_layer[2]<jupyter_output>/home/hep/shayashi/.local/lib/python3.6/site-packages/ipykernel_launcher.py:25: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at  ../torch/csrc/utils/tensor_new.cpp:201.)<jupyter_text>Let's visualizing the activations from the latent layer (`Z-axis`) for `k` and `b`; parameters of the oscillation of the pendulum.<jupyter_code>fig = fig = plt.figure(figsize=(30,8))
for i in range(3):
    ax = fig.add_subplot(1, 3, 1+i, projection='3d')
    k_array = np.linspace(5,10,size)
    b_array = np.linspace(0.5,1,size)
    X,Y = np.meshgrid(k_array, b_array)
    Z = neuron_activation[i]
    surf = ax.plot_surface(X, Y, Z, cmap=None,
                           linewidth=0, antialiased=True)
    ax.set_xlabel("k")
    ax.set_ylabel("b")
    ax.set_zlim3d(-.2,.2)
    ax.set_title("Latent Neuron %i" % (i+1))
    ax.view_init(25, -120)


plt.show()
from utils import pendulum as solution
# Pendulum settings
tmax = 10
A0 = 1
delta0 = 0
m = 1
N_SAMPLE = 50

xarr = np.linspace(0,tmax,N_SAMPLE)
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True)

yarr = solution(xarr,A0,delta0,10,1,m)
sampled_points = yarr
y_test = []
xspace = np.linspace(0,tmax,50)

for x in xspace:
    x_in = np.append(sampled_points, x)
    x_in = torch.Tensor(x_in).reshape(1,N_SAMPLE+1)
    y_test.append(scinet.forward(x_in)[0,0])
ax1.plot(xspace,yarr, ".", label="Input trajectory")
ax1.plot(xspace,y_test, label="Scinet reconstruction")
ax1.legend(loc="upper right")
ax1.set_title("k=%.1f, b=%.1f" %(10,1))

yarr = solution(xarr,A0,delta0,5,0.5,m)
sampled_points = yarr
y_test = []
xspace = np.linspace(0,tmax,50)

for x in xspace:
    x_in = np.append(sampled_points, x)
    x_in = torch.Tensor(x_in).reshape(1,N_SAMPLE+1)
    y_test.append(scinet.forward(x_in)[0,0])
ax2.plot(xspace,yarr, ".", label="Input trajectory")
ax2.plot(xspace,y_test, label="Scinet reconstruction")
ax2.set_title("k=%.1f, b=%.1f" %(5,0.5))
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14415376401494928, 'fraction_numerical': 0.03844100373731981, 'mean_word_length': 4.2998585572843, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 2, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '489661', 'license': 'MIT', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1449, 'n_words': 297, 'path': 'Analysis.ipynb', 'repo_name': 'shingohayashida/MRES-SciNet_PyTorch', 'size': '3801'}