Error while combining single Tensors with multiple Tensors

username: KURUVILLA_ABRAHAM
cooked: <p>Hi Team</p>
<h1><a name="requirement-1" class="anchor" href="#requirement-1"></a>Requirement</h1>
<p>I want to multiply a single image with multiple tensors vice versa with another images also same using Linear Layer of Pytorch</p>
<p><strong>|Image|Target_Color|</strong><br>
|Image1|color1|<br>
|Image1|color2|<br>
|Image1|color3|<br>
|Image1|color4|<br>
|Image1|color5|<br>
|Image1|color6|<br>
|Image1|color7|<br>
|Image2|color1|<br>
|Image2|color2|<br>
|Image2|color3|<br>
|Image2|color4|<br>
|Image2|color5|<br>
|Image2|color6|<br>
|Image2|color7|<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/4/f/4f3dcfe4b1daff20896fcc1480ace9cd0dc2b24e.png" alt="image" data-base62-sha1="bj09q4lauCRLP2HTNkzr90BqgZ8" width="272" height="482"></p>
<p>where Image1 and Image2 are 2 tensors which are multiplied with 7 colors each</p>
<h1><a name="below-is-the-code-i-am-using-2" class="anchor" href="#below-is-the-code-i-am-using-2"></a>Below is the code i am using -</h1>
<h1><a name="producing-both-red-and-blue-colors-3" class="anchor" href="#producing-both-red-and-blue-colors-3"></a>producing both red and blue colors</h1>
<h1><a name="target-color-blue-4" class="anchor" href="#target-color-blue-4"></a>Target color blue</h1>
<p>Target_color_blue = torch.zeros(1, 1, 3, dtype=torch.float)<br>
Target_color_blue[:,:,2] = 255</p>
<h1><a name="target-color-red-5" class="anchor" href="#target-color-red-5"></a>Target color red</h1>
<p>Target_color_red = torch.zeros(1, 1, 3, dtype=torch.float)<br>
Target_color_red[:,:,0] = 255</p>
<p>Target_color_list = [Target_color_blue,Target_color_red]</p>
<h1><a name="stacking-both-red-and-blue-tensors-6" class="anchor" href="#stacking-both-red-and-blue-tensors-6"></a>Stacking both red and blue tensors</h1>
<p>Target_colors = torch.stack(target_color_list)</p>
<h1><a name="single-image-7" class="anchor" href="#single-image-7"></a>Single image</h1>
<p>image = torch.randn(1,16,16,256)</p>
<p><span class="hashtag-raw">#This</span> is my model block i am using -</p>
<h1><a name="main-block-8" class="anchor" href="#main-block-8"></a>Main block</h1>
<p>class AdaIN(nn.Module):<br>
‘’’<br>
Adain Class<br>
##<span class="hashtag-raw">#Passing</span> style information w in Adain Network from Color embedding####<br>
‘’’<br>
def <strong>init</strong>(self,channels,w_dim):<br>
super(AdaIN,self).<strong>init</strong>()</p>
<pre><code>    self.instance_norm = nn.InstanceNorm2d(channels)
    self.style_scale_transform = nn.Linear(w_dim,channels)        
    self.style_shift_transform = nn.Linear(w_dim,channels)
    
def forward(self,image,w):
    
    x = self.instance_norm(image)
    style_scale = self.style_scale_transform(w).unsqueeze(2).unsqueeze(3) 
    style_bias = self.style_shift_transform(w).unsqueeze(2).unsqueeze(3)
    out = style_scale * x + style_bias
    return out
</code></pre>
<h1><a name="class-mappingnetworknnmodule-9" class="anchor" href="#class-mappingnetworknnmodule-9"></a>class MappingNetwork(nn.Module):</h1>
<pre><code>'''
Mapping Layers Class
Values:
    z_dim: the dimension of the noise vector, a scalar
    hidden_dim: the inner dimension, a scalar
    w_dim: the dimension of the intermediate noise vector, a scalar
'''

def __init__(self,z_dim,w_dim):
    super(MappingNetwork,self).__init__()
    self.mapping = nn.Sequential(
        PixelNorm(),
        nn.Linear(z_dim,w_dim),
        nn.ReLU(),
        nn.Linear(w_dim, w_dim),
        nn.ReLU(),
        nn.Linear(w_dim, w_dim),
        nn.ReLU(),
        nn.Linear(w_dim, w_dim),
        nn.ReLU(),
        nn.Linear(w_dim, w_dim),
        nn.ReLU(),
        nn.Linear(w_dim, w_dim),
        nn.ReLU(),
        nn.Linear(w_dim, w_dim),
        nn.ReLU(),
        nn.Linear(w_dim, w_dim),

    )

def forward(self,target_color):
    '''
    Function for completing a forward pass of MappingLayers: 
    Given an initial Target tensor, returns the intermediate target tensor.
    Parameters:
        noise: a target_color tensor with dimensions (n_samples, z_dim)
    '''
    return self.mapping(target_color)
</code></pre>
<h1><a name="calling-the-above-blocks-10" class="anchor" href="#calling-the-above-blocks-10"></a>calling the above blocks -</h1>
<p>style = MappingNetwork(3,256)</p>
<p>style_tensor = style(Target_colors)</p>
<p>Adain_bk = AdaIN(3,256)</p>
<p>image = torch.randn(1,16,16,256)</p>
<p>final = Adain_bk(image,Target_colors)</p>
<h1><a name="runtimeerror-mat1-and-mat2-shapes-cannot-be-multiplied-2x3-and-256x3-11" class="anchor" href="#runtimeerror-mat1-and-mat2-shapes-cannot-be-multiplied-2x3-and-256x3-11"></a>RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x3 and 256x3)</h1>
<p>while calling the Adain block above .</p>
<p>Could you please guide how we can i change our model according to the requirement above</p>

username: my3bikaht
cooked: <aside class="quote no-group" data-username="KURUVILLA_ABRAHAM" data-post="1" data-topic="143617">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kuruvilla_abraham/48/45695_2.png" class="avatar"> KURUVILLA_ABRAHAM:</div>
<blockquote>
<p><code>style_scale * x</code></p>
</blockquote>
</aside>
<p>style_scale probably has shape (2,3)<br>
x probably has shape (256,3)<br>
You can’t multiply matrices in this configuration</p>

username: KURUVILLA_ABRAHAM
cooked: <p>Ok so it should be (3,256) to multiply ryt ?<br>
Will this solve my requirement of multiplying 1 tensor with multiple tensors</p>

username: my3bikaht
cooked: <p>it does look like style_scale is a set of weights, and you want to multiply element-wise, then style_scale should have same shape as x</p>