Skip to content

TensorFlow

In this guide, you will learn how to use TensorFlow in a Jupyter notebook using Rafay's MLOps platform based on Kubeflow. TensorFlow is an open-source software library developed by Google that allows you to build and train machine learning models.

Tired of the complexities of installing TensorFlow in Jupyter Notebook? Try Rafay's MLOps for free and to set up your data science environment effortlessly!


Launch Notebook

In this step, we will create and launch a Jupyter notebook with the TensorFlow image.

  • Login into your Kubeflow workspace
  • Click on the Notebooks menu
  • Create New Notebook and provide a friendly name
  • For the Image, ensure you select either "jupyter-tensorflow-full" or "jupyter-tensorflow-cuda-full"
  • Use the defaults for everything else for now
  • Click on launch to start the notebook

New TensorFlow Notebook

Note

Since we will be performing a very simple test, we will not require access to a GPU.

It can take a few minutes for the notebook to be ready (esp. if the container image has not been already downloaded and cached on the cluster). The image below shows what a successful notebook creation should look like.

Ready TensorFlow Notebook


Connect to Notebook

Click on Connect to connect to your notebook. This will launch a new web browser tab and you will have access to the Launcher menu.

TensorFlow Notebook

To verify if TensorFlow and related packages are preinstalled, you can type the following command in a cell and run.

pip list | grep "tensor"

This should print results similar to the one below

tensorboard                   2.13.0
tensorboard-data-server       0.7.2
tensorflow                    2.13.0
tensorflow-estimator          2.13.0
tensorflow-io-gcs-filesystem  0.34.0
Note: you may need to restart the kernel to use updated packages.

Add TensorFlow Code

In this guide, we are going to use an extremely simple Python program that adds two numbers using TensorFlow. In your new notebook,

  • Click on "Python 3" notebook
  • Copy/paste the following code
import tensorflow as tf

a = tf.constant(2)
b = tf.constant(3)

c = tf.add(a, b)

# Eager execution is enabled by default in TensorFlow 2.x, so you can just print the result
print(c.numpy())  # Or simply print(c)

This program creates two constants (i.e. a and b). It then adds them together using TensorFlow’s add function. It then prints the result to the console. To run the program, click on the Run button in the notebook. As you can see from the image below, you should see the result (5) printed to the console.

TensorFlow Notebook Success


Next Steps

By following these simple steps, you can start building and training machine learning models using TensorFlow in a Jupyter Notebook. Here are some resources you can use to try advanced scenarios: