書名: TensorFlow 2.0 Quick Start Guide作者名: Tony Holdroyd本章字?jǐn)?shù): 131字更新時(shí)間: 2021-06-24 16:02:03
Declaring eager variables
The way to declare a TensorFlow eager variable is as follows:
t0 = 24 # python variable
t1 = tf.Variable(42) # rank 0 tensor
t2 = tf.Variable([ [ [0., 1., 2.], [3., 4., 5.] ], [ [6., 7., 8.], [9., 10., 11.] ] ]) #rank 3 tensor
t0, t1, t2
The output will be as follows:
(24, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=42>, <tf.Variable 'Variable:0' shape=(2, 2, 3) dtype=float32, numpy= array([[[ 0., 1., 2.], [ 3., 4., 5.]], [[ 6., 7., 8.], [ 9., 10., 11.]]], dtype=float32)>)
TensorFlow will infer the datatype, defaulting to tf.float32 for floats and tf.int32 for integers (see the preceding examples).
Alternatively, the datatype can be explicitly specified, as here:
f64 = tf.Variable(89, dtype = tf.float64)
f64.dtype
TensorFlow has a large number of built-in datatypes.
Examples include those seen previously, tf.int16, tf.complex64, and tf.string. See https://www.tensorflow.org/api_docs/python/tf/dtypes/DType. To reassign a variable, use var.assign(), as here:
f1 = tf.Variable(89.)
f1
# <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=89.0>
f1.assign(98.)
f1
# <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=98.0>
推薦閱讀
- 電氣自動化專業(yè)英語(第3版)
- Dreamweaver CS3+Flash CS3+Fireworks CS3創(chuàng)意網(wǎng)站構(gòu)建實(shí)例詳解
- CSS全程指南
- 機(jī)器人創(chuàng)新實(shí)訓(xùn)教程
- AI 3.0
- Troubleshooting OpenVPN
- 單片機(jī)技術(shù)項(xiàng)目化原理與實(shí)訓(xùn)
- 貫通開源Web圖形與報(bào)表技術(shù)全集
- MongoDB 4 Quick Start Guide
- 傳感器原理與工程應(yīng)用
- 步步驚“芯”
- 新一代人工智能與語音識別
- 案例解說虛擬儀器典型控制應(yīng)用
- 華人動畫師的法蘭西印象
- 博弈論與無線傳感器網(wǎng)絡(luò)安全