Ron Cohen Ron Cohen - 1 year ago 478
Python Question

What is tensorflow.compat.as_str()?

In the Google/Udemy Tensorflow tutorial there is the following code:

import tensorflow as tf
def read_data(filename):
"""Extract the first file enclosed in a zip file as a list of words"""
with zipfile.ZipFile(filename) as f:
data = tf.compat.as_str([0])).split()
return data

This executes fine but I cannot find
in the Tensorflow documentation or anywhere else.

Q1: What does

Q2: Is this tensorflow
library documented somewhere?

Q3: This is a call to the tensorflow library, so how and why does it work in normal python code, rather than inside a tensorflow graph? I.e. I thought tensorflow library calls had to be inside a tensorflow graph defintion block:

graph = tf.Graph()
with graph.as_default()
... tensorflow function calls here ...

I am running python 2.7.

Answer Source
  1. tf.compat.as_str converts input into a string

  2. I couldn't find any documentation, but you can look at the source code here

  3. Tensorflow functions as a python module. The graph context is used to define a graph (mathematical computations) that will be used to train the model.

typical usage involves the Graph.as_default() context manager, which overrides the current default graph for the lifetime of the contex