官术网_书友最值得收藏!

Code warm-up - Hello TensorFlow

As a customary tradition when learning any new programming language, library, or platform, let's write the simple Hello TensorFlow code as a warm-up exercise before we pe deeper.

We assume that you have already installed TensorFlow. If you have not, refer to the TensorFlow installation guide at https://www.tensorflow.org/install/ for detailed instructions to install TensorFlow.

Open the file ch-01_TensorFlow_101.ipynb in Jupyter Notebook to follow and run the code as you study the text.

  1. Import the TensorFlow Library with the following code:
import tensorflow as tf
  1. Get a TensorFlow session. TensorFlow offers two kinds of sessions: Session() and InteractiveSession(). We will create an interactive session with the following code:
tfs = tf.InteractiveSession()

The only difference between Session() and InteractiveSession() is that the session created with InteractiveSession() becomes the default session. Thus, we do not need to specify the session context to execute the session-related command later. For example, say that we have a session object, tfs, and a constant object, hello. If tfs is an InteractiveSession() object, then we can evaluate hello with the code hello.eval(). If tfs is a Session() object, then we have to use either tfs.hello.eval() or a with block. The most common practice is to use the with block, which will be shown later in this chapter.

  1. Define a TensorFlow constant, hello:
hello = tf.constant("Hello TensorFlow !!")
  1. Execute the constant in a TensorFlow session and print the output:
print(tfs.run(hello))
  1. You will get the following output:
'Hello TensorFlow !!'

Now that you have written and executed the first two lines of code with TensorFlow, let's look at the basic ingredients of TensorFlow.

主站蜘蛛池模板: 龙岩市| 隆德县| 徐闻县| 离岛区| 揭西县| 莱芜市| 宁晋县| 商南县| 郧西县| 军事| 电白县| 黎平县| 铁岭县| 南木林县| 平远县| 亚东县| 连城县| 鹤山市| 南华县| 嘉定区| 霍山县| 钟祥市| 丰宁| 常熟市| 卢氏县| 西昌市| 开远市| 江门市| 洪洞县| 巩义市| 岱山县| 西丰县| 东港市| 宜兰市| 阳朔县| 桦南县| 凉城县| 孝感市| 锡林浩特市| 惠水县| 冀州市|