Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Conceptual Analysis of Tensor and variable in TensorFlow Neural Network

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the conceptual analysis of tensors and variables in TensorFlow neural network, which is very detailed and has a certain reference value. Interested friends must read it!

I. definition of tensor

Tensor: the tensor of TensorFlow is an n-dimensional array and the type is tf.Tensor.

Scalar: a number (tensor of order 0)

Vectors: one-dimensional array (first-order tensor)

Matrix: two-dimensional array (second-order tensor)

Tensor attributes 1. Types of tensors

# create a constant tensor a = tf.constant (3.0) print (a)

2. The order of the tensor

3. Tensor instruction 1, constant tensor (ordinary) # create constant tensor a = tf.constant (3.0) print (a)

2. Tensor array 1, fixed tensor array (0) # create tensor array # 0: array_0 = tf.zeros (shape= [3 Magazine 3]) # 3 array (0)

2. Fixed tensor array (1) # 1: array_1 = tf.ones (shape= [3prime3]) # 3i3array (1) 3, random tensor array # random: array_random = tf.random_normal (shape= [2mae3], mean=1.75, stddev=0.12) # 2pion3 array mean (1.75) standard deviation

3. View the tensor value

View tensor value: tensor .eval ()

# session (view tensor) with tf.Session () as sess: print (a.eval ()) print (array_0.eval ()) print (array_1.eval ()) print (array_random.eval ())

4. Tensor type change # modify tensor type array_0 = tf.cast (array_0, tf.int32)

5. Tensor shape change

Note: it belongs to the dynamic change tensor and requires a fixed number of tensor elements.

# modify the tensor shape array_random = tf.reshape (array_random, shape= [3prime2])

Before modification:

After modification:

Code # tensor (create and modify) import tensorflow as tf# create tensor def Create_Tensor (): # create constant tensor a = tf.constant (3.0) print (a) # create tensor array # 0: array_0 = tf.zeros (shape= [3,3]) # 3arrays (0) # 1: array_1 = tf.ones (shape= [3) 3]) # 3: array (1) # Random: array_random = tf.random_normal (shape= [2,3], mean=1.75 Stddev=0.12) # 2: 3 Array mean (1.75) Standard deviation # session (View Tensor) with tf.Session () as sess: print (a.eval ()) print (array_0.eval ()) print (array_1.eval ()) print (array_random.eval ()) ) # modify the tensor def Modify_Tensor (): global array_0 Array_random print ('modified:') # modify the tensor type array_0 = tf.cast (array_0, tf.int32) # modify the tensor shape array_random = tf.reshape (array_random, shape= [3 2]) # session (view tensor) with tf.Session () as sess: print (array_0.eval ()) print (array_random.eval ()) # create tensor Create_Tensor () # modify tensor Modify_Tensor () 4, variable 1, define variable # define variable a = tf.Variable (initial_value=2) b = tf.Variable (initial_value=4) c = tf.add (ajib) 2, initialize variable

The variable of TensorFlow must be initialized, otherwise an error will be reported.

# initialization variable init = tf.global_variables_initializer () 3, Open session (execution) # Open session with tf.Session () as sess: sess.run (init) print (sess.run (c))

Code # variable import tensorflow as tf # defines variable a = tf.Variable (initial_value=2) b = tf.Variable (initial_value=4) c = tf.add (Azov b) # initialization variable init = tf.global_variables_initializer () # Open session with tf.Session () as sess: sess.run (init) print (sess.run (c)) all the contents of the article "Conceptual Analysis of Tensor and variables in TensorFlow Neural Network" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report