tensorflow-related APIs

Source: Internet
Author: User

tensorflow-Correlation Apitensorflow Correlation function understanding

Task Time: Unknown time

Tf.truncated_normal
truncated_normal(    shape,    mean=0.0,    stddev=1.0,    dtype=tf.float32,    seed=None,    name=None)
Function Description:

Produces a truncated normal distribution random number, the value range is [mean - 2 * stddev, mean + 2 * stddev] .

Parameter list:
Name of parameter must-Choose type Description
Shape Is 1-dimensional shaping tensor or array Dimensions of the output tensor
Mean Whether 0 dimensional tensor or value Mean value
StdDev Whether 0 dimensional tensor or value Standard deviation
Dtype Whether Dtype Output type
Seed Whether Numerical Random seed, if seed is assigned, produces the same random number each time
Name Whether String Operation name
Example code:

You can now create the source file truncated_normal.pyin the /home/ubuntu directory:

Example code:/home/ubuntu/truncated_normal.py
#!/usr/bin/pythonimport tensorflow as tfinitial = tf.truncated_normal(shape=[3,3], mean=0, stddev=1)print tf.Session().run(initial)

Then execute:

python /home/ubuntu/truncated_normal.py
Execution Result:

Will get a value range [-2, 2] of the 3 * 3 matrix, you can also try to modify the source code to see what changes in the output results?

Tf.constant
constant(    value,    dtype=None,    shape=None,    name=‘Const‘,    verify_shape=False)
Function Description:

Generates a constant tensor of a shape dimension based on value values

Parameter list:
Name of parameter must-Choose type Description
Value Is Constant Value or list The value of the output tensor
Dtype Whether Dtype Output tensor element type
Shape Whether 1-dimensional shaping tensor or array Dimensions of the output tensor
Name Whether String Tensor name
Verify_shape Whether Boolean Detects if shape is the same shape as value, and if it is fasle, the last element will be used to complete the shape
Example code:

You can now create the source file constant.pyin the /home/ubuntu directory, which can be referenced in the following:

Example code:/home/ubuntu/constant.py
#!/usr/bin/pythonimport tensorflow as tfimport numpy as npa = tf.constant([1,2,3,4,5,6],shape=[2,3])b = tf.constant(-1,shape=[3,2])c = tf.matmul(a,b)e = tf.constant(np.arange(1,13,dtype=np.int32),shape=[2,2,3])f = tf.constant(np.arange(13,25,dtype=np.int32),shape=[2,3,2])g = tf.matmul(e,f)with tf.Session() as sess:    print sess.run(a)    print ("##################################")    print sess.run(b)    print ("##################################")    print sess.run(c)    print ("##################################")    print sess.run(e)    print ("##################################")    print sess.run(f)    print ("##################################")    print sess.run(g)

Then execute:

python /home/ubuntu/constant.py
Execution Result:
    • a:2x3 dimension tensor;
    • b:3x2 dimension tensor;
    • c:2x2 dimension tensor;
    • e:2x2x3 dimension tensor;
    • f:2x3x2 dimension tensor;
    • g:2x2x2 dimension tensor.

You can also try to modify the source code to see what happens to the output?

Tf.placeholder
placeholder(    dtype,    shape=None,    name=None)
Function Description:

is a placeholder that needs to be supplied with data at execution time

Parameter list:
Name of parameter must-Choose type Description
Dtype Is Dtype Placeholder data type
Shape Whether 1-dimensional shaping tensor or array Placeholder Dimension
Name Whether String Placeholder Name
Example code:

You can now create the source file placeholder.pyin the /home/ubuntu directory, which can be referenced in the following:

Example code:/home/ubuntu/placeholder.py
#!/usr/bin/pythonimport tensorflow as tfimport numpy as npx = tf.placeholder(tf.float32,[None,10])y = tf.matmul(x,x)with tf.Session() as sess:    rand_array = np.random.rand(10,10)    print sess.run(y,feed_dict={x:rand_array})

Then execute:

python /home/ubuntu/placeholder.py
Execution Result:

Outputs the tensor of a 10x10 dimension. You can also try to modify the source code to see what happens to the output?

Tf.nn.bias_add
bias_add(    value,    bias,    data_format=None,    name=None)
Function Description:

Adding the deviation item bias to value can be seen as a special case of tf.add where the bias must be one-dimensional and the same as the last dimension of value, and the data type must be the same as value.

Parameter list:
Name of parameter must-Choose type Description
Value Is Tensor Data types are float, double, Int64, Int32, Uint8, Int16, int8, complex64, or complex128
Bias Is 1-dimensional tensor Dimensions must be equal to the last dimension of value
Data_format Whether String Data format, supporting ' NHWC ' and ' NCHW '
Name Whether String Operation name
Example code:

You can now create the source file bias_add.pyin the /home/ubuntu directory, which can be referenced in the following:

Example code:/home/ubuntu/bias_add.py
#!/usr/bin/pythonimport tensorflow as tfimport numpy as npa = tf.constant([[1.0, 2.0],[1.0, 2.0],[1.0, 2.0]])b = tf.constant([2.0,1.0])c = tf.constant([1.0])sess = tf.Session()print sess.run(tf.nn.bias_add(a, b)) #print sess.run(tf.nn.bias_add(a,c)) errorprint ("##################################")print sess.run(tf.add(a, b))print ("##################################")print sess.run(tf.add(a, c))

Then execute:

python /home/ubuntu/bias_add.py
Execution Result:

3 x 3x2 dimensions. You can also try to modify the source code to see what happens to the output?

Tf.reduce_mean
reduce_mean(    input_tensor,    axis=None,    keep_dims=False,    name=None,    reduction_indices=None)
Function Description:

Calculate tensor input_tensor Average

Parameter list:
required type description
Input_tensor Is Tensor Enter the tensor of the average to be averaged
Axis Whether None, 0, 1 None: Global averaging; 0: averaging of each column; 1: Averaging each row
Keep_dims Whether Boolean Keep the original dimension, down to 1
Name Whether String Operation name
Reduction_indices Whether None is equivalent to axis and is deprecated
Example code:

You can now create the source file reduce_mean.pyin the /home/ubuntu directory, which can be referenced in the following:

Example code:/home/ubuntu/reduce_mean.py
#!/usr/bin/pythonimport tensorflow as tfimport numpy as npinitial = [[1.,1.],[2.,2.]]x = tf.Variable(initial,dtype=tf.float32)init_op = tf.global_variables_initializer()with tf.Session() as sess:    sess.run(init_op)    print sess.run(tf.reduce_mean(x))    print sess.run(tf.reduce_mean(x,0)) #Column    print sess.run(tf.reduce_mean(x,1)) #row

Then execute:

python /home/ubuntu/reduce_mean.py
Execution Result:
1.5[ 1.5  1.5][ 1.  2.]

You can also try to modify the source code to see what happens to the output?

Tf.squared_difference
squared_difference(    x,    y,    name=None)
Function Description:

Calculate tensor x, y corresponding element squared difference

Parameter list:
Name of parameter must-Choose type Description
X Is Tensor Is half, float32, Float64, Int32, Int64, Complex64, complex128 one of the types
Y Is Tensor Is half, float32, Float64, Int32, Int64, Complex64, complex128 one of the types
Name Whether String Operation name
Example code:

You can now create the source file squared_difference.pyin the /home/ubuntu directory, which can be referenced in the following:

Example code:/home/ubuntu/squared_difference.py
#!/usr/bin/pythonimport tensorflow as tfimport numpy as npinitial_x = [[1.,1.],[2.,2.]]x = tf.Variable(initial_x,dtype=tf.float32)initial_y = [[3.,3.],[4.,4.]]y = tf.Variable(initial_y,dtype=tf.float32)diff = tf.squared_difference(x,y)init_op = tf.global_variables_initializer()with tf.Session() as sess:    sess.run(init_op)    print sess.run(diff)

Then execute:

python /home/ubuntu/squared_difference.py
Execution Result:
[[ 4.  4.] [ 4.  4.]]

You can also try to modify the source code to see what happens to the output?

Tf.square
square(    x,    name=None)
Function Description:

Calculates the square of the tensor corresponding element

Parameter list:
Name of parameter must-Choose type Description
X Is Tensor Is half, float32, Float64, Int32, Int64, Complex64, complex128 one of the types
Name Whether String Operation name
Example code:

You can now create the source file square.pyin the /home/ubuntu directory, which can be referenced in the following:

Example code:/home/ubuntu/square.py
#!/usr/bin/pythonimport tensorflow as tfimport numpy as npinitial_x = [[1.,1.],[2.,2.]]x = tf.Variable(initial_x,dtype=tf.float32)x2 = tf.square(x)init_op = tf.global_variables_initializer()with tf.Session() as sess:    sess.run(init_op)    print sess.run(x2)

Then execute:

python /home/ubuntu/square.py
Execution Result:
[[ 1.  1.] [ 4.  4.]]

You can also try to modify the source code to see what happens to the output?

TensorFlow Related class understanding

Task Time: Unknown time

Tf. Variable
__init__(    initial_value=None,    trainable=True,    collections=None,    validate_shape=True,    caching_device=None,    name=None,    variable_def=None,    dtype=None,    expected_shape=None,    import_scope=None)
Function Description:

Maintain the state information during the execution of the diagram, such as changes in the weight values of the neural network.

Parameter list:
Name of parameter type Description
Initial_value Tensor The initial value of the Variable class, which must specify the shape information, or the subsequent validate_shape should be set to False
Trainable Boolean Whether to add variables to collection Graphkeys.trainable_variables (collection is a global store that is not affected by the variable name living space, where it is saved, and is desirable everywhere)
Collections Graph Collections Global storage, default is Graphkeys.global_variables
Validate_shape Boolean Whether to allow Initial_value initialization by an unknown dimension
Caching_device String Indicates which device is used to cache variables
Name String Variable name
Dtype Dtype If it is set, the initialized value will initialize as this type.
Expected_shape Tensorshape If set, then the initial value will be this dimension
Example code:

You can now create the source file variable.pyin the /home/ubuntu directory, which can be referenced in the following:

Example code:/home/ubuntu/variable.py
 #!/usr/bin/pythonimport TensorFlow as Tfinitial = Tf.truncated_normal (shape=[10,10],mean=0,stddev=1) W=tf. Variable (initial) list = [[1.,1.],[2.,2.] X = tf. Variable (list,dtype=tf.float32) init_op = Tf.global_variables_initializer () with TF. Session () as Sess:sess.run (init_op) print ("################## (1) ################") Print Sess.run (W) print ( "################## (2) ################") Print Sess.run (W[:2,:2]) op = w[:2,:2].assign (22.*tf.ones ((2,2))) print ("################### (3) ###############") Print Sess.run (OP) print ("################### (4) ###############") Prin T (W.eval (Sess)) #computes and returns the value of this variable print ("#################### (5) ##############") pr Int (W.eval ()) #Usage with the default session print ("##################### (6) #############") Print W.dtype pri NT Sess.run (w.initial_value) print Sess.run (w.op) Print w.shape print ("################### (7) ###############") Print Sess.run (X) 

Then execute:

python /home/ubuntu/Variable.py
Complete

Task Time: Unknown time

Congratulations, you have completed the contents of this experiment

You can do a series of more TensorFlow tutorials:

    • TensorFlow-Linear regression
    • TensorFlow-based on CNN digital recognition

For more information about TensorFlow, refer to TensorFlow website .

tensorflow-related APIs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.