Neurons in keras


Keras



Imports


In [1]:
# Neural networks.
import tensorflow.keras as kr

# Numerical arrays
import numpy as np

# Data frames.
import pandas as pd

# Plotting
import matplotlib.pyplot as plt
In [2]:
# Plot style.
plt.style.use("ggplot")

# Plot size.
plt.rcParams['figure.figsize'] = [14, 8]


Single linear neuron


Single linear neuron


$$ y = x $$


In [3]:
# Create a new neural network.
m = kr.models.Sequential()

# Add a single neuron in a single layer, initialised with weight 1 and bias 0.
m.add(kr.layers.Dense(1, input_dim=1, activation="linear", kernel_initializer='ones', bias_initializer='zeros'))

# Compile the model.
m.compile(loss="mean_squared_error", optimizer="sgd")
In [4]:
# Create some input values.
x = np.arange(-10.0, 10.1, 1.0)

# Run each x value through the neural network.
y = m.predict(x)
In [5]:
# Have a look at x.
x
Out[5]:
array([-10.,  -9.,  -8.,  -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,
         1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])
In [6]:
# Here are the corresponding outputs.
y
Out[6]:
array([[-10.],
       [ -9.],
       [ -8.],
       [ -7.],
       [ -6.],
       [ -5.],
       [ -4.],
       [ -3.],
       [ -2.],
       [ -1.],
       [  0.],
       [  1.],
       [  2.],
       [  3.],
       [  4.],
       [  5.],
       [  6.],
       [  7.],
       [  8.],
       [  9.],
       [ 10.]], dtype=float32)
In [7]:
# Plot the values.
plt.plot(x, y, 'k.');


Single linear neuron with different weight and bias



$$ y = 2x + 1 $$


In [8]:
# Create a new neural network.
m = kr.models.Sequential()

# Add a single neuron in a single layer, initialised with weight 2 and bias 1.
m.add(kr.layers.Dense(1, input_dim=1, activation="linear", kernel_initializer=kr.initializers.Constant(value=2.0), bias_initializer=kr.initializers.Constant(value=1.0)))

# Compile the model.
m.compile(loss="mean_squared_error", optimizer="sgd")
In [9]:
# Create some input values.
x = np.arange(-10.0, 10.1, 1.0)

# Run each x value through the neural network.
y = m.predict(x)
In [10]:
# Plot the values.
plt.plot(x, y, 'k.');


Two neurons


Two linear neurons


In [11]:
# Create a new neural network.
m = kr.models.Sequential()

# Add a two neurons in a single layer.
m.add(kr.layers.Dense(2, input_dim=1, activation="linear"))

# Add a single neuron in a single layer, initialised with weight 1 and bias 0.
m.add(kr.layers.Dense(1, activation="linear", kernel_initializer=kr.initializers.Constant(value=1), bias_initializer=kr.initializers.Constant(value=0)))

# Set the weight/bias of the two neurons.
m.layers[0].set_weights([np.matrix([2.0, 3.0]), np.array([-5.0, -3.0])])

# Compile the model.
m.compile(loss="mean_squared_error", optimizer="sgd")
In [12]:
# Create some input values.
x = np.arange(-10.0, 10.1, 1.0)

# Run each x value through the neural network.
y = m.predict(x)
In [13]:
# Look at the outputs.
y
Out[13]:
array([[-58.],
       [-53.],
       [-48.],
       [-43.],
       [-38.],
       [-33.],
       [-28.],
       [-23.],
       [-18.],
       [-13.],
       [ -8.],
       [ -3.],
       [  2.],
       [  7.],
       [ 12.],
       [ 17.],
       [ 22.],
       [ 27.],
       [ 32.],
       [ 37.],
       [ 42.]], dtype=float32)
In [14]:
# Plot the values.
plt.plot(x, y, 'k.');
In [15]:
# Calculating the output using numpy arrays.
neuron1 =  2.0 * x - 5.0
neuron2 =  3.0 * x - 3.0
neuron3 = neuron1 + neuron2
neuron3
Out[15]:
array([-58., -53., -48., -43., -38., -33., -28., -23., -18., -13.,  -8.,
        -3.,   2.,   7.,  12.,  17.,  22.,  27.,  32.,  37.,  42.])


Sigmoid activation


Single neuron


$$ f(x) \ = \ S(x)\ = \ \frac{1}{1 + e^{-x}} \ = \ \frac{e^x}{e^x + 1} $$


In [16]:
# Create a new neural network.
m = kr.models.Sequential()

# Add a single neuron in a single layer, initialised with weight 1 and bias 0, with sigmoid activation.
m.add(kr.layers.Dense(1, input_dim=1, activation="sigmoid", kernel_initializer=kr.initializers.Constant(value=1.0), bias_initializer=kr.initializers.Constant(value=0.0)))

# Compile the model.
m.compile(loss="mean_squared_error", optimizer="sgd")
In [17]:
# Create some input values.
x = np.arange(-10.0, 10.1, 1.0)

# Run each x value through the neural network.
y = m.predict(x)
In [18]:
# Plot the values.
plt.plot(x, y, 'k.');


Sequential Dense layers


Dense neural network


End