<jupyter_start><jupyter_text>Regression test using 1D Convolution Neural Network This notebook shows how to use one dimension Convolutional Neural Netorks to perform regression test on two variables. The independent variable *x* and the dependent variable *y*First we import the necessary libraries<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
from collections import OrderedDict,Counter<jupyter_output><empty_output><jupyter_text>Let's generate a random sample of 300 points using `torch.randn`.*y* is then calculated from *x* values using any equation you want. Here i'm using the below.And plot x,y<jupyter_code>x = torch.randn(300)
y = 2.5 * x**3 + x**2 - 5* x + 3 + 8.5 * torch.randn(300)

plt.figure(figsize=(10,10))
plt.scatter(x,y)<jupyter_output><empty_output><jupyter_text>CNNLet's build the Deep Neural Network using the below method:1. Define a sequential model2. Add two `Conv1d` layers3. Batch normalize them4. Use Sigmoid as an activation function for the hidden and input layers5. Maxpool the convolutional outcome6. Flatten the outcome into a single Linear layer7. The model spits out 1 prediction<jupyter_code>class Net1(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Sequential(OrderedDict([('conv1',nn.Conv1d(1,10,1,bias=False)),
                                                ('bn1',nn.BatchNorm1d(10)),
                                                ('act1',nn.Sigmoid()),
                                                ('conv2',nn.Conv1d(10,100,1,bias=False)),
                                                ('bn2',nn.BatchNorm1d(100)),
                                                ('act1',nn.Sigmoid()),
                                                ('avgpool',nn.AdaptiveMaxPool1d(1)),
                                                ('flat',nn.Flatten())
                                               ]))
        self.layer3 = nn.Linear(in_features=100,out_features=1,bias=True)
        
    def forward(self,x):
        x = self.model(x)
        x = self.layer3(x)
        return x
    
    #Define learning function 
    def learn(self,epochs,x,y,lr):
        loss_fun = nn.MSELoss()
        optimizer = torch.optim.SGD(self.parameters(),lr=lr,momentum=0.9,weight_decay=1e-5)
        losses = []
        for epoch in range(epochs):
            optimizer.zero_grad()
            output = self(x)
            loss = loss_fun(output,y)
            losses.append(loss.item())
            loss.backward()
            if epoch%(epochs/20)==0: print("epoch:{} loss: {}".format(epoch,round(loss.item(),8)))
            optimizer.step()
        return losses
    
    @torch.no_grad()
    def predict(self,x):
        y = self(x)
        return y
                   
net1 = Net1()<jupyter_output><empty_output><jupyter_text>Adjust input tensor into Conv1d friendly shapeSince the input layer is a convolutional one that takes 3d tensors as input, we need to alter the shape of our input by double squeezing our independent variabes<jupyter_code>X = x.unsqueeze(1);X.unsqueeze_(1);X.shape<jupyter_output><empty_output><jupyter_text>Let's test the input/output to make sure everything is in order before start the learning process<jupyter_code>net1(X).shape
loss1 = net1.learn(100,X,y.view(-1,1),1e-3)
plt.figure(figsize=(12,8))
plt.plot(loss1)
plt.xlabel('Epochs',fontsize=14)
plt.ylabel('Losses',fontsize=14)
with torch.no_grad():
    y_hat1 = net1(X) #The predictions
y_hat1.shape
plt.figure(figsize=(12,12))
plt.scatter(x,y)
plt.scatter(X,y_hat1.detach())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11233784156776153, 'fraction_numerical': 0.028981507038365995, 'mean_word_length': 2.591674925668979, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '571253', 'license': 'MIT', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1100, 'n_words': 272, 'path': 'NN/Conv1dRegression.ipynb', 'repo_name': 'ayjabri/Regression', 'size': '3623'}