<h3 dir="auto">System information</h3>
<ul dir="auto">
<li><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>:<br>
Yes</li>
<li><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>:<br>
Linux Ubuntu 16.04</li>
<li><strong>TensorFlow installed from (source or binary)</strong>:<br>
pip install</li>
<li><strong>TensorFlow version (use command below)</strong>:<br>
v1.4.0-rc1-11-g130a514 1.4.0</li>
<li><strong>Python version</strong>:<br>
3.5.2</li>
<li><strong>Bazel version (if compiling from source)</strong>:<br>
NA</li>
<li><strong>GCC/Compiler version (if compiling from source)</strong>:<br>
NA</li>
<li><strong>CUDA/cuDNN version</strong>:<br>
8.0.61</li>
<li><strong>GPU model and memory</strong>:<br>
GeForce GTX 1080 Ti, 11GB</li>
<li><strong>Exact command to reproduce</strong>:</li>
</ul>
<h3 dir="auto">Describe the problem</h3>
<p dir="auto">When using tf.layers, I notice that an extra op is generated when not specifying the name.</p>
<p dir="auto">For example, here is an example using tf.layers.dense. "dense_1" is an extra op. Similar problem has been observed for tf.layers.conv2d also.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="x = tf.placeholder(tf.float32, [None, 16], name='x')
hidden = tf.layers.dense(x, 32)
hidden = tf.layers.dense(hidden, 32)
y = tf.layers.dense(hidden, 1, name='y')"><pre class="notranslate"><code class="notranslate">x = tf.placeholder(tf.float32, [None, 16], name='x')
hidden = tf.layers.dense(x, 32)
hidden = tf.layers.dense(hidden, 32)
y = tf.layers.dense(hidden, 1, name='y')
</code></pre></div>
<p dir="auto">The graph in tensorboard is as follows.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/13603534/32421364-b057853e-c24c-11e7-8aa7-865a42fc22ee.png"><img src="https://user-images.githubusercontent.com/13603534/32421364-b057853e-c24c-11e7-8aa7-865a42fc22ee.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">If I add the name,</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="x = tf.placeholder(tf.float32, [None, 16], name='x')
hidden = tf.layers.dense(x, 32, name='h1')
hidden = tf.layers.dense(hidden, 32, name='h2')
y = tf.layers.dense(hidden, 1, name='y')"><pre class="notranslate"><code class="notranslate">x = tf.placeholder(tf.float32, [None, 16], name='x')
hidden = tf.layers.dense(x, 32, name='h1')
hidden = tf.layers.dense(hidden, 32, name='h2')
y = tf.layers.dense(hidden, 1, name='y')
</code></pre></div>
<p dir="auto">then the graph looks as expected,<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/13603534/32421378-daedd956-c24c-11e7-9487-ecf9ed0d8027.png"><img src="https://user-images.githubusercontent.com/13603534/32421378-daedd956-c24c-11e7-9487-ecf9ed0d8027.png" alt="image" style="max-width: 100%;"></a></p>
<h3 dir="auto">System information</h3>
<ul dir="auto">
<li><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>: Yes</li>
<li><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>: Linux Ubuntu 16.04</li>
<li><strong>TensorFlow installed from (source or binary)</strong>: binary</li>
<li><strong>TensorFlow version (use command below)</strong>: 1.3.0</li>
<li><strong>Python version</strong>: 3.6</li>
<li><strong>Bazel version (if compiling from source)</strong>: N/A</li>
<li><strong>CUDA/cuDNN version</strong>: N/A</li>
<li><strong>GPU model and memory</strong>: N/A</li>
<li><strong>Exact command to reproduce</strong>: N/A</li>
</ul>
<h3 dir="auto">Describe the problem</h3>
<p dir="auto">This is a tiny code that creates layers and connects them in series.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import tensorflow as tf
from tensorflow.python.layers.base import Layer


class A(Layer):
    def build(self, input_shape):
        self.v = self.add_variable('v', (), tf.float32)
        self.built = True
    
    def call(self, inputs):
        return self.v * inputs
    

# Case 1
with tf.Graph().as_default() as graph:
    x = tf.placeholder(tf.float32, (), 'x')
   
    out = x
    out = A()(out)
    out = A()(out)
    out = A()(out)
    
    tf.summary.FileWriter('/tmp/tensorboard/1', graph=graph).close()

# Case 2
with tf.Graph().as_default() as graph:
    x = tf.placeholder(tf.float32, (), 'x')
   
    out = x
    out = A(name='a')(out)
    out = A(name='a_1')(out)
    out = A(name='a_2')(out)
    
    tf.summary.FileWriter('/tmp/tensorboard/2', graph=graph).close()

# Case 3
with tf.Graph().as_default() as graph:
    x = tf.placeholder(tf.float32, (), 'x')
   
    out = x
    out = A(name='a_1')(out)
    out = A(name='a_2')(out)
    out = A(name='a_3')(out)
    
    tf.summary.FileWriter('/tmp/tensorboard/3', graph=graph).close()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">tensorflow</span> <span class="pl-k">as</span> <span class="pl-s1">tf</span>
<span class="pl-k">from</span> <span class="pl-s1">tensorflow</span>.<span class="pl-s1">python</span>.<span class="pl-s1">layers</span>.<span class="pl-s1">base</span> <span class="pl-k">import</span> <span class="pl-v">Layer</span>


<span class="pl-k">class</span> <span class="pl-v">A</span>(<span class="pl-v">Layer</span>):
    <span class="pl-k">def</span> <span class="pl-en">build</span>(<span class="pl-s1">self</span>, <span class="pl-s1">input_shape</span>):
        <span class="pl-s1">self</span>.<span class="pl-s1">v</span> <span class="pl-c1">=</span> <span class="pl-s1">self</span>.<span class="pl-en">add_variable</span>(<span class="pl-s">'v'</span>, (), <span class="pl-s1">tf</span>.<span class="pl-s1">float32</span>)
        <span class="pl-s1">self</span>.<span class="pl-s1">built</span> <span class="pl-c1">=</span> <span class="pl-c1">True</span>
    
    <span class="pl-k">def</span> <span class="pl-en">call</span>(<span class="pl-s1">self</span>, <span class="pl-s1">inputs</span>):
        <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-s1">v</span> <span class="pl-c1">*</span> <span class="pl-s1">inputs</span>
    

<span class="pl-c"># Case 1</span>
<span class="pl-k">with</span> <span class="pl-s1">tf</span>.<span class="pl-v">Graph</span>().<span class="pl-en">as_default</span>() <span class="pl-k">as</span> <span class="pl-s1">graph</span>:
    <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">tf</span>.<span class="pl-en">placeholder</span>(<span class="pl-s1">tf</span>.<span class="pl-s1">float32</span>, (), <span class="pl-s">'x'</span>)
   
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-s1">x</span>
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>()(<span class="pl-s1">out</span>)
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>()(<span class="pl-s1">out</span>)
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>()(<span class="pl-s1">out</span>)
    
    <span class="pl-s1">tf</span>.<span class="pl-s1">summary</span>.<span class="pl-v">FileWriter</span>(<span class="pl-s">'/tmp/tensorboard/1'</span>, <span class="pl-s1">graph</span><span class="pl-c1">=</span><span class="pl-s1">graph</span>).<span class="pl-en">close</span>()

<span class="pl-c"># Case 2</span>
<span class="pl-k">with</span> <span class="pl-s1">tf</span>.<span class="pl-v">Graph</span>().<span class="pl-en">as_default</span>() <span class="pl-k">as</span> <span class="pl-s1">graph</span>:
    <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">tf</span>.<span class="pl-en">placeholder</span>(<span class="pl-s1">tf</span>.<span class="pl-s1">float32</span>, (), <span class="pl-s">'x'</span>)
   
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-s1">x</span>
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'a'</span>)(<span class="pl-s1">out</span>)
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'a_1'</span>)(<span class="pl-s1">out</span>)
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'a_2'</span>)(<span class="pl-s1">out</span>)
    
    <span class="pl-s1">tf</span>.<span class="pl-s1">summary</span>.<span class="pl-v">FileWriter</span>(<span class="pl-s">'/tmp/tensorboard/2'</span>, <span class="pl-s1">graph</span><span class="pl-c1">=</span><span class="pl-s1">graph</span>).<span class="pl-en">close</span>()

<span class="pl-c"># Case 3</span>
<span class="pl-k">with</span> <span class="pl-s1">tf</span>.<span class="pl-v">Graph</span>().<span class="pl-en">as_default</span>() <span class="pl-k">as</span> <span class="pl-s1">graph</span>:
    <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">tf</span>.<span class="pl-en">placeholder</span>(<span class="pl-s1">tf</span>.<span class="pl-s1">float32</span>, (), <span class="pl-s">'x'</span>)
   
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-s1">x</span>
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'a_1'</span>)(<span class="pl-s1">out</span>)
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'a_2'</span>)(<span class="pl-s1">out</span>)
    <span class="pl-s1">out</span> <span class="pl-c1">=</span> <span class="pl-v">A</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'a_3'</span>)(<span class="pl-s1">out</span>)
    
    <span class="pl-s1">tf</span>.<span class="pl-s1">summary</span>.<span class="pl-v">FileWriter</span>(<span class="pl-s">'/tmp/tensorboard/3'</span>, <span class="pl-s1">graph</span><span class="pl-c1">=</span><span class="pl-s1">graph</span>).<span class="pl-en">close</span>()</pre></div>
<h4 dir="auto">Results</h4>
<p dir="auto">Other than case 3, an unexpected graph is generated.<br>
Is this a bug?</p>
<ul dir="auto">
<li>Case 1</li>
</ul>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/7009040/31054076-b290a772-a6e5-11e7-8e6a-2d97420b5e0e.png"><img alt="result" height="450" src="https://user-images.githubusercontent.com/7009040/31054076-b290a772-a6e5-11e7-8e6a-2d97420b5e0e.png" style="max-width: 100%;"></a></p>
<ul dir="auto">
<li>Case 2</li>
</ul>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/7009040/31054078-b2a7ca7e-a6e5-11e7-9a17-805fd56e6ea7.png"><img alt="result" height="450" src="https://user-images.githubusercontent.com/7009040/31054078-b2a7ca7e-a6e5-11e7-9a17-805fd56e6ea7.png" style="max-width: 100%;"></a></p>
<ul dir="auto">
<li>Case 3</li>
</ul>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/7009040/31054077-b291b7c0-a6e5-11e7-9256-be7c986094ec.png"><img alt="result" height="450" src="https://user-images.githubusercontent.com/7009040/31054077-b291b7c0-a6e5-11e7-9256-be7c986094ec.png" style="max-width: 100%;"></a></p>