# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
#Author-<NAME>
# -

# ## Concise Implementation of Linear Regression

from mxnet import autograd, nd, gluon

# +
#Generating datasets as per the last notebook
# -

true_w=nd.array([2, -3.4])
true_b=4.2


def synthetic_data(w, b, num_examples):
    X=nd.random.normal(scale=1, shape=(num_examples, len(w)))
    y=nd.dot(X, w)+b
    y+=nd.random.normal(scale=0.01, shape=y.shape)
    return X, y


features, labels=synthetic_data(true_w, true_b, 1000)


# ## Reading data

# +
# We use Gluons's data module to read the data
# shuffle-shuffles the dataset through each epoch
# -

def load_array(data_arrays, batch_size, is_train=True):
    """Construct a Gluon data loader"""
    dataset=gluon.data.ArrayDataset(*data_arrays)
    return gluon.data.DataLoader(dataset, batch_size, shuffle=is_train)
batch_size=10
data_iter=load_array((features, labels), batch_size)

# +
# We can go through the data_iter
# -

for X, y in data_iter:
    print(X, y)
    break

# ## Define the Model

# +
# There is a fully connected layer in Linear Regression
# In Gluon the fully connected layer is defined by Dense class
# We want to generate a single output therefore the (1) in Dense class
# -

from mxnet.gluon import nn

net=nn.Sequential()

net.add(nn.Dense(1))

# ## Initialize Model Parameters

# +
# We need to initialize the weights and biases  in the linear regression model
# Gluon provides the init to access the initializer package
# The weights will be sampled from Gaussian distribution with mean 0 and std. deviation by 0.01
# The bias
# -

from mxnet import init

net.initialize(init.Normal(sigma=0.01))

# ## Define the Loss Function

from mxnet.gluon import loss as gloss

loss=gloss.L2Loss()

# ## Define the Optimization Algorithm

trainer=gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.03})

# ## Training

num_epochs=3
for epoch in range(1, num_epochs+1):
    for X, y in data_iter:
        with autograd.record():
            l=loss(net(X), y)
        l.backward()
        trainer.step(batch_size)
    l=loss(net(features), labels)
    print('epoch %d, loss: %f' % (epoch, l.mean().asnumpy()))

net[0].weight.data()

net[0].bias.data()



StarcoderdataJupyterScriptsDedupFiltered