<p dir="auto">Hi all,<br>
I want to <strong>include trainable weights in loss function</strong>. As I dont know how to, I use a layer that contains weight only. This layer only output weights to loss function.<br>
However, there are too many weights variables, I dont want to output all of them for the whole batch. So I decide to select some of related weights variables as an output.<br>
I will use input class info to select weights variables.<br>
For example. My layer has a kernel of size (10, 1024). Then if input is 0, it outputs kernel[0, :]. If input is 1, kernel[1,:]</p>
<p dir="auto">However, when I implement following model, the input always has a shape of (?, 1).  ? is the undefined batch number. So, I use this line to select output for these inputs.<br>
<code class="notranslate">proxy_of_class = self.kernel[x[:,0], :]</code></p>
<p dir="auto">But I always encounter this error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  File &quot;proxyModel.py&quot;, line 18, in call
    proxy_of_class = self.kernel[x[:,0], :]
ValueError: Shapes must be equal rank, but are 1 and 0
        From merging shape 0 with other shapes. for 'proxy/strided_slice_1/stack_1' (op: 'Pack') with input shapes: [?], []."><pre class="notranslate"><code class="notranslate">  File "proxyModel.py", line 18, in call
    proxy_of_class = self.kernel[x[:,0], :]
ValueError: Shapes must be equal rank, but are 1 and 0
        From merging shape 0 with other shapes. for 'proxy/strided_slice_1/stack_1' (op: 'Pack') with input shapes: [?], [].
</code></pre></div>
<p dir="auto"><strong>How can I solve this problem?</strong></p>
<p dir="auto">Below is my code based on tensorflow1.6</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from __future__ import division
import tensorflow as tf
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Layer

class Proxy(Layer):
    def __init__(self,num_class=2, **kwargs):
        self.num_class = num_class
        super(Proxy, self).__init__(**kwargs)
    def build(self, input_shape):
        self.kernel = self.add_weight(name='kernel',
                                        shape=(self.num_class, 1024),
                                        initializer='uniform',
                                        trainable=True,
                                        constraint=None)
        super(Proxy, self).build(input_shape)
    def call(self, x, mask=None):
        proxy_of_class = self.kernel[x[:,0], :]
        return proxy_of_class
    def compute_output_shape(self, input_shape):
        return (input_shape[0], 1024)

def build_model(image_size, n_classes):
    input1 = Input(shape=image_size) # for image encode
    flat = Flatten()(input1)
    dense = Dense(1024,activation='relu')(flat)
    input2 = Input(shape=(1,))       # for class index
    output = Proxy()(input2)
    model = Model(inputs=[input1, input2], outputs=[dense, output])
    model.summary()
    return model

build_model((224,224,3),2)"><pre class="notranslate"><code class="notranslate">from __future__ import division
import tensorflow as tf
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Layer

class Proxy(Layer):
    def __init__(self,num_class=2, **kwargs):
        self.num_class = num_class
        super(Proxy, self).__init__(**kwargs)
    def build(self, input_shape):
        self.kernel = self.add_weight(name='kernel',
                                        shape=(self.num_class, 1024),
                                        initializer='uniform',
                                        trainable=True,
                                        constraint=None)
        super(Proxy, self).build(input_shape)
    def call(self, x, mask=None):
        proxy_of_class = self.kernel[x[:,0], :]
        return proxy_of_class
    def compute_output_shape(self, input_shape):
        return (input_shape[0], 1024)

def build_model(image_size, n_classes):
    input1 = Input(shape=image_size) # for image encode
    flat = Flatten()(input1)
    dense = Dense(1024,activation='relu')(flat)
    input2 = Input(shape=(1,))       # for class index
    output = Proxy()(input2)
    model = Model(inputs=[input1, input2], outputs=[dense, output])
    model.summary()
    return model

build_model((224,224,3),2)
</code></pre></div>
<p dir="auto">If you open a GitHub issue, here is our policy:</p>
<p dir="auto">It must be a bug, a feature request, or a significant problem with the documentation (for small docs fixes please send a PR instead).<br>
The form below must be filled out.</p>
<p dir="auto"><strong>Here's why we have that policy:</strong>.</p>
<p dir="auto">Keras developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.</p>
<p dir="auto"><strong>System information</strong>.</p>
<p dir="auto">TensorFlow version (you are using): 2.7.0<br>
Are you willing to contribute it (Yes/No) : Currently no</p>
<p dir="auto"><strong>Describe the feature and the current behavior/state</strong>.</p>
<p dir="auto">ResNets are arguably one of the most influential architectures in deep learning. Today, they are used in different capacities. For example, sometimes they act as strong baselines, sometimes they are used as backbones. Since their inception, their performance on ImageNet-1k, in particular, has improved quite a lot. I think it's time the ResNets under <code class="notranslate">tf.keras.applications</code> were updated to facilitate these changes.</p>
<p dir="auto"><strong>Will this change the current api? How?</strong></p>
<p dir="auto">ResNet-RS (<a href="https://arxiv.org/abs/2103.07579" rel="nofollow">https://arxiv.org/abs/2103.07579</a>) introduces slight architectural changes to the vanilla ResNet architecture (<a href="https://arxiv.org/abs/1512.03385" rel="nofollow">https://arxiv.org/abs/1512.03385</a>). So, yes, there will be changes to the current implementation of ResNets (among other things) we have under <code class="notranslate">tf.keras.applications</code>. We could call it <code class="notranslate">tf.keras.applications.ResNet50RS</code>, for example. Following summarizes the performance benefits that ResNet-RS introduces to the final ImageNet-1k performance (measured on the <code class="notranslate">val</code> set):</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/22957388/145714574-b057a95e-4d6c-48ba-b7ea-67e953e83f29.png"><img src="https://user-images.githubusercontent.com/22957388/145714574-b057a95e-4d6c-48ba-b7ea-67e953e83f29.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><sub><a href="https://github.com/tensorflow/tpu/tree/master/models/official/resnet/resnet_rs#imagenet-checkpoints">Source</a></sub></p>
<p dir="auto"><strong>Who will benefit from this feature?</strong></p>
<p dir="auto">Keras users that use ResNets from <code class="notranslate">tf.keras.applications</code> for building downstream applications.</p>
<p dir="auto"><strong><a href="https://github.com/keras-team/keras/blob/master/CONTRIBUTING.md">Contributing</a></strong></p>
<ul dir="auto">
<li>Do you want to contribute a PR? (yes/no): Currently no</li>
<li>If yes, please read <a href="https://github.com/keras-team/keras/blob/master/CONTRIBUTING.md">this page</a> for instructions</li>
<li>Briefly describe your candidate solution(if contributing):</li>
</ul>