TensorFlow examples - AshokBhat/ml GitHub Wiki

Key objects and routines

Function Description
model.compile Configures the model for training
model.fit Trains the model for a fixed number of epochs (iterations on a dataset)
model.to_json JSON string of network configuration
model.to_yaml YAML string of network configuration
model.summary Summary of the model

Examples

Example

  • Math equation - y = 3x+1
  • Neural network - 1 layer having 1 neuron and input shape is just 1 value
import tensorflow as tf
import numpy as np
from tensorflow import keras
model = tf.keras.Sequential()
neuron_layer = keras.layers.Dense(units=1, input_shape=[1])])
model.add(neuron_layer)
model.compile(optimizer='sgd', loss='mean_squared_error')
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)
model.fit(xs, ys, epochs=500)
print(model.predict([10.0]))