# Simple neural network with TensorFlow 1.14

import tensorflow as tf
import numpy as np

eta = 0.1 # learning rate (for "gradient descent algorithm")
maximum_error = 0.01 # maximum "mean squared error"

model = tf.keras.Sequential()

model.add(tf.keras.layers.Dense(5, activation=tf.sigmoid, input_shape=(4,))) # 5 neurons (layer k=1), 4 entries
model.add(tf.keras.layers.Dense(6, activation=tf.sigmoid))                   # 6 neurons (layer k=2)
model.add(tf.keras.layers.Dense(3, activation=tf.sigmoid))                   # 3 neurons (layer k=3)

model.compile(optimizer=tf.train.GradientDescentOptimizer(eta), loss=tf.losses.mean_squared_error)

# Input data
xx = np.array([[5.0, 1.0, 0.0, 3.0]])
print("INPUT DATA")
print(xx)

# Desired output data with those input data
dd = np.array([[1.0, 0.0, 0.5]])
print("DESIRED OUTPUT DATA")
print(dd)

# Train (fit) the neural network (model)
print("TRAINING NEURAL NETWORK...")
mse = model.evaluate(x=xx, y=dd)
iterations = 0
while mse > maximum_error:
  model.fit(x=xx, y=dd)
  mse = model.evaluate(x=xx, y=dd)
  iterations = iterations + 1
print("NEURAL NETWORK TRAINED")

print("Error : [" + str(mse) + "]")
print("Iterations : [" + str(iterations) + "]")

# Calculate (predict) the output data
yy = model.predict(xx)
print("OUTPUT DATA")
print(yy)
      

// Generated by xformulas.net on 20190917_122136