Playing with neurons

In [1]:
import matplotlib.pyplot as plt
import numpy as np
import keras as kr
Using TensorFlow backend.
In [2]:
plt.rcParams['figure.figsize'] = (10, 10)
In [3]:
x = np.linspace(-10.0, 10.0, 2000)

y_l = (3.0 * x) + 5.0
y_p = (x * x) - 41.0
y_s = 10.0 * np.sin(2.0 * x)
In [4]:
plt.plot(x, y_l, label='$3x + 5$')
plt.plot(x, y_p, label='$x^2 - 41$')
plt.plot(x, y_s, label='$10 \sin(2x)$')

plt.legend()
Out[4]:
<matplotlib.legend.Legend at 0x28f725e6d48>
In [5]:
#y = y_l
y = y_p
#y = y_s

# Create a new neural network.
m = kr.models.Sequential()

# Add neurons.
#m.add(kr.layers.Dense(1, input_dim=1, activation="linear"))

# Add neurons
m.add(kr.layers.Dense(10, input_dim=1, activation="tanh"))
m.add(kr.layers.Dense(10, activation="linear"))
m.add(kr.layers.Dense(1, activation='linear'))

# Compile the model.
m.compile(loss="mean_squared_error", optimizer="adam")

# Train the model.
m.fit(x, y, epochs=15, batch_size=10)

# Run each x value through the neural network.
p = m.predict(x)

# Plot the values.
plt.plot(x, p, 'r-', label='Prediction')
plt.plot(x, y, 'k:', label='Original')
plt.legend()
WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:986: The name tf.assign_add is deprecated. Please use tf.compat.v1.assign_add instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:973: The name tf.assign is deprecated. Please use tf.compat.v1.assign instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:2741: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

Epoch 1/15
WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:174: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:181: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:190: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:199: The name tf.is_variable_initialized is deprecated. Please use tf.compat.v1.is_variable_initialized instead.

WARNING:tensorflow:From C:\Users\mclou\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:206: The name tf.variables_initializer is deprecated. Please use tf.compat.v1.variables_initializer instead.

2000/2000 [==============================] - 1s 537us/step - loss: 940.2609
Epoch 2/15
2000/2000 [==============================] - 0s 142us/step - loss: 889.8701
Epoch 3/15
2000/2000 [==============================] - 0s 140us/step - loss: 733.4230
Epoch 4/15
2000/2000 [==============================] - 0s 126us/step - loss: 542.5035
Epoch 5/15
2000/2000 [==============================] - 0s 135us/step - loss: 434.7237
Epoch 6/15
2000/2000 [==============================] - 0s 113us/step - loss: 373.4142
Epoch 7/15
2000/2000 [==============================] - 0s 115us/step - loss: 325.7642
Epoch 8/15
2000/2000 [==============================] - 0s 115us/step - loss: 284.2620
Epoch 9/15
2000/2000 [==============================] - 0s 116us/step - loss: 245.2698
Epoch 10/15
2000/2000 [==============================] - 0s 149us/step - loss: 208.8869
Epoch 11/15
2000/2000 [==============================] - 0s 116us/step - loss: 176.2597
Epoch 12/15
2000/2000 [==============================] - 0s 122us/step - loss: 146.8940
Epoch 13/15
2000/2000 [==============================] - 0s 113us/step - loss: 121.4796
Epoch 14/15
2000/2000 [==============================] - 0s 139us/step - loss: 99.3574
Epoch 15/15
2000/2000 [==============================] - 0s 118us/step - loss: 81.1958
Out[5]:
<matplotlib.legend.Legend at 0x28f73bd88c8>

End