<jupyter_start><jupyter_text>Regression using Deep Neural Networks  Load Dataset from sklearn<jupyter_code>!pip install sklearn
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_boston
boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
df["TARGET"] = boston.target
df.tail()
scaler = StandardScaler()
scaler.fit(df.values[:, :-1])
df.values[:, :-1] = scaler.transform(df.values[:, :-1])

df.tail()<jupyter_output><empty_output><jupyter_text>Train Model with PyTorch<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
data = torch.from_numpy(df.values).float()

data.shape
y = data[:, -1:]
x = data[:, :-1]

print(x.shape, y.shape)
n_epochs = 100000
learning_rate = 1e-4
print_interval = 5000<jupyter_output><empty_output><jupyter_text>Build Models  Build Model using nn.Module<jupyter_code>relu = nn.ReLU()
leaky_relu = nn.LeakyReLU(0.1)
class MyModel(nn.Module):
    
    def __init__(self, input_dim, output_dim):
        self.input_dim = input_dim
        self.output_dim = output_dim
        
        super().__init__()
        
        self.linear1 = nn.Linear(input_dim, 3)
        self.linear2 = nn.Linear(3, 3)
        self.linear3 = nn.Linear(3, output_dim)
        self.act = nn.ReLU()
        
    def forward(self, x):
        # |x| = (batch_size, input_dim)
        h = self.act(self.linear1(x)) # |h| = (batch_size, 3)
        h = self.act(self.linear2(h))
        y = self.linear3(h)
        # |y| = (batch_size, output_dim)
        
        return y
    
model = MyModel(x.size(-1), y.size(-1))

model<jupyter_output><empty_output><jupyter_text>Build Model with LeakyReLU using nn.Sequential<jupyter_code>model = nn.Sequential(
    nn.Linear(x.size(-1), 3),
    nn.LeakyReLU(),
    nn.Linear(3, 3),
    nn.LeakyReLU(),
    nn.Linear(3, 3),
    nn.LeakyReLU(),
    nn.Linear(3, 3),
    nn.LeakyReLU(),
    nn.Linear(3, 3),
    nn.LeakyReLU(),
    nn.Linear(3, y.size(-1)),
)

model
optimizer = optim.SGD(model.parameters(),
                      lr=learning_rate)
for i in range(n_epochs):
    y_hat = model(x)
    loss = F.mse_loss(y_hat, y)
    
    optimizer.zero_grad()
    loss.backward()
    
    optimizer.step()
    
    if (i + 1) % print_interval == 0:
        print('Epoch %d: loss=%.4e' % (i + 1, loss))<jupyter_output>Epoch 5000: loss=8.4416e+01
Epoch 10000: loss=8.4415e+01
Epoch 15000: loss=8.4413e+01
Epoch 20000: loss=7.8406e+01
Epoch 25000: loss=1.3286e+01
Epoch 30000: loss=1.2725e+01
Epoch 35000: loss=1.2526e+01
Epoch 40000: loss=1.1895e+01
Epoch 45000: loss=1.1548e+01
Epoch 50000: loss=1.1223e+01
Epoch 55000: loss=1.0938e+01
Epoch 60000: loss=1.0711e+01
Epoch 65000: loss=9.3744e+00
Epoch 70000: loss=7.6595e+00
Epoch 75000: loss=6.9922e+00
Epoch 80000: loss=7.1098e+00
Epoch 85000: loss=6.8505e+00
Epoch 90000: loss=7.0606e+00
Epoch 95000: loss=6.6112e+00
Epoch 100000: loss=6.5394e+00<jupyter_text>Let's see the result!<jupyter_code>df = pd.DataFrame(torch.cat([y, y_hat], dim=1).detach().numpy(),
                  columns=["y", "y_hat"])

sns.pairplot(df, height=5)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14474899907607022, 'fraction_numerical': 0.08931321219587311, 'mean_word_length': 3.653295128939828, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '242792', 'license': 'MIT', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1314, 'n_words': 255, 'path': '10-dnn_1/10-06-dnn_regression.ipynb', 'repo_name': 'sujungleeml/testvscode', 'size': '3247'}