Introduction to TensorFlow in Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School
# Import tensorflow
import tensorflow as tf
# Define a sequential model
model = tf.keras.Sequential()
# Define the hidden layer
model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(784,)))
# Define the output layer
model.add(tf.keras.layers.Dense(4, activation='softmax'))
# Compile model
model.compile('adam', loss='categorical_crossentropy')
# Train model
model.fit(image_features, image_labels)
features
labels
batch_size
epochs
validation_split
# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
# Recomile the model with the accuracy metric
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
# Evaluate the test set
model.evaluate(test)
Introduction to TensorFlow in Python