# Neural Networks: Perceptrons & Sigmoid Neurons

A neural network is a deep learning system that is modeled after the human brain and the way biological organisms learn.

A perceptron is a single layer of a neural network. It takes in a few binary inputs, or yes or no inputs, and outputs a single binary output. Binary objects can have a value of either 0 (meaning no) or 1 (meaning yes). This is a model of a perceptron:

It looks confusing, but you don’t have to understand all of the symbols. Just know that x1, x2, and x3 are the 3 binary inputs into the neural network, the big circle in the middle is the decider, and the z is the yes or no output.

As an example, let’s use a perceptron to decide if I will or will not go out to get food. I will put in some considerations (x1, x2, x3) and make a yes or no decision. These will be my inputs, or factors that will affect my decision of whether to eat out or not:

• x1 = Food at home = 0
• x2 = Tired = 1
• x3 = Have cash = 1

If I have food at home, I will not go out to eat. If I have cash and/or I’m too tired to cook, I will go out to eat. But what if all 3 of these conditions are true? I have food at home, but I also have cash. Then, we have a conflicting yes and no. That’s why we need to add weights to our perceptron.

Weights are multipliers that show how important each input is to our final decision. I will add some weights to our inputs:

• x1 = Food at home = 1 x -3
• x2 = Tired = 1 x 3
• x3 = Have cash = 1 x 6

So if there was a day when I had food at home, I was tired, and I had cash: -3 + 3 + 6 = 6. I get 6 which is not a binary (0 or 1) output. Now that we’ve added weights, we have to figure out a way to change the output to be yes or no.

(By the way the w1, w2, and w3 in the above diagram are the weights added to the inputs)

We can achieve this by using sigmoid neurons instead of perceptrons. Sigmoid neurons are basically the same as perceptrons except they use decimals between 0 and 1 to make sure our output always comes out as a number between 0 and 1.

Let’s modify our inputs to reflect more ambiguous circumstances using sigmoid neurons. Let’s say I have food at home, but I don’t like the food. I’m only kind of tired. I barely have enough cash. I will give them values between 0 and 1 based on the particular situation.

• x1 = Food at home = 0.5
• x2 = Tired = 0.3
• x3 = Have cash = 0.9

Now we will add the same weights:

• x1 = Food at home = 0.5 x -3
• x2 = Tired = 0.3 x 3
• x3 = Have cash = 0.9 x 6

-1.5 + 0.9 + 5.4 = 4.8

Then, we would take the number 4.8 and turn it into a number between 0 and 1 using the sigmoid function.

An entire neural network is a multilayer perceptron or multiple layers of sigmoid neurons.

# What is Theano?

Theano is an open source Python library for building neural networks.

In its most basic form, Theano is an optimizing compiler for mathematical expressions. It is specially designed to handle the computation required for large neural network algorithms used in Deep Learning.

Theano is written in Python & works well with Python libraries such as NumPy.

It was created at MILA (Montreal Institute for Learning Algorithms) at the University of Montreal. Development on the project started in 2007 and Theano has been around for over a decade. It is a pioneer in the field of Deep Learning research and development. It is the tool behind many research breakthroughs.

# What is Keras?

Keras is a high-level framework for building and training deep learning models with relatively low amounts of code. It’s designed to make creating neural networks as simple as possible.

Although, it doesn’t run on its own. Keras is a front-end wrapper that runs on top of more complicated tools– TensorFlow or Theano. This means that you make your model in Keras, but behind the scenes it’s being converted to TensorFlow or Theano code.

One helpful feature of Keras is that it is designed to have best practices built in, so using the default settings is usually a safe bet. This makes it even easier for beginners.

In order to train a Machine Learning model, you need training data. TensorFlow offers multiple methods of loading datasets. These are your three main options:

2. Feed data at request
3. Set up a custom data pipeline

#### Option 1: Preload data into memory

• Easiest and fastest approach
• All of the data must fit in your computer’s memory
• Uses regular Python code, no TensorFlow

#### Option 2: Feed data at request

• Gives you more control, but also more manual responsibility
• Work with larger datasets
• Normal Python code

#### Option 3: Set up a custom data pipeline

A data pipeline lets TensorFlow load data into memory by itself as it needs it

• Can hold as much data as you want
• Requires some TensorFlow code
• Supports parallel processing (makes training larger datasets more efficient)

# Building a Simple Model in TensorFlow

## What are we building?

We will be building a very simple TensorFlow model that adds two numbers together. This will seem trivial, but it’s an uncomplicated way to introduce yourself to TensorFlow and making models.

## How to build it

1. Open PyCharm and create a new project
2. Save the project with a .py extension. For example, `addition.py`

Now, time to code.

``````import os
import tensorflow as tf``````

First, we import the things we will need into our Python project. We add the `as tf` when we import TensorFlow so you don’t have to type the entire name out each time you want to access it.

``os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'``

Then, we add this line to prevent TensorFlow from flooding the screen with a bunch of log messages, like it normally does. It makes the output easier to read.

``````X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")``````

Here we define the X and Y input nodes. We are defining them as placeholder nodes that will get a different value each time we run it. In the parentheses, we set their data types and give them names.

``addition = tf.add(X, Y, name="addition")``

This node does the actual adding. We are basically asking it to add X and Y in this line. We also must give it a name.

``````# create the session
with tf.Session() as session:

result = session.run(addition, feed_dict={X: [5], Y: [3]})
print(result)

# output: [ 8. ]``````

We need to create a TensorFlow session to run the addition operation on X and Y. We input values for X and Y with the `feed_dict` parameter.

We are feeding the model arrays because TensorFlow always works with tensors, or multi-dimensional arrays.

You can now run your code.

``````import os
import tensorflow as tf

# Turn off TensorFlow warning messages in program output    (limits # of log messages)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# Define the nodes
X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")

# Create the session
with tf.Session() as session:

result = session.run(addition, feed_dict={X: [1, 5, 6], Y: [3, 4, 5]})

print(result)``````

Since the inputs are arrays, you can put multiple numbers for X and Y. The output for the above should be` [ 4. 9. 11. ]`

This simple program may have seemed over-complicated just to do addition, but it taught you the basic structure of creating and running a TensorFlow model:

1. Define a model
2. Create a session
3. Pass in data
4. Send it to TensorFlow for execution

# How to Install TensorFlow

TensorFlow is a tool to build machine learning models. Learn about it here.

Before we install TensorFlow, we have to install a programming language to use to write code in TensorFlow. TensorFlow works with multiple languages including JavaScript and Python. In this tutorial, we’re going to be downloading Python. How to install Python.

We will be installing TensorFlow with pip so make sure you have pip installed on your computer. You can check if you have pip installed by typing this line in Terminal:

``pip3 --version``

It should install by itself when you install Python so you shouldn’t have to worry about that.

Now we will install TensorFlow with the command below. It will install in your `\$HOME` directory. If you don’t want this, find a tutorial that shows you how to use a virtual environment. I personally haven’t had luck with this strategy.

``pip3 install --user --upgrade tensorflow ``

Verify the install with this line:

``python3 -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"``

# What is TensorFlow?

TensorFlow is a software framework for building and deploying machine learning models.

It was developed by Google for their personal use, but they ended up deciding to make it public in late 2015. The first stable version came in 2017.

TensorFlow is open source. This means it is released under a license which allows users to do basically whatever they want with the software.

TensorFlow is low-level and can take quite a few lines of code to build stuff. Keras is a popular, high-level TensorFlow wrapper for building neural networks with much fewer lines of code.

Keras simplifies the process of coding common tasks in TensorFlow, making it much easier and quicker. Read a little bit more about Keras here.

# Creating a Simple Machine Learning iOS App

### Description of the app

This app will take a picture of an object and tell you what that object is. For example, if you take a picture of a hot dog, it will tell you it’s a hot dog. If you take a picture of a Golden Retriever, it will tell you it’s a Golden Retriever.

### What is Core ML?

Core ML allows you to integrate trained machine learning models into your iOS app. A trained model is the result of a machine learning algorithm being applied to a set of training data. Core ML models are pre-trained, meaning you don’t have to train them because they’ve already been trained. The models are static which means you are not able to use the data from your app to train the model any further.

We will be using the Inception v3 model for our app. It has been trained to recognize over 1000 categories of images including animals, foods, trees, vehicles, and more.

This tutorial assumes you have a basic understanding of iOS app development.

## Creating the app

### Step One: Create a new Xcode Project

Xcode is the software used to create iOS apps. If you haven’t already, you must download and set Xcode up. There are plenty of tutorials on how to do this.

1. Afterwards, create a new Single View App, name it, and save it wherever you like.

### Step Two: Incorporate the Inception v3 Model into your Project

1. Go to https://developer.apple.com/machine-learning/build-run-models/
2. Scroll down to the CoreML Models and find “Inception v3”
5. Make sure the check box “Copy Items if needed” is checked and press Finish (It might take a while for the model to incorporate into Xcode so be patient at first)

### Step Three: Setup up your ViewController.swift

1. Open up the ViewController.swift file in your Xcode project file structure.
``````import UIKit
import CoreML
import Vision

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

}

}``````

The `import CoreML` statement allows you to use CoreML in your code. The `import Vision` statement makes it easier to process images.

The `UIImagePickerControllerDelegate` lets you access your phone’s camera, and the `UINavigationControllerDelegate` lets you navigate back and forth between screens.

### Step Four: Set up your Main.storyboard

1. Click the yellow button on the top-left of the View Controller box
2. Click the Editor tab in the menu bar of your Mac
3. Find “Embed In” and hover on it and choose “Navigation Controller”
4. A new gray box should appear that says “Navigation Controller.” Slide it over to the left to get it out of the way.
1. In the top right corner, press the Library button. Type in “Bar Button Item” and drag the Bar Button Item into the top right of the View Controller
2. Change the Bar Button Item’s icon to a camera icon
3. Press the Library button again. Type “Image View” and drag an Image View into the View Controller. Resize it fill the entire box.
4. Click the check box that says “Use Safe Area Layout Guides.”
1. Connect the Bar Button Item into your ViewController.swift as an IBAction with a name like “cameraTapped”
2. Connect the Image View into the file as an IBOutlet with a name like “imageView”

### Step Five: Connect Storyboard items to variables in code

``````import UIKit
import CoreML
import Vision

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var imageView: UIImageView!

}

@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
}

}``````

### Step Six: Add an Image Picker to the View Controller

``````import UIKit
import CoreML
import Vision

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var imageView: UIImageView!

let imagePicker = UIImagePickerController()

imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.image = userPickedImage
}
imagePicker.dismiss(animated: true, completion: nil)
}

@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
present(imagePicker, animated: true, completion: nil)
}

}``````

All this code basically allows you to have access to the phone’s camera to take pictures through the app. When you press the Bar Button Item that we set up in your app, it will present a view that lets you take a picture.

### Step Seven: Get permission to use the camera

1. Go to the Info.plist file
3. For the Key, select the “Privacy – Camera Usage Description”
4. For the Value, write something like “We need to use your camera”

Now the camera functionality in your app should be fully functional.

### Step Eight: Convert the Image to a CIImage

Add the `guard` statement to your `imagePickerController` function.

``````     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.image = userPickedImage
guard let ciimage = CIImage(image: userPickedImage) else {
fatalError("Could not convert to CIImage")
}
}
imagePicker.dismiss(animated: true, completion: nil)
}``````

`CIImage` is a special type of image that allows us to use the Vision and CoreML frameworks in order to get an interpretation from the model.

### Step Nine: Create the Function that Interprets Images with Inceptionv3 Model

Now we’re going to add the function that will actually look at the image, analyze it, and classify it as something.

Add this `detect` function right above your `cameraTapped` IBAction

``````func detect(image: CIImage) {
guard let model = try? VNCoreMLModel(for: Inceptionv3().model) else {
}

let request = VNCoreMLRequest(model: model) { (request, error) in
guard let results = request.results as? [VNClassificationObservation] else {
fatalError("Model failed to process image")
}

if let firstResult = results.first {
}
}

let handler = VNImageRequestHandler(ciImage: image)

do {
try handler.perform([request])
}
catch {
print(error)
}

}

@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
present(imagePicker, animated: true, completion: nil)
}``````

First, we created a variable called `model` which was a reference to the Inception v3 model that we could access in our code.

Then, we created the `request` that basically uses the model to process input and give out relevant results.

Whatever result the request comes up with (`results.first`) will be displayed on the user’s screen in the navigation title (`self.navigationItem.title`).

Then, we put the `CIImage` that is a parameter of the `detect` function into the `VNImageRequestHandler` variable so we can use it in the model.

Last, we performed the request on the handler (A.K.A the image).

### Step Ten: Call the detect function

Last but not least, we have to call the `detect` function in our code so that it actually runs on the image.

Go back to your `imagePickerController` function.

``````func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

if let userPickedImage = info[.originalImage] as? UIImage {
imageView.image = userPickedImage

guard let ciimage = CIImage(image: userPickedImage) else {
fatalError("Could not convert to CIImage")
}

detect(image: ciimage)

}

imagePicker.dismiss(animated: true, completion: nil)

}``````

The only line that we added is `detect(image: ciimage)`. This line calls the `detect` function and the `ciimage` variable defined right above it is the `image` parameter. Whatever image you use for this parameter is the image that will be analyzed and classified by the model.

## The Result

The app is now completely done and ready to run.

Remember: It is not even close to 100% accurate. It only knows about 1000 objects and it’s not a very sophisticated AI. But it’s a cool beginner project and it’s fun to see when it works.

Here’s an example. I took the photo of my dog Blue and it correctly identified him as a Silky Terrier.

Here are some more examples of objects that it recognized.

Thank you so much for reading and I hope you enjoyed this project. Come back soon for more!

# What is Deep Learning?

Deep learning (DL) is a method of Machine Learning which teaches machines with as little human effort as possible. They learn from example, like humans, by digesting large amounts of data.

Unlike other other types of Machine Learning that require you to outline features to look for in the data, Deep Learning models deal directly with the data– no human intervention!

The downside to this is Deep Learning models require more data and higher computing power. Also, it takes longer to train DL models.

One of the benefits to DL models is they are more accurate. In addition, they are the best models if you don’t know the features you’re looking for in your data.

Deep Learning is accomplished using neural networks. A neural network is a computing model which is structured based on the structure of a brain.

Deep Learning is used in driverless cars, detecting cancer cells, improving worker safety around heavy machinery, in home assistants, and many other situations nowadays.

# What is Machine Learning?

Machine Learning (ML) provides Artificial Intelligence machines the ability to “learn.” This is achieved by using algorithms that discover patterns and generate insights from data.

Unlike traditional programming, you’re not giving the computer detailed instructions on what to do. Instead, you give the computer data and tools that allow it to study and solve the problem without directive. The computer remembers and adapts to new information each time it is used, also known as learning.

Use ML when you can’t tell the computer explicitly what to do in each situation.

## Types of Machine Learning

• Supervised – You show the machine connection between variables and outcomes. It’s like a knowledgeable tutor.
• Unsupervised – The machine learns through lots of observation and trial and error. Unlike supervised learning, you’re not showing the machine the correct answer. The more data you give it, the more accurate the machine will become.
• Semi-supervised – Mix between supervised and unsupervised.
• Reinforcement – The machine gets a reward for performing a desired action.