<jupyter_start><jupyter_text>Import packages* numpy - package for scientific computing with Python* pyplot - provides a MATLAB-like plotting framework.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Import keras packages* keras - the neural network library* Sequential -  basic keras model composed of a linear stack of layers.* Dense - a regular densely-connected NN layer.<jupyter_code>import keras
from keras.models import Sequential
from keras.layers import Dense<jupyter_output><empty_output><jupyter_text>Define a linear regression problem* Create a 100 data points and we will fit them on a straight line.* train_data_X has values between –1 and 1, and train_data_Y has 4 times the train_data_X and some randomness.<jupyter_code>train_data_X = np.linspace(-1, 1, 101)
train_data_Y = 4 * train_data_X + np.random.randn(101) * 0.44
train_data_X
train_data_Y
plt.scatter(train_data_X, train_data_Y, label='data',color=['red','green'])<jupyter_output><empty_output><jupyter_text>Building the model* Steps to build a keras model:     * Define the model    * Add layers    * Compile the model    * Train the model* We create a sequential model, which is the basic model in keras. * Only a single connection is required, so we use a Dense layer with linear activation.* Take input x and apply weight, w, and bias, b (wx + b ) followed by a linear activation to produce output.* Train the weights for 200 epochs. The value of weights should become 4.* Define mean squared error(mse) as the loss with simple gradient descent(sgd) as the optimizer.* get_weights() : returns the weights of the layer as a list of Numpy arrays. * summary() :  prints a summary representation of your model.<jupyter_code>model = Sequential()
model.add(Dense(input_dim=1,output_dim=1, init='uniform', activation='linear'))
# model.add(Dense(input_dim=1, name='Layer_1', output_dim=1, init='uniform', activation='linear'))
model.summary()
len(model.layers)
weights = model.layers[0].get_weights()
len(weights)
weights
weight_initial = weights[0]
bias_initial = weights[1]
print('Linear regression model is initialized with weights w: %.2f, b: %.2f' % (weight_initial, bias_initial))
model.compile(optimizer='sgd', loss='mse')
model.fit(train_data_X, train_data_Y, epochs=200, verbose=1)
weights = model.layers[0].get_weights()
weight_final = weights[0]
bias_final = weights[1]
print('Linear regression model is trained to have weight w: %.2f, b: %.2f' % (weight_final, bias_final))<jupyter_output><empty_output><jupyter_text>Test the model* predict() - Generates output predictions for the input samples.* result will be weight_final * train_data_X + bias_final* plot the regression line.<jupyter_code>result = model.predict(train_data_X)
plt.scatter(train_data_X, train_data_Y, label='data',color=['red','green'])
plt.plot(train_data_X, result, label='prediction')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1032498307379824, 'fraction_numerical': 0.013879485443466486, 'mean_word_length': 6.314356435643564, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '456951', 'license': 'Apache-2.0', 'n_tokens_mistral': 947, 'n_tokens_neox': 906, 'n_words': 304, 'path': 'Building_a_basic_keras_model.ipynb', 'repo_name': 'Katniss394/Keras', 'size': '2954'}