<jupyter_start><jupyter_text>Linear Regressionhttps://github.com/yunjey/pytorch-tutorial  Artificial dataset<jupyter_code>import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt

# hyper parameters
input_size = 1
output_size = 1
num_epochs = 100
learning_rate = 0.001

# toy dataset
# 15 samples, 1 features
x_train = np.array([3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167,
                    7.042, 10.791, 5.313, 7.997, 3.1], dtype=np.float32)

y_train = np.array([1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221,
                    2.827, 3.465, 1.65, 2.904, 1.3], dtype=np.float32)

x_train = x_train.reshape(15, 1)
y_train = y_train.reshape(15, 1)

# linear regression model
class LinearRegression(nn.Module):

    def __init__(self, input_size, output_size):
        super(LinearRegression, self).__init__()
        self.linear = nn.Linear(input_size, output_size)
    
    def forward(self, x):
        out = self.linear(x)
        return out

model = LinearRegression(input_size, output_size)

# loss and optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

# train the model
for epoch in range(num_epochs):
    inputs = torch.from_numpy(x_train)
    targets = torch.from_numpy(y_train)

    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    loss.backward()
    optimizer.step()
    
    if (epoch + 1) % 10 == 0:
        print('Epoch [%d/%d], Loss: %.4f' % (epoch + 1, num_epochs, loss.item()))
torch.save(model.state_dict(), 'model.pkl')
# plot the graph
predicted = model(torch.from_numpy(x_train)).detach().numpy()
plt.plot(x_train, y_train, 'ro', label='Original data')
plt.plot(x_train, predicted, label='Fitted line')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Boston house price dataset- https://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/- https://medium.com/@haydar_ai/learning-data-science-day-9-linear-regression-on-boston-housing-dataset-cd62a80775ef<jupyter_code>import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt
%matplotlib inline
# hyper parameters
input_size = 13
output_size = 1
num_epochs = 5000
learning_rate = 0.01

boston = load_boston()
X = boston.data
y = boston.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 5)

# データの標準化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

y_train = np.expand_dims(y_train, axis=1)
y_test = np.expand_dims(y_test, axis=1)

# linear regression model
class LinearRegression(nn.Module):

    def __init__(self, input_size, output_size):
        super(LinearRegression, self).__init__()
        self.linear = nn.Linear(input_size, output_size)

    def forward(self, x):
        out = self.linear(x)
        return out

model = LinearRegression(input_size, output_size)

# loss and optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

def train(X_train, y_train):
    inputs = torch.from_numpy(X_train).float()
    targets = torch.from_numpy(y_train).float()

    optimizer.zero_grad()
    outputs = model(inputs)

    loss = criterion(outputs, targets)
    loss.backward()
    optimizer.step()

    return loss.item()

def valid(X_test, y_test):
    inputs = torch.from_numpy(X_test).float()
    targets = torch.from_numpy(y_test).float()
    
    outputs = model(inputs)
    val_loss = criterion(outputs, targets)
    
    return val_loss.item()
        
# train the model
loss_list = []
val_loss_list = []
for epoch in range(num_epochs):
    # data shuffle
    perm = np.arange(X_train.shape[0])
    np.random.shuffle(perm)
    X_train = X_train[perm]
    y_train = y_train[perm]

    loss = train(X_train, y_train)
    val_loss = valid(X_test, y_test)

    if epoch % 200 == 0:
        print('epoch %d, loss: %.4f val_loss: %.4f' % (epoch, loss, val_loss))

    loss_list.append(loss)
    val_loss_list.append(val_loss)

# plot learning curve
plt.plot(range(num_epochs), loss_list, 'r-', label='train_loss')
plt.plot(range(num_epochs), val_loss_list, 'b-', label='val_loss')
plt.legend()<jupyter_output>epoch 0, loss: 582.9910 val_loss: 594.2480
epoch 200, loss: 453.9804 val_loss: 479.6869
epoch 400, loss: 373.9557 val_loss: 402.7326
epoch 600, loss: 308.8472 val_loss: 337.8119
epoch 800, loss: 253.5647 val_loss: 281.1577
epoch 1000, loss: 206.5357 val_loss: 232.3899
epoch 1200, loss: 166.8685 val_loss: 191.0127
epoch 1400, loss: 133.7838 val_loss: 156.2874
epoch 1600, loss: 106.5488 val_loss: 127.4714
epoch 1800, loss: 84.4694 val_loss: 103.8716
epoch 2000, loss: 66.8853 val_loss: 84.8388
epoch 2200, loss: 53.1687 val_loss: 69.7598
epoch 2400, loss: 42.7244 val_loss: 58.0542
epoch 2600, loss: 34.9919 val_loss: 49.1742
epoch 2800, loss: 29.4506 val_loss: 42.6082
epoch 3000, loss: 25.6266 val_loss: 37.8878
epoch 3200, loss: 23.0996 val_loss: 34.5939
epoch 3400, loss: 21.5107 val_loss: 32.3652
epoch 3600, loss: 20.5666 val_loss: 30.9023
epoch 3800, loss: 20.0406 val_loss: 29.9688
epoch 4000, loss: 19.7679 val_loss: 29.3874
epoch 4200, loss: 19.6375 val_loss: 29.0325
epoch 4400, loss: 19[...]
{'fraction_non_alphanumeric': 0.1260765988638446, 'fraction_numerical': 0.09712296133406634, 'mean_word_length': 4.086672879776328, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 3, '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': '37636', 'license': 'MIT', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2075, 'n_words': 479, 'path': '180130-linear-regression.ipynb', 'repo_name': 'aidiary/pytorch-examples', 'size': '5457'}