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

Example Analysis of tensorflow Tensor

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly analyzes the relevant knowledge points of the example analysis of the tensorflow tensor, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about the example analysis of the tensorflow tensor.

Here is a further introduction to the tensor in the way of an example.

Many instructions (that is, nodes) can create tensors, such as tf.constant (), etc. Here are a few examples to demonstrate the operation of tensors.

(1) example 1: the addition of two constants

The code is as follows:

Import tensorflow as tf

F = tf.constant

S = tf.constant

Sum = tf.add (f, s)

With tf.Session () as sess:

Print (sess.run (sum))

The diagram built by the code looks something like this:

As you can see, the tensors f, s and sum are all generated by the corresponding instructions (tf.constant and tf.add). When using session to evaluate the tensor sum, the nodes (instructions) of the entire graph are executed.

The above code uses the default diagram (the default diagram is not specified), or you can create a new diagram to use, for example, you can write the code like this:

The diagram built by this code is similar to the one above, except that each node is named, and the screenshot is as follows:

(2) example 2: vector addition

The code is as follows:

Import tensorflow as tf

F = tf.ones ([3], dtype=tf.int32)

S = tf.constant ([1, 2, 3], dtype=tf.int32)

Sum = tf.add (f, s)

With tf.Session () as sess:

Print (sum.eval ())

This code is explained as follows:

The diagram built by this code is the same as in example 1, except that the type and value of the tensor are different, and the result of execution is like the screenshot:

(3) example 3: tensor of several dimensions

As mentioned in the previous program, a tensor can have several dimensions, 0 is a constant, 1 is a vector (vector), and 2 is a matrix.

The following code demonstrates the tensors of dimensions 0, 1, and 2:

The implementation results are as follows:

Use the function get_shap () to get dimension information, such as:

The implementation results are as follows:

(4) example 4: Matrix multiplication

The code is as follows:

Execution result:

For a matrix tensor, you can modify its number of rows or dimensions, such as this:

(5) example 5: use of variables

The variable is also a tensor, and the instruction tf.Variable () creates a variable and sets a value, but tf's assignment to the variable is "strange". Tf.Variable (xxx) does not assign a value, and tf.global_variables_initializer () must be performed as follows. You must assign a value before using a variable, or an exception will be thrown:

The characteristic of a variable is that it can be assigned a new value, but it should be noted that tf.assign () returns an instruction (node). Before the instruction is executed, the variable will not be assigned a new value. For more information, please refer to the following code:

The execution result is as follows:

This is the end of the "sample analysis of the tensorflow tensor". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!

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

Internet Technology

Wechat

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

12
Report