tensorflow study notes (ii) a constant, a variable, a placeholder session

Constants, variables, placeholders, sessions are basic things tensorflow programming is most commonly used, as defined in the variable tensorflow, constants are tensor (tensor) type.

Constant tf.constant ()

Tensorflow variable defined, are constants Tensor (tensor) will not change the type commonly used in amount during operation, such as linear regression Y = w * X + b, a series of known (X, Y), by gradient find w and b decrease, and the X-Y values ​​will not change the program to run, changing only w b and reduce the error to the true value, it is used to represent a constant input and output.

Declare a scalar constants:

t_1 = tf.constant(5)

Declare a constant vector:

t_2 = tf.constant([2, 3, 5])

Variable tf.Variable ()

To constantly adjusted linear regression w, b do fit, w and b must declare a variable, so the variable used to represent parameters in the model.

A statement M rows and N columns, all zeros variables:

b_1 = tf.Variable (tf.zeros ([M, N], tf.float32)) # tf.zeros create a full amount of 0, float32 digital type, then tf.Variable () to turn it into a variable

A statement as a normal distribution is the mean 2 (default = 0.0) standard deviation is 4 (default is 1.0) of 2 rows 3 tensor:

w_1 = tf.Variable(tf.random_nomarl([2, 3], mean=2.0, stddev=4, seed=2)

Session Session ()

As main () function is the same, the session entry is tensorflow program, tf procedures generally define the relationship between the node and the node (calculation), and then the calculation result in the session automatically according to a defined operation.

tensorflow TF AS Import 
A = tf.constant ([. 1, 2]) 
B = tf.constant ([2,. 3]) # define constants a, b are 1X2 tensor 
c = tf.add (a, b) # definition of a + B = C 
D = tf.scalar_mul (tf.constant (2), C) = 2 * # define C D 

with tf.Session () AS Sess: 
    Print (sess.run (D)) according to a defined # operation (FIG.) of the calculated value d, and prints

The fourth row defines the fifth row c, d operation, but there is no direct outcome of c and d, where d is calculated in sess.run session (d), it automatically calculates the d-defined operation of the foregoing results, and to calculate c sess.run (c) is displayed without recalculation d. If the total sess.run session only (c), the program will not calculate d.

Placeholder tf.placeholder ()

As with the name, it is a placeholder for a variable and give a bit, you can not assign a specific value to a variable, give the variable a position, passing specific values ​​to variables in the session is running. I.e. placeholder for providing data to the computing FIG.

tf.placeholder(dtype,shape=None,name=None)  

dtype data type of the variable, shape variables are the shape (several odd row), name is the name of the variable.

tensorflow TF AS Import 
Import numpy NP AS 
A np.array = ([. 1, 2]) 
B = np.array ([2,. 3]) # Create a, b ndarry 1x2 of two variables 
X = tf.placeholder (tf .int32) 
the Y = tf.placeholder (tf.int32) # define two placeholders tensor shape can not write, when the incoming values will automatically determine 

C = tf.add (X-, the Y) 
D = TF. scalar_mul (tf.constant (2), C) 

with tf.Session () AS Sess: 
    Re = sess.run (D, feed_dict = {X-: A, the Y: B}) # provide specific value when calculating FIG 
    print ( re)

 Session with the incoming feed_dict = {} When the value is not passed tf.constant () of this type, must be of type specific numerical array, np.ndarry like.

Guess you like

Origin www.cnblogs.com/panda-blog/p/12305782.html