23 The steps below provide guidance on migrating numpy code to Tensorflow. This is useful when wanting to run optimization problems while making use of parallelization & GPUs.
Import TensorFlow
import tensorflow as tf
Convert Variables to TensorFlow Variables
Only floats should be converted to Tensorflow variables. Ints work well with native tensorflow types
# Numpy
x = np.array([1.0, 2.0, 3.0])
# TensorFlow
x = tf.Variable([1.0, 2.0, 3.0])
Convert Operations to TensorFlow Operations
Replace numpy operations with Tensorflow operations.
# Numpy
y = np.square(x)
# TensorFlow
y = tf.square(x)
Use TensorFlow Optimizers
Use TensorFlow optimizers instead of custom optimization algorithms.
# Numpy
learning_rate = 0.01
for i in range(1000):
gradients = compute_gradients() # Custom gradient computation
x.assign(x - learning_rate * gradients)
# TensorFlow
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op = optimizer.minimize(loss) # Assuming you have a loss function defined
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train_op)
Compute Loss
Define your loss function using TensorFlow operations.
# Numpy
def compute_loss(x, y):
return np.sum((x - y)**2)
# TensorFlow
def compute_loss(x, y):
return tf.reduce_sum(tf.square(x - y))
Run Operations in a Session
TensorFlow operations need to be run within a session.
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Run your computations here
Ensure GPU Usage
To make use of GPUs, ensure you have the necessary drivers installed and TensorFlow is built with GPU support. You can specify the device explicitly using with tf.device('/gpu:0')
.
# Checking GPU availibility
tf.config.list_physical_devices('GPU')
# Ensuring the GPU is used
with tf.device('/gpu:0'):
x = tf.Variable([1.0, 2.0, 3.0])
y = tf.square(x)