
Sadržaj
- O čemu se radi
- Baza referentnog koda
- 1. Dohvaćanje i ispis vrijednosti unutar Session.run
- 2. Upotrijebite operaciju tf.Print
- 3. Za nadzor koristite vizualizaciju Tensorboard
- a) očistite graf vlastitim imenima i opsezima imena
- b) Dodajte tf.sažetke
- c) Dodajte tf.summary.FileWriter za stvaranje datoteka dnevnika
- d) Pokrenite tensorboard poslužitelj sa svog terminala
- 4. Upotrijebite program za otklanjanje pogrešaka Tensorboard
- 5. Upotrijebite program za otklanjanje pogrešaka TensorFlow
- Zaključak
O čemu se radi
Otklanjanje pogrešaka prvo je dvostruko teže od pisanja koda. Stoga, ako kod napišete što pametnije, po definiciji niste dovoljno pametni da biste ga riješili. - BRIAN W. KERNIGHANOtklanjanje pogrešaka općenito može biti dosadan i izazovan zadatak. Ipak, mora vam biti ugodno prolaziti kroz napisani kod i prepoznavati probleme. Obično postoji mnogo vodiča, a postupak ispravljanja pogrešaka često je dobro dokumentiran za mnoge jezike i okvire.
Kad je pak riječ o TensorFlowu, pojavljuju se novi izazovi zbog načina na koji on funkcionira.
Kao što službena dokumentacija kaže:
Osnovni program TensorFlow sastoji se od dva odvojena odjeljka:
- Izgradnja računskog grafa (tf.Graph).
- Pokretanje računskog grafa (pomoću tf.Session).

Stvarno se računanje vrši session.run()
, što znači da moramo pronaći način za provjeru vrijednosti unutar ove funkcije.
Baza referentnog koda
Kao referencu, ovdje ću pružiti svom Github-ovom spremištu odgovarajući kod.
Upotrijebit ćemo osnovnu neuronsku mrežu za klasifikaciju rukom napisanih znamenki iz skupa podataka MNIST, koristeći:
tf.nn.softmax_cross_entropy_with_logits_v2
kao operacija klasifikacije TF za definiranje gubitkatf.train.GradientDescentOptimizer
radi smanjenja gubitka
Pokretanje ove male neuronske mreže pokazuje da već može postići točnost od ~ 92%:
Proces otklanjanja pogrešaka
Sada za otklanjanje pogrešaka u osnovi postoji 5 (pragmatičnih) načina da se to postigne.
Kao popratna napomena: Često je korisno tvrditi oblike kako bi se osiguralo da sve funkcionira zajedno kako je predviđeno.1. Dohvaćanje i ispis vrijednosti unutar Session.run
Ovo je vjerojatno najbrži i najlakši način da dobijete potrebne informacije.
- lako i brzo
- bilo koja procjena može se donijeti odasvuda
- potrebno je zadržati referencu na tenzor, što je loše u složenim modelima
U osnovi, sesiju pokrećete u ispisu i dodajete joj rječnik, otprilike ovako: print( f"The bias parameter is: {sess.run(b, feed_dict={x: mnist.test.images, y_: mnist.test.labels})}" )
Ako kôd postane složeniji, moglo bi se upotrijebiti djelomično izvršavanje sesije. Ali budući da je ovo eksperimentalna značajka, neću je primijeniti za demonstraciju.
Uz to, ne zaboravite .eval()
metodu za posebno vrednovanje tenzora.
Potpuni kod pogledajte ovdje na Githubu.
2. Upotrijebite operaciju tf.Print
Metoda tf.Print dobro dođe tijekom procjene vremena izvođenja kada ne želimo eksplicitno dohvatiti kôd s session.run (). To je identitet koji ispisuje podatke tijekom ocjenjivanja.
- omogućuje nam da vidimo razvoj vrijednosti tijekom vrednovanja
- ima ograničenu konfiguraciju i stoga može lako začepiti terminal
Yufeng G stvorio je fantastičan video i članak o tome kako koristiti izjavu tf.Print. I kako ističe, od vitalne je važnosti čvor za ispis strukturirati na način na koji se dalje koristi. Kao što kaže:
Od vitalne je važnosti da zapravo koristite ovaj vraćeni čvor, jer ako ne, bit će viseći.U svoj kôd dodao sam iskaz za ispis koji dohvaća vrijednosti unutar sesije kako bi ilustrirao kako se obje metode različito izvode u izvršavanju.
Uz procjenu vremena izvođenja dolazi i mogućnost tvrdnje o vremenu izvođenja s tf.Assert
.
Cijeli kod pogledajte ovdje.
3. Za nadzor koristite vizualizaciju Tensorboard
Prije ulaska u ovu metodu otklanjanja pogrešaka, imajte na umu da postoje Tensorboard i Tensorboard program za otklanjanje pogrešaka!
Web stranica TF nudi izvrsne upute za implementaciju i korištenje ploče.
Ključ za upotrebu je serializacija podataka. TensorFlow pruža sažete operacije koje vam omogućuju izvoz sažete informacije o modelu. Oni su poput sidara koji ploči za vizualizaciju govore što treba zacrtati.
a) Clean the graph with proper names and name scopes
First we need to organize all the variables and operations with the scope
methods that TF provides.
with tf.name_scope("variables_scope"): x = tf.placeholder(tf.float32, shape=[None, 784], name="x_placeholder") y_ = tf.placeholder(tf.float32, shape=[None, 10], name="y_placeholder")
b) Add tf.summaries
For example:
with tf.name_scope("weights_scope"): W = tf.Variable(tf.zeros([784, 10]), name="weights_variable") tf.summary.histogram("weight_histogram", W)
c) Add a tf.summary.FileWriter to create log files
Tip: Make sure to create sub folders for each log to avoid accumulation of graphs.
d) Start the tensorboard server from your terminal
For example: tensorboard --logdir=./tfb_logs/ --port=8090 --host=127.0.0.1
Navigating to the tensorboard server (in this case //127.0.0.1:8090
) shows the following:

Now the full power and use of tensorboard becomes clear. It allows you very easily to spot errors in your machine learning model. My code example is a very simple one. Imagine a model with multiple layers and more variables and operations!
See full code here on Github.
4. Use the Tensorboard debugger
As the Tensorboard Github repository states:
This dashboard is in its alpha release. Some features are not yet fully functional.However, it can still be used and provides cool debugging features. Please check out the Github repository to get an adequate overview. Also, see their video to get a deeper understanding. They have done a great job.
To accomplish this, there are 3 things to add to our previous example:
- Import
from tensorflow.python import debug as tf_debug
- Add your session with
tf_debug.TensorBoardDebugWrapsperSession
- Add to your tensorboard server the
debugger_port
Now you have the option to debug the whole visualized model like with any other debugger, but with a beautiful map. You are able to select certain nodes and inspect them, control execution with the “step” and “continue” buttons, and visualize tensors and their values.

There is much more to talk about regarding this unique feature of Tensorflow, but I will probably dedicate another article to that.
See my full code here on Github.
5. Use the TensorFlow debugger
The last method, but also very powerful, is the CLI TensorFlow debugger.
This debugger focuses on the command-line interface (CLI) of tfdbg, as opposed to the graphical user interface (GUI) of tfdbg, that is the TensorBoard Debugger Plugin.
You simply wrap the session with tf_debug.LocalCLIDebugWrapperSession(sess)
and then you start the debugging with executing the file (maybe it's necessary to add the --debug
flag).
It basically allows you to run and step through the execution of your model, while providing evaluation metrics.
I think the official documention could be improved, but they have also created a video which introduces the feature in a good way.
So the key features here are the commands invoke_stepper
and then pressing s
to step through each operation. It is the basic debugger functionality of a debugger but in the CLI. It looks like this:

See the full code here on Github.
Conclusion
As shown, there are many ways to debug a TensorFlow application. Each method has its own strengths and weaknesses. I didn’t mention the Python debugger, because it is not TensorFlow specific, but keep in mind that the simple Python debugger already provides some good insights!
There is a great presentation by Wookayin who talks about these concepts as well but also goes over some general debugging advise. That advice is:
- name tensors properly
- check and sanitize input
- logging
- assertions
- proper use of exceptions
- failing fast -> immediately abort if something is wrong
- don’t repeat yourself
- organize your modules and code
I am really excited for all the features that TensorFlow has to offer for people who are building machine learning systems. They are doing a great job! Looking forward to further developments! :)
Thanks for reading my article! Feel free to leave any feedback!
Daniel is a LL.M. student in business law, working as a software engineer and organizer of tech related events in Vienna. His current personal learning efforts focus on machine learning.
Connect on:
- Github
- Medium
- Steemit
- Hashnode