SoFunction
Updated on 2024-11-18

jupyter notebook tensorflow print device information example

Using log_device_placement=True directly in juypter notebook does not print device information.

# Creates a graph.
with ('/device:CPU:0'):
  a = ([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  b = ([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
  c = (a, b)
# Creates a session with log_device_placement set to True.
sess = (config=(log_device_placement=True,allow_soft_placement=True))
# Runs the op.

print((c))

Need to use output_partition_graphs to output device information

# Creates a graph.
with ('/device:GPU:0'):
  a = ([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  b = ([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
  c = (a, b)
# Creates a session with log_device_placement set to True.
sess = (config=(log_device_placement=True,allow_soft_placement=True))

# Runs the op.
options = (output_partition_graphs=True)
metadata = ()
c_val = (c, options=options, run_metadata=metadata)

print metadata.partition_graphs

Additional knowledge:Jupyter can't print on the console

Because the data is in Chinese, I purposely set the jupter in front of the

reload(sys)
("utf-8")


As a result, I can't enter the content when I use the print statement. The reason for this is that the reload has changed sdout to an ipython object, so you need to store the stdout object temporarily.

It may be worth trying the following code

import sys
stdo = 
reload(sys)
('utf-8')
= stdo

Above this jupyter notebook tensorflow print device information example is all I have shared with you, I hope to give you a reference, and I hope you support me more.