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

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>
主站蜘蛛池模板: 瑞金市| 泰顺县| 屯门区| 南雄市| 长兴县| 庄河市| 阳谷县| 平南县| 禄丰县| 南澳县| 普宁市| 噶尔县| 富宁县| 格尔木市| 建德市| 绍兴县| 麟游县| 泸定县| 益阳市| 东至县| 乾安县| 泽普县| 曲松县| 揭阳市| 兴安盟| 绍兴市| 宜君县| 福清市| 密山市| 威信县| 黄山市| 叶城县| 凌海市| 六盘水市| 句容市| 南川市| 西乌| 沂南县| 若尔盖县| 商洛市| 天峻县|