<jupyter_start><jupyter_text>MXNet (Using Gluon)<jupyter_code>import mxnet as mx
from mxnet import nd, autograd, gluon
import matplotlib.pyplot as plt

data_ctx = mx.cpu()
model_ctx = mx.gpu()
num_inputs = 2
num_outputs = 1
num_examples = 10000

def function(x):
    return 2 * x[:, 0] - 0.2 * x[:, 1] + 2.4

train_x = nd.random.normal(shape = (num_examples, num_inputs), ctx = data_ctx)
noise = 0.1 * nd.random.normal(shape = (num_examples,), ctx = data_ctx)
train_y = function(train_x) + noise

test_x = nd.random.normal(shape = (1000, num_inputs), ctx = data_ctx)
noise = 0.1 * nd.random.normal(shape = (1000,), ctx = data_ctx)
test_y= function(test_x) + noise

print(train_x.shape)
print(train_y.shape)

print(test_x.shape)
print(test_y.shape)<jupyter_output>(10000, 2)
(10000,)
(1000, 2)
(1000,)<jupyter_text>Visualizing training dataset<jupyter_code>plt.scatter(train_x[:, 0].asnumpy(), train_y.asnumpy())
plt.title('Label vs Feature 1')
plt.xlabel('Features 1')
plt.ylabel('Labels')
plt.show()

plt.scatter(train_x[:, 1].asnumpy(), train_y.asnumpy())
plt.title('Label vs Feature 2')
plt.xlabel('Features 2')
plt.ylabel('Labels')
plt.show()<jupyter_output><empty_output><jupyter_text>Visualizing testing dataset<jupyter_code>plt.scatter(test_x[:, 0].asnumpy(), test_y.asnumpy())
plt.title('Label vs Feature 1')
plt.xlabel('Features 1')
plt.ylabel('Labels')
plt.show()

plt.scatter(test_x[:, 1].asnumpy(), test_y.asnumpy())
plt.title('Label vs Feature 2')
plt.xlabel('Features 2')
plt.ylabel('Labels')
plt.show()<jupyter_output><empty_output><jupyter_text>Defining data iterator for training dataset<jupyter_code>batch_size = 16
dataset = gluon.data.DataLoader(gluon.data.ArrayDataset(train_x, train_y), batch_size = batch_size, shuffle = True)<jupyter_output><empty_output><jupyter_text>Building linear regression model<jupyter_code>model = gluon.nn.Dense(num_outputs)
model.initialize(ctx = model_ctx, init = mx.init.Normal(sigma = 1.0))<jupyter_output><empty_output><jupyter_text>Defining our objective function<jupyter_code>meansquare = gluon.loss.L2Loss()<jupyter_output><empty_output><jupyter_text>Defining our parameters optimizer<jupyter_code>optimizer = gluon.Trainer(model.collect_params(), 'sgd', {'learning_rate': 0.0001})<jupyter_output><empty_output><jupyter_text>Training the  model<jupyter_code>epochs = 20
num_batches = num_examples/batch_size
losses = []

for epoch in range(epochs):
    cumulative_loss = 0
    for i, (features, labels) in enumerate(dataset):
        features = features.as_in_context(model_ctx)
        labels = labels.as_in_context(model_ctx).reshape((-1, 1))
        with autograd.record():
            output = model(features)
            loss = meansquare(output, labels)
        loss.backward()
        optimizer.step(batch_size)
        cumulative_loss += loss.mean()

    print('Epoch: {} Loss: {}'.format(epoch, cumulative_loss.asscalar()/num_batches))
    losses.append(cumulative_loss.asscalar()/num_batches)
def plot(losses, sample_size=100):
    xs = list(range(len(losses)))
    plt.plot(xs, losses, '-r')
    plt.title('Loss during training')
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.show()
    ####################################
    # Visualizing training predictions #
    ####################################
    plt.plot(train_x[:sample_size, 0].asnumpy(),
             model(train_x[:sample_size, :].as_in_context(model_ctx)).asnumpy(), 'or', label='Estimated')
    plt.plot(train_x[:sample_size, 0].asnumpy(),
             function(train_x[:sample_size, :]).asnumpy(), '*g', label='Real')
    plt.legend()
    plt.xlabel('First Features')
    plt.ylabel('Labels')
    plt.title('Prediction on training data as viewed from feature 1')
    plt.show()
    
    plt.plot(train_x[:sample_size, 1].asnumpy(),
             model(train_x[:sample_size, :].as_in_context(model_ctx)).asnumpy(), 'or', label='Estimated')
    plt.plot(train_x[:sample_size, 1].asnumpy(),
             function(train_x[:sample_size, :]).asnumpy(), '*g', label='Real')
    plt.legend()
    plt.xlabel('Second Features')
    plt.ylabel('Labels')
    plt.title('Prediction on training data as viewed from feature 2')
    plt.show()
    ####################################
    # Visualizing testing predictions #
    ####################################
    plt.plot(test_x[:sample_size, 0].asnumpy(),
             model(test_x[:sample_size, :].as_in_context(model_ctx)).asnumpy(), 'or', label='Estimated')
    plt.plot(test_x[:sample_size, 0].asnumpy(),
             function(test_x[:sample_size, :]).asnumpy(), '*g', label='Real')
    plt.legend()
    plt.xlabel('First Features')
    plt.ylabel('Labels')
    plt.title('Prediction on testing data as viewed from feature 1')
    plt.show()
    
    plt.plot(test_x[:sample_size, 1].asnumpy(),
             model(test_x[:sample_size, :].as_in_context(model_ctx)).asnumpy(), 'or', label='Estimated')
    plt.plot(test_x[:sample_size, 1].asnumpy(),
             function(test_x[:sample_size, :]).asnumpy(), '*g', label='Real')
    plt.legend()
    plt.xlabel('Second Features')
    plt.ylabel('Labels')
    plt.title('Prediction on testing data as viewed from feature 2')
    plt.show()
plot(losses)<jupyter_output><empty_output><jupyter_text>Lets look out our weight and bias value after training<jupyter_code>for param in model.collect_params().values():
    print(f'\n{param.name} value: {param.data()}')<jupyter_output>dense2_weight value: 
[[ 1.5061544  -0.14645386]]
<NDArray 1x2 @gpu(0)>

dense2_bias value: 
[1.7221934]
<NDArray 1 @gpu(0)>
{'fraction_non_alphanumeric': 0.18032490974729243, 'fraction_numerical': 0.021660649819494584, 'mean_word_length': 4.970905172413793, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '233213', 'license': 'MIT', 'n_tokens_mistral': 2090, 'n_tokens_neox': 1944, 'n_words': 345, 'path': 'MXNet Gluon Implementations/01. Linear_Regression_MXNet_(Using_Gluon).ipynb', 'repo_name': 'braindotai/MXNet-Gluon-Tutorials', 'size': '5540'}