votes up 6

graph() cannot be invoked inside a graph context.

Package:
Exception Class:
ValueError

Raise code

""" 
    True on success, or False if no summary was written because no default
    summary writer was available.

  Raises:
    ValueError: `graph` summary API is invoked in a graph mode.
  """
  if not context.executing_eagerly():
    raise ValueError("graph() cannot be invoked inside a graph context.")
  writer = _summary_state.writer
  if writer is None:
    return constant_op.constant(False)
  with ops.device("cpu:0"):
    if not _should_record_summaries_v2():
      return constant_op.constant(False)
🙏 Scream for help to Ukraine
Today, 25th May 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

graph function helps us to write a TensorFlow graph summary

Error to reproduce:

import tensorflow as tf

writer = tf.summary.create_file_writer("/tmp/mylogs")
@tf.function
def fn():
  graph = tf.Graph()
  with graph.as_default():
    c = tf.constant(30.0)
  with writer.as_default():
    tf.summary.graph(graph)
fn()

We will get an error because when we write our code within function TensorFlow eagerness(tf.executing_eagerly) will be False.

Simple example here

@tf.function
def fn():
  with tf.init_scope():
    print(tf.executing_eagerly())   #True
  print(tf.executing_eagerly())     #False
fn()

Fix code:

import tensorflow as tf
writer = tf.summary.create_file_writer("/tmp/mylogs")

graph = tf.Graph()
with graph.as_default():
  c = tf.constant(30.0)
with writer.as_default():
  tf.summary.graph(graph)

Jul 04, 2021 anonim answer
anonim 13.0k

Add a possible fix

Please authorize to post fix