Created
October 24, 2019 05:29
-
-
Save analyticsindiamagazine/54681cd0aa2758cdf6c0d8d453688d32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Deep_Neural_Network.ipynb", | |
"provenance": [], | |
"collapsed_sections": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "RGxe5H9TNCM7", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Regression With Tensorflow 2.0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "sX3FalwWNU2N", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "9a021702-8258-4123-d0a2-8bd7f36be4c9" | |
}, | |
"source": [ | |
"#Imports Tensorflow 2.0 in Google Colab\n", | |
"try:\n", | |
" %tensorflow_version 2.x \n", | |
"except Exception:\n", | |
" pass\n", | |
"\n", | |
"import tensorflow as tf" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"TensorFlow 2.x selected.\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "jMlc5th0JQXi", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"X_train : An array consisting of the independent variables from the training set.\n", | |
"\n", | |
"y_train : An array consisting of the dependent variable / Target from the training set.\n", | |
"\n", | |
"X_test : An array consisting of the independent variables from the test set.\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BK1u6YRBY1B5", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"\n", | |
"#Initializing The Neural Network\n", | |
"model = keras.Sequential([\n", | |
" keras.layers.InputLayer(input_shape = 'number_of_independent_variables'),\n", | |
" keras.layers.Dense(500, activation = \"relu\", kernel_initializer = 'uniform'),\n", | |
" keras.layers.Dense(1)\n", | |
"])" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "RpgxGwiMJ1pM", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"The above code block initializes a neural network with :\n", | |
"\n", | |
"* An input layer consisting of n number of nodes where n is the number of independent variables.\n", | |
"\n", | |
"* A hidden layer with 500 nodes , activated by relu activation function and uniform initializaton of weights.\n", | |
"\n", | |
"* An output layer of a single node.\n", | |
"\n", | |
"---\n", | |
"\n", | |
"Click [here](https://www.tensorflow.org/api_docs/python/tf/keras/activations) for complete list of available **activation functions**.\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "W0V6N-DdbENO", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#Compiling The Neural Network With an optimizer,a loss and metrics\n", | |
"model.compile(\n", | |
" optimizer='sgd',\n", | |
" loss='mean_squared_logarithmic_error',\n", | |
" metrics= ['RootMeanSquaredError']\n", | |
" )" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Fa0o8q35K7fU", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Click [here](https://www.tensorflow.org/api_docs/python/tf/keras/optimizers) for complete list of available **optimizers**.\n", | |
"\n", | |
"Click [here](https://www.tensorflow.org/api_docs/python/tf/keras/losses) for complete list of available **loss functions**.\n", | |
"\n", | |
"Click [here](https://www.tensorflow.org/api_docs/python/tf/keras/metrics) for complete list of available **metrics**.\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "0n_p-xwCcEVU", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#Training The Network With Training Data And Validating On A Fraction It\n", | |
"model.fit(X_train, y_train,epochs=20,validation_split = 0.2, shuffle=True)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "9pv0X3GIL6Ie", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"* The above code block trains the neural network using the training data (X_train and y_train) for 20 cycles specified by the epochs. One epoch is one complete cycle of training with the training data.\n", | |
"\n", | |
"* validation_split specifies the fraction of training samples to be used for validating or testing during the training process.\n", | |
"\n", | |
"* shuffle when set to true shuffles the training sample at each epoch.\n", | |
"\n", | |
"---\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "b2VMgEeWfA8h", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#Predicting Using The Trained Network\n", | |
"predictions = model.predict(X_test)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment