<p dir="auto">This issue is opened to host a discussion about the recurrent attention API for keras.<br>
Related issues:</p>
<p dir="auto"><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="360325397" data-permission-text="Title is private" data-url="https://github.com/keras-team/keras/issues/11142" data-hovercard-type="pull_request" data-hovercard-url="/keras-team/keras/pull/11142/hovercard" href="https://github.com/keras-team/keras/pull/11142">#11142</a>.<br>
<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="269448036" data-permission-text="Title is private" data-url="https://github.com/keras-team/keras/issues/8296" data-hovercard-type="pull_request" data-hovercard-url="/keras-team/keras/pull/8296/hovercard" href="https://github.com/keras-team/keras/pull/8296">#8296</a>.<br>
<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="249896911" data-permission-text="Title is private" data-url="https://github.com/keras-team/keras/issues/7633" data-hovercard-type="issue" data-hovercard-url="/keras-team/keras/issues/7633/hovercard" href="https://github.com/keras-team/keras/issues/7633">#7633</a>.</p>
<p dir="auto">Handling Tensors of tf.Tensor outside a custom layer is not a problem.<br>
For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="InputTensor = np.array([[[[255,0,0],[255,0,0]],[[255,0,0],[0,144,0]]],
                        [[[0,255,0],[0,255,0]],[[0,255,0],[144,0,0]]],
                        [[[0,245,0],[0,245,0]],[[0,245,0],[144,0,0]]]])

X = tf.constant(InputTensor)
print(X[0,0,0,0].numpy()) #---&gt;255
print(tf.shape(X)[0].numpy() #---&gt;3
print(X.shape[0]) #---&gt;3
print(X) #----&gt;
&lt;tf.Tensor: shape=(3, 2, 2, 3), dtype=int64, numpy=
array([[[[255,   0,   0],
         [255,   0,   0]],

        [[255,   0,   0],
         [  0, 144,   0]]],


       [[[  0, 255,   0],
         [  0, 255,   0]],

        [[  0, 255,   0],
         [144,   0,   0]]],


       [[[  0, 245,   0],
         [  0, 245,   0]],

        [[  0, 245,   0],
         [144,   0,   0]]]])&gt;"><pre class="notranslate"><code class="notranslate">InputTensor = np.array([[[[255,0,0],[255,0,0]],[[255,0,0],[0,144,0]]],
                        [[[0,255,0],[0,255,0]],[[0,255,0],[144,0,0]]],
                        [[[0,245,0],[0,245,0]],[[0,245,0],[144,0,0]]]])

X = tf.constant(InputTensor)
print(X[0,0,0,0].numpy()) #---&gt;255
print(tf.shape(X)[0].numpy() #---&gt;3
print(X.shape[0]) #---&gt;3
print(X) #----&gt;
&lt;tf.Tensor: shape=(3, 2, 2, 3), dtype=int64, numpy=
array([[[[255,   0,   0],
         [255,   0,   0]],

        [[255,   0,   0],
         [  0, 144,   0]]],


       [[[  0, 255,   0],
         [  0, 255,   0]],

        [[  0, 255,   0],
         [144,   0,   0]]],


       [[[  0, 245,   0],
         [  0, 245,   0]],

        [[  0, 245,   0],
         [144,   0,   0]]]])&gt;
</code></pre></div>
<p dir="auto">You get all the values you need for calculating. But inside custom layers the input tensor has another type:<br>
Something like</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Tensor(&quot;model_10/Cast:0&quot;, shape = (None,2,2,3), dtype = float32)"><pre class="notranslate"><code class="notranslate">Tensor("model_10/Cast:0", shape = (None,2,2,3), dtype = float32)
</code></pre></div>
<p dir="auto">There is no numpy(). tf.shape(X)[0].numpy() does not work or X.shape[0] to get the batch. The numpy() function is helpful to get the values of a tensor. The following example is inside of a custom layer. If I make a tensor with tf.constant, then I receive a tensor without the numpy() function.</p>
<p dir="auto">The next example is a dirty example which works fine but you must handle it with attention:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from keras.layers import Layer
import numpy as np
import tensorflow as tf
from keras.layers import Input
from keras.models import Model 

InputTensor = np.array([[[[255,0,0],[255,0,0]],[[255,0,0],[0,144,0]]],
                        [[[0,255,0],[0,255,0]],[[0,255,0],[144,0,0]]],
                        [[[0,245,0],[0,245,0]],[[0,245,0],[144,0,0]]]])

class MyLayer(Layer):
    def __init__(self,indexmax, **kwargs):
        self.indexmax = indexmax
        super(MyLayer,self).__init__(**kwargs)
        
    def build(self, input_shape):
        self.built = True
        super(MyLayer,self).build(input_shape)

    def call(self, X):
        print(X)
        y = np.array([[[[0,0,0]]],[[[0,0,0]]],[[[0,0,0]]]])
        if X[0,0,0,0] == 255:
            y[0,0,0,0] = 10
        if X[1,0,0,1] == 255:
	  y[1,0,0,1] = 20
        if X[2,0,0,1] == 245:
	  y[2,0,0,1] = 30   #y[2,0,0,1] = x[2,0,0,1]-30 # &lt;--- Does not work!
	
        Y =tf.constant(y)
        Y= tf.compat.v1.placeholder_with_default(Y,(None,1,1,3))
        return Y
    
indexmax = InputTensor.shape[0]
inputlayer = Input(shape = InputTensor[0].shape)
layer = MyLayer(indexmax)(inputlayer)

model = Model(inputlayer, layer ) #Model wird erzeugt
model.summary()

Output = model.predict(InputTensor)
Output = np.array(Output)
print(&quot;Output = &quot;+str(Output))"><pre class="notranslate"><code class="notranslate">from keras.layers import Layer
import numpy as np
import tensorflow as tf
from keras.layers import Input
from keras.models import Model 

InputTensor = np.array([[[[255,0,0],[255,0,0]],[[255,0,0],[0,144,0]]],
                        [[[0,255,0],[0,255,0]],[[0,255,0],[144,0,0]]],
                        [[[0,245,0],[0,245,0]],[[0,245,0],[144,0,0]]]])

class MyLayer(Layer):
    def __init__(self,indexmax, **kwargs):
        self.indexmax = indexmax
        super(MyLayer,self).__init__(**kwargs)
        
    def build(self, input_shape):
        self.built = True
        super(MyLayer,self).build(input_shape)

    def call(self, X):
        print(X)
        y = np.array([[[[0,0,0]]],[[[0,0,0]]],[[[0,0,0]]]])
        if X[0,0,0,0] == 255:
            y[0,0,0,0] = 10
        if X[1,0,0,1] == 255:
	  y[1,0,0,1] = 20
        if X[2,0,0,1] == 245:
	  y[2,0,0,1] = 30   #y[2,0,0,1] = x[2,0,0,1]-30 # &lt;--- Does not work!
	
        Y =tf.constant(y)
        Y= tf.compat.v1.placeholder_with_default(Y,(None,1,1,3))
        return Y
    
indexmax = InputTensor.shape[0]
inputlayer = Input(shape = InputTensor[0].shape)
layer = MyLayer(indexmax)(inputlayer)

model = Model(inputlayer, layer ) #Model wird erzeugt
model.summary()

Output = model.predict(InputTensor)
Output = np.array(Output)
print("Output = "+str(Output))
</code></pre></div>
<p dir="auto">But if I try to assign the value of X[2,0,0,1] to a variable or to another tensor, it does not work. I need the value of the input tensor X for calculating to build output-tensors for example with index maps. To slice the input tensor make the access very complicated and also do not work either. What I wish to avoid the parameter indexmax in the custom layer to transport the batch size into the custom layer, which has first the value None. This is also a very important value to know! If there is no possibility to get these values custom layers make no sense!</p>