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

How to debug TensorFlow

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to debug TensorFlow". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to debug TensorFlow.

TensorFlow has been at the top of the deep learning framework since its birth, and although the PyTorch 1.0 stable version was officially released in December 2018, it has quickly narrowed the gap, but it is also difficult to surpass.

The strengths of TensorFlow are deployment (including TensorFlow Lite deployment on mobile) and operational efficiency. In addition, there is a full range of support for various operation. Basically, all the operators you can think of have been implemented, so just call them directly. In addition, Google Brain's cutting-edge research, and now a lot of DeepMind research, open source code must be based on TensorFlow, such as the popular AutoML technology, etc., so it is natural to become No.1.

But I have to complain about its debugging function, which is too difficult to use. This also directly leads to the unusually steep learning curve of TensorFlow, which is similar to that of vim, which is difficult and painful to learn, but after learning well, it is quite cool.

So, how do you debug TensorFlow? Use breakpoints or print? Or a high-end tfdbg? Neither.

Because of the design of the TensorFlow static diagram (except the eager mode, which is discussed separately later), setting breakpoints simply cannot get the value of the actual tensor, which is executed in the background in the form of C++. What about print? You can only print out the shape information of tensor. Tfdbg, this official development of a special tool, right? However, I suggest not to try, not only to hit the command bit by bit, but also to jam when I am in a large debug program.

By the way, there is another violent method, which I used at the beginning, which is to pull tensor out of sess.run, so that you can get the specific value of tensor, but it is troublesome to change it manually every time.

All right, the artifact is coming out: tf.Print. You can use this in older versions of TensorFlow, which is very convenient:

X = tf.Print (x, [x, x] shape, x [0], …] , message= "x debug info", summarize=100)

Among them, x is the tensor that needs to be printed. Note that the first input is the same as the output, but it can be different to do some operations, but generally debug does not need it, so the output on the left side of the equation is also x.

The second input indicates what needs to be printed in square brackets, which can be the specific value of tensor x, its shape,slice, or even a function.

The third input message is used to identify this print, and the string can be customized.

The final summarize controls the number of output elements. For example, 100 outputs the first 100 elements of x.

For the new version of TensorFlow, use tf.print with the following syntax:

Print_op = tf.print (x)

Withtf.control_dependencies ([print_op]):

Out = tf.add (x, x)

Sess.run (out)

Isn't it convenient?

Although it is not as convenient as setting breakpoints directly in PyCharm, it is much easier to print out the tensor to locate the problem. Of course, if you are learning code and want to step through, it is recommended to use eager mode, which is very similar to PyTorch, of course, at the expense of operational efficiency.

At this point, I believe you have a deeper understanding of "how to debug TensorFlow", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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