Skip to content

TensorFlow model training code for testing

This page assumes that you've followed the instructions to install TensorFlow using conda and successfully installed TF in your conda environment. Below we provide more TF model training code for you to fully test your installation. Remember to log onto dev-amd20-v100.

All the code should be typed in (or copy-paste) to an interactive python interpreter, after running the first four lines below from your terminal. After you are done and have quit the python session, remember to deactivate your conda environment via conda deactivate.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
export PATH=/mnt/home/user123/anaconda3/bin:$PATH
conda activate tf_gpu_Feb2023
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib/:/lib64/:$CONDA_PREFIX/lib/:$CONDA_PREFIX/lib/python3.9/site-packages/tensorrt_libs
export XLA_FLAGS=--xla_gpu_cuda_data_dir=$CONDA_PREFIX/lib

python

# Here insert your python code after >>> the prompt of the interactive Python interpreter

conda deactivate

Code 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import tensorflow as tf
import numpy as np

# Generate random data
x = np.random.rand(100, 10)
y = np.random.randint(0, 2, size=(100,))

# Define the model architecture
model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(64, activation='relu', input_shape=(10,)),
  tf.keras.layers.Dense(32, activation='relu'),
  tf.keras.layers.Dense(1, activation='sigmoid')
])

# Compile the model
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# Train the model
model.fit(x, y, epochs=10, validation_split=0.2)

# Evaluate the model on test data
test_loss, test_acc = model.evaluate(x, y, verbose=2)
print(f'Test accuracy: {test_acc}')

Code 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

Code 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import tensorflow as tf
from tensorflow.keras.datasets import boston_housing

(x_train, y_train), (x_test, y_test) = boston_housing.load_data()

model = tf.keras.Sequential([
  tf.keras.layers.Dense(64, activation='relu', input_shape=(13,)),
  tf.keras.layers.Dense(64, activation='relu'),
  tf.keras.layers.Dense(1)
])

model.compile(optimizer='adam', loss='mse', metrics=['mae'])

history = model.fit(x_train, y_train, epochs=10, batch_size=16, validation_split=0.2)

test_loss, test_mae = model.evaluate(x_test, y_test)

print(f'Test MAE: {test_mae:.2f}')

Special thanks to Siddak Marwaha who provided the above code.