<jupyter_start><jupyter_text>Linear Regression  Import required packages<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import cntk
from cntk import Trainer, learning_rate_schedule, UnitType
from cntk.learners import sgd
from cntk.layers import default_options, Dense
import argparse<jupyter_output><empty_output><jupyter_text>Define the parameters<jupyter_code>num_samples = 500
initial_learning_rate = 0.001
num_epochs = 300
num_samples_to_train = 400
batch_size = 25

# Network has only one output which is its prediction.
input_dim = 1
num_outputs = 1
num_minibatches_to_train = int(num_samples_to_train / batch_size)<jupyter_output><empty_output><jupyter_text>Arbitrary data creation<jupyter_code># random data
features = np.linspace(-1, 1, num_samples)
predictions = 2 * features + np.random.randn(*features.shape) * 0.5<jupyter_output><empty_output><jupyter_text>Scatter plot<jupyter_code>plt.scatter(features, predictions, c='r')
plt.xlabel("X")
plt.ylabel("Y")
plt.show()<jupyter_output><empty_output><jupyter_text>Adding one dimension for further processing. Input must be formatted as (batch_size,1).<jupyter_code>features = features[:,None]
predictions = predictions[:,None]<jupyter_output><empty_output><jupyter_text>Network Architecture  Output is a single node with a linear operation.<jupyter_code>input = cntk.input_variable(input_dim)
label = cntk.input_variable(num_outputs)
pred = Dense(num_outputs)(input)<jupyter_output><empty_output><jupyter_text>Defining loss<jupyter_code>loss = cntk.squared_error(pred, label)
eval_fun = cntk.squared_error(pred, label)<jupyter_output><empty_output><jupyter_text>Training  Instantiate the trainer object to drive the model training<jupyter_code>learning_rate = learning_rate_schedule(initial_learning_rate, UnitType.minibatch)
optimizer_op = sgd(pred.parameters, learning_rate)
train_op = Trainer(pred, (loss, eval_fun), [optimizer_op])<jupyter_output><empty_output><jupyter_text>Training loop<jupyter_code>for epoch in range(0, num_epochs):
    for batch_num in range(0, num_minibatches_to_train):
        batch_features = features[(batch_num * batch_size):(batch_num * batch_size + batch_size), :].astype(np.float32)
        batch_labels = predictions[(batch_num * batch_size):(batch_num * batch_size + batch_size), :]
        train_op.train_minibatch({input: batch_features, label: batch_labels})
        training_loss = train_op.previous_minibatch_loss_average
        eval_value = train_op.previous_minibatch_evaluation_average
    print("Epoch: {0}, Loss: {1:.2f}".format(epoch+1, training_loss))<jupyter_output>/home/sina/anaconda/envs/cntk-py35/lib/python3.5/site-packages/cntk/core.py:351: UserWarning: your data is of type "float64", but your input variable (uid "Input4") expects "<class 'numpy.float32'>". Please convert your data beforehand to speed up training.
  (sample.dtype, var.uid, str(var.dtype)))<jupyter_text>Model Evaluation  Test data<jupyter_code>test_features = features[num_samples_to_train:num_samples, :]
test_labels = predictions[num_samples_to_train:num_samples, :]<jupyter_output><empty_output><jupyter_text>Train data<jupyter_code>train_features = features[0:num_samples_to_train, :]
train_labels = predictions[0:num_samples_to_train, :]<jupyter_output><empty_output><jupyter_text>Print weight and bias<jupyter_code>print("Trained parameters are: w= {0:.2f}, b={1:.2f}".format(pred.W.value[0][0], pred.b.value[0]))<jupyter_output>Trained parameters are: w= 1.57, b=-0.12<jupyter_text>Plot  Evaluation of training set<jupyter_code>plt.scatter(train_features[:,0], train_labels[:,0], c='b')
X = train_features[:,0]
Y = pred.W.value[0] * train_features[:,0] + pred.b.value[0]
plt.plot(X, Y, 'r')
plt.xlabel("Feature")
plt.ylabel("Predicted")
plt.show()<jupyter_output><empty_output><jupyter_text>Evaluation on test set<jupyter_code>plt.scatter(test_features[:,0], test_labels[:,0], c='b')
X = test_features[:,0]
Y = pred.W.value[0] * test_features[:,0] + pred.b.value[0]
plt.plot(X, Y, 'r')
plt.xlabel("Feature")
plt.ylabel("Predicted")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12561214495592557, 'fraction_numerical': 0.017140058765915768, 'mean_word_length': 8.284090909090908, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 56, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '566680', 'license': 'MIT', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1469, 'n_words': 254, 'path': 'codes/IPython/p02-basic-models/1-linear-regression/linear-regression.ipynb', 'repo_name': 'mehrdad-shokri/CNTK-World', 'size': '4084'}