title: Tensorflow: fill tensor inside a function
body: <p>What I'm trying to define is the following idea:</p>
  
  <p>Consider whe have these tensors</p>
  
  <pre class="lang-py prettyprint-override"><code>a = tf.constant([1., 1.5, 1.2]) # tensor with shape [3,]
  b = tf.constant([1., 2., 3.])   # ""
  c = tf.constant([3., 0., 6.])   # ""
  
  t = tf.constant([0.5, 0.6, 0.7, 2., 4., 5., 6.]) # tensor with shape [7,]
  
  </code></pre>
  
  <p>Now let's consider I want to compute a new tensor, working with each element of previous tensors, for example:</p>
  
  <pre class="lang-py prettyprint-override"><code>def new_tensor(a, b, c, t):
      X = tf.constant([[tf.sin(a*t[1]), b*t[3], c+t[4]], 
          [tf.cos(b*t[5]), tf.atan2(t[5], c), a+t[2]+b],  
          [a+t[4], a+b, c*t[0]]])
      return X
  </code></pre>
  
  <p><code>X</code> should be a tensor with shape <code>[3, 3, 3]</code>. That is, I want to define a function that takes four tensors as input: three of them of same shape and the fourth with a different one. I want the function to compute a tensor (<code>X</code>) for each value of first three inputs (<code>a, b, c</code>).</p>
  
  <p>With this code TensorFlow gives this error:</p>
  
  <pre><code>TypeError: List of Tensors when single Tensor expected
  </code></pre>
  
  <p>According to this <a href="https://stackoverflow.com/questions/35661677/tensorflow-generating-a-random-constant/35662013#35662013">post</a>, this is beacuse <code>tf.constant</code> cannot take a tensor as input, and they recommend to use <code>tf.Variable</code> instead. But I don't think that fits for me because I have to work later with <code>X</code> and don't want to initializate it, etc. I have also read this <a href="https://stackoverflow.com/questions/49148375/updating-a-tensor-in-tensorflow">other post</a>, but couldn't find any answer to my problem.</p>
  
  <p>Is there any way to do what I want? Does my code make sense for my purpose? Thank you in advance.</p>
  
  <p><strong>UPDATE: with jdehesa answer</strong></p>
  
  <p>Taking @jdehesa answer and making resulting tensor more simple:</p>
  
  <pre class="lang-py prettyprint-override"><code>def new_tensor(a, b, c, t):
      # Could also use tf.convert_to_tensor
      X = tf.stack([[a+t[1],   b*t[1],    c+t[1]],
                    [b*t[0],  t[5]+ c,  a+t[2]+b],
                    [a+t[4],      a+b,   c*t[0]]])
      return X
  </code></pre>
  
  <p>And with tensors:</p>
  
  <pre class="lang-py prettyprint-override"><code>a = tf.constant([1., 1., 1.]) # tensor with shape [3,]
  b = tf.constant([2., 2., 2.])   # ""
  c = tf.constant([3., 3., 3.])   # ""
  
  t = tf.constant([1., 1., 1., 1., 1., 1., 1.]) # tensor with shape [7,]
  </code></pre>
  
  <p>What I get is the following tensor:</p>
  
  <pre class="lang-py prettyprint-override"><code># When evaluating x = new_tensor(a,b,c,t)
  [[[2. 2. 2.]
    [2. 2. 2.]
    [4. 4. 4.]]
  
   [[2. 2. 2.]
    [4. 4. 4.]
    [4. 4. 4.]]
  
   [[2. 2. 2.]
    [3. 3. 3.]
    [3. 3. 3.]]]
  
  </code></pre>
  
  <p>But what I would expected is the following:</p>
  
  <pre class="lang-py prettyprint-override"><code>[[[2. 2. 4.]
    [2. 4. 4.]
    [2. 3. 3.]]
  
   [[2. 2. 4.]
    [2. 4. 4.]
    [2. 3. 3.]]
  
   [[2. 2. 4.]
    [2. 4. 4.]
    [2. 3. 3.]]]
  </code></pre>
  
  <p>As what I want it to evaluate to each element of input tensors.</p>