tensorflow2错误-无法在函数调用tf.summary.trace_export中创建插件/配置文件/目录

时间:2019-11-05 08:24:36

标签: python tensorboard tensorflow2.0

我使用tensorflow2尝试使用函数调用tf.summary.trace_export()记录tensorflow执行,并在tensorboard图形中查看它。但是,在调用tf.summary.trace_export(name="my_func_trace", step=0, profiler_outdir=logdir)时,出现

错误
tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a directory: logs/func/20191105-014756\plugins\profile\2019-11-05_01-47-57; No such file or directory

除了使用

创建文件编写器外,我还需要手动创建/ plugin / profile /吗?
stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
logdir = 'logs/func/%s' % stamp
writer = tf.summary.create_file_writer(logdir)

当我尝试执行tensorflow.org(https://www.tensorflow.org/tensorboard/graphs#graphs_of_tffunctions)中给出的示例时,也会出现同样的错误

这是我的tensorflow简单代码:

import tensorflow as tf
from datetime import datetime

stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
logdir = './logs/tensor-constants/%s' % stamp
writer = tf.summary.create_file_writer(logdir)

a = tf.constant(1, dtype=tf.int32, shape=(), name='a')
b = tf.constant(2, dtype=tf.int32, shape=(), name='b')

tf.summary.trace_on(graph=True, profiler=True)

add = tf.math.add(a,b, name='addition')

# Print will be tensornode value
print(add)

with writer.as_default():
   tf.summary.trace_export(name="tensor-constants",
        step=0,
        profiler_outdir=logdir)

错误跟踪:

(venv) PS C:\Users\amvij\Vijay\github\tensorflow-learning> & c:/Users/amvij/Vijay/github/tensorflow-learning/venv/Scripts/python.exe c:/Users/amvij/Vijay/github/tensorflow-learning/basics/tensor-constants.py
2019-11-05 02:11:50.385963: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-11-05 02:11:50.409tf.Tensor(3, shape=(), dtype=int32)
Traceback (most recent call last):
  File "c:/Users/amvij/Vijay/github/tensorflow-learning/basics/tensor-constants.py", line 28, in <module>
    profiler_outdir=logdir)
  File "C:\Users\amvij\Vijay\github\tensorflow-learning\venv\lib\site-packages\tensorflow_core\python\ops\summary_ops_v2.py", line 1218, in trace_export
    _profiler.save(profiler_outdir, _profiler.stop())
  File "C:\Users\amvij\Vijay\github\tensorflow-learning\venv\lib\site-packages\tensorflow_core\python\eager\profiler.py", line 140, in save
    gfile.MakeDirs(plugin_dir)
  File "C:\Users\amvij\Vijay\github\tensorflow-learning\venv\lib\site-packages\tensorflow_core\python\lib\io\file_io.py", line 438, in recursive_create_dir
    recursive_create_dir_v2(dirname)
  File "C:\Users\amvij\Vijay\github\tensorflow-learning\venv\lib\site-packages\tensorflow_core\python\lib\io\file_io.py", line 453, in recursive_create_dir_v2
    pywrap_tensorflow.RecursivelyCreateDir(compat.as_bytes(path))
tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a directory: ./logs/tensor-constants/20191105-021150\plugins\profile\2019-11-05_02-11-50; No such file or directory
(venv) PS C:\Users\amvij\Vijay\github\tensorflow-learning>

对于tensorflow.org代码(https://www.tensorflow.org/tensorboard/graphs#graphs_of_tffunctions)中给出的示例,也会出现相同的错误:

from datetime import datetime
import tensorflow as tf

# The function to be traced.
@tf.function
def my_func(x, y):
  # A simple hand-rolled layer.
  return tf.nn.relu(tf.matmul(x, y))

# Set up logging.
stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
logdir = 'logs/func/%s' % stamp
writer = tf.summary.create_file_writer(logdir)

# Sample data for your function.
x = tf.random.uniform((3, 3))
y = tf.random.uniform((3, 3))

# Bracket the function call with
# tf.summary.trace_on() and tf.summary.trace_export().
tf.summary.trace_on(graph=True, profiler=True)
# Call only one tf.function when tracing.
z = my_func(x, y)
with writer.as_default():
  tf.summary.trace_export(
      name="my_func_trace",
      step=0,
      profiler_outdir=logdir)

我正在使用Windows 10进行开发。

3 个答案:

答案 0 :(得分:1)

logdir = logs\\fit\\ + datetime.now().strftime("%Y%m%d-%H%M%S") 从日志目录查看输出运行tensorboard --logdir=fit

对于@tf.function,不要使用日期时间,而只需使用logdir = logs\\func

要查看tf.function图,请从日志目录运行tensorboard --logdir=func

如果使用张量板扩展名(真的不离开jupyter实验室),请从/logs/fitlogs/func运行。

答案 1 :(得分:1)

在Windows上,使用logdir = 'logs\\tensor-constants\\%s' % stamp代替logdir = './logs/tensor-constants/%s' % stamp

答案 2 :(得分:0)

我发现的唯一解决方案是更改tensorflow_core/python/lib/io内部的问题file_io.py

将相应的recursive_create_dir_v2(dirname)更改为os.makedirs(dirname, exist_ok=True)

做到

@tf_export(v1=["gfile.MakeDirs"])
def recursive_create_dir(dirname):
    """Creates a directory and all parent/intermediate directories.

    It succeeds if dirname already exists and is writable.

    Args:
      dirname: string, name of the directory to be created

    Raises:
      errors.OpError: If the operation fails.
    """
    os.makedirs(dirname, exist_ok=True)

我不知道为什么他们决定需要构建自己的bugy定制方法来创建目录,而不是使用标准的Python库。