{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introducing Keras\n", "> In this first chapter, you will get introduced to neural networks, understand what kind of problems they can solve, and when to use them. You will also build several networks and save the earth by training a regression model that approximates the orbit of a meteor that is approaching us! This is the Summary of lecture \"Introduction to Deep Learning with Keras\", via datacamp.\n", "\n", "- toc: true \n", "- badges: true\n", "- comments: true\n", "- author: Chanseok Kang\n", "- categories: [Python, Datacamp, Tensorflow-Keras, Deep_Learning]\n", "- image: images/plot_orbit.png" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import tensorflow as tf\n", "import matplotlib.pyplot as plt\n", "\n", "plt.rcParams['figure.figsize'] = (8, 8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is Keras?\n", "- Keras + Tensorflow\n", " - Tensorflow's high level framework of choice\n", " - Keras is complementary to Tensorflow\n", "- Feature Engineering\n", "![fe](image/fe.png)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Your first neural network\n", "- Neural Network\n", "![nn](image/simple_nn.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Hello nets!\n", "You're going to build a simple neural network to get a feeling of how quickly it is to accomplish this in Keras.\n", "\n", "You will build a network that takes two numbers as an input, passes them through a hidden layer of 10 neurons, and finally outputs a single non-constrained number.\n", "\n", "A non-constrained output can be obtained by avoiding setting an activation function in the output layer. This is useful for problems like regression, when we want our output to be able to take any non-constrained value.\n", "![net](image/hello_nets.png)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential\"\n", "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "dense (Dense) (None, 10) 30 \n", "_________________________________________________________________\n", "dense_1 (Dense) (None, 1) 11 \n", "=================================================================\n", "Total params: 41\n", "Trainable params: 41\n", "Non-trainable params: 0\n", "_________________________________________________________________\n" ] } ], "source": [ "from tensorflow.keras import Sequential\n", "from tensorflow.keras.layers import Dense\n", "\n", "# Create a Sequential model\n", "model = Sequential()\n", "\n", "# Add an input layer and a hidden layer with 10 neurons\n", "model.add(Dense(10, input_shape=(2, ), activation='relu'))\n", "\n", "# Add a 1-neuron output layer\n", "model.add(Dense(1))\n", "\n", "# Summarize your model\n", "model.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Surviving a meteor strike\n", "- Scientific prediction\n", "![meteor](image/meteor.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specifying a model\n", "You will build a simple regression model to predict the orbit of the meteor!\n", "\n", "Your training data consist of measurements taken at time steps from -10 minutes before the impact region to +10 minutes after. Each time step can be viewed as an X coordinate in our graph, which has an associated position Y for the meteor orbit at that time step.\n", "\n", "Note that you can view this problem as approximating a quadratic function via the use of neural networks.\n", "![meteor2](image/meteor_orbit_3.jpg)\n", "This data is stored in two numpy arrays: one called `time_steps` , what we call features, and another called `y_positions`, with the labels. Go on and build your model! It should be able to predict the y positions for the meteor orbit at future time steps." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | time_steps | \n", "y | \n", "
---|---|---|
0 | \n", "-10.000000 | \n", "100.000000 | \n", "
1 | \n", "-9.989995 | \n", "99.800000 | \n", "
2 | \n", "-9.979990 | \n", "99.600200 | \n", "
3 | \n", "-9.969985 | \n", "99.400601 | \n", "
4 | \n", "-9.959980 | \n", "99.201201 | \n", "