9951 explained code solutions for 126 technologies


python-tensorflowHow do I write a Python TensorFlow example code?


Example code

import tensorflow as tf

# Create a constant op
# This op is added as a node to the default graph
hello = tf.constant("Hello, TensorFlow!")

# start a TF session
sess = tf.Session()

# run the op and get result
print(sess.run(hello))

Output example

b'Hello, TensorFlow!'

This example code creates a constant op and adds it to the default graph. It then starts a TensorFlow session and runs the op to get the result.

The code consists of four parts:

  1. import tensorflow as tf: This imports the TensorFlow library as the tf object.

  2. hello = tf.constant("Hello, TensorFlow!"): This creates a constant op with the string "Hello, TensorFlow!" and assigns it to the hello variable.

  3. sess = tf.Session(): This creates a TensorFlow session and assigns it to the sess variable.

  4. print(sess.run(hello)): This runs the hello op in the sess session and prints the result.

For more information on TensorFlow and how to write example code, please see the TensorFlow documentation.

Edit this code on GitHub