How to zeroing one row and column in one hidden layer by pytorch

username: students
cooked: <p>hello, i tried to zeroing one row and column in last layer of encoder after training and show the result without train. can you help me??</p>
<p><span class="hashtag">#Define</span> the Convolutional Autoencoder</p>
<p>class ConvAutoencoder(nn.Module):</p>
<pre><code>def __init__(self):

    super(ConvAutoencoder, self).__init__()

   

    #Encoder

    self.conv1 = nn.Conv2d(3, 16, 3, stride=2, padding=1)

    self.conv2 = nn.Conv2d(16, 8, 3, stride=2, padding=1)

    self.conv3 = nn.Conv2d(8,8,3)



    #Decoder

    self.conv4 = nn.ConvTranspose2d(8, 8, 3)

    self.conv5 = nn.ConvTranspose2d(8, 16, 3, stride=2, padding=1, output_padding=1)

    self.conv6 = nn.ConvTranspose2d(16, 3, 3, stride=2, padding=1, output_padding=1)

def forward(self, x):

    x = F.relu(self.conv1(x))      

    x = F.relu(self.conv2(x))

    x = F.relu(self.conv3(x))  

    x = F.relu(self.conv4(x))

    x = F.relu(self.conv5(x))

    x = F.relu(self.conv6(x))

  #  x=torch.sigmoid(x)

    return x
</code></pre>
<p><span class="hashtag">#Instantiate</span> the model</p>
<p>model = ConvAutoencoder()</p>
<p>print(model)</p>

username: ptrblck
cooked: <p>Would you like to zero out the output activation of <code>self.conv3</code> in one row and column?<br>
If so, you could create a mask tensor and multiply it with the output activation (set the rows and columns to zeros in the mask and the rest to ones).</p>

username: students
cooked: <p>Thanks for your guidance. If I have a problem, I will ask the question again…</p>

username: students
cooked: <p>for making mask i used from this code but have error:<br>
mask= torch.cat([torch.ones([8,8,2,3]), torch.zeros([8,8,1,3])],0)</p>
<p>error: RuntimeError: Sizes of tensors must match except in dimension 0. Got 2 and 1 in dimension 2 (The offending index is 1)</p>
<p>dimension of output activation of <code>self.conv3</code> is [8,8,3,3] and i would to repeat end row and column to one before the last row and column.</p>
<p>thanks for you guidance…</p>

username: ptrblck
cooked: <p>The specified dimension doesn’t seem right, so you should use:</p>
<pre><code class="lang-python">mask = torch.cat([torch.ones([8,8,2,3]), torch.zeros([8,8,1,3])], 2)
</code></pre>
<p>instead. This will create a <code>mask</code> tensor in the shape <code>[8, 8, 3, 3]</code>.</p>

username: students
cooked: <p>thanks alot…<br>
i have another question…<br>
if i want to remove row and column in one hidden layer after train (just for test phase) and repeat  the  above row and column to this row, how i work and coding in pytorch…</p>
<p>thanks for helping…</p>

username: ptrblck
cooked: <p>You could directly assign the tensors as desired, e.g.:</p>
<pre><code class="lang-python">x = torch.randn(4, 4)
x[1, 2] = x[0, 1]
</code></pre>
<p>Note, that you might need to wrap it in a <code>with torch.no_grad()</code> guard, in case you are manipulating parameters of a model.</p>

username: students
cooked: <p>can you help me to achieve this goal for this code???</p>
<p><span class="hashtag">#Define</span> the Convolutional Autoencoder<br>
class ConvAutoencoder(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(ConvAutoencoder, self).<strong>init</strong>()</p>
<pre><code>    #Encoder
    self.conv1 = nn.Conv2d(3, 16, 3, stride=2, padding=1)
    self.conv2 = nn.Conv2d(16, 8, 3, stride=2, padding=1)
    self.conv3 = nn.Conv2d(8,8,3)

    #Decoder
    self.conv4 = nn.ConvTranspose2d(8, 8, 3)
    self.conv5 = nn.ConvTranspose2d(8, 16, 3, stride=2, padding=1, output_padding=1)
    self.conv6 = nn.ConvTranspose2d(16, 3, 3, stride=2, padding=1, output_padding=1)

def forward(self, x):
    x = F.relu(self.conv1(x))      
    x = F.relu(self.conv2(x))
    x = F.relu(self.conv3(x))  ***# in this output layer i need to zeroing end of row and column and repeat above row and column in this row and column....***
   
    x = F.relu(self.conv4(x))
    x = F.relu(self.conv5(x))
    x = F.relu(self.conv6(x))

    return x
</code></pre>
<p>model = ConvAutoencoder()<br>
print(model)</p>
<p>def train(model, num_epochs=5, batch_size=64, learning_rate=1e-3):</p>
<pre><code>torch.manual_seed(42)

criterion = nn.MSELoss() # mean square error loss

optimizer = torch.optim.Adam(model.parameters(),

                             lr=learning_rate, 

                             weight_decay=1e-5) # &lt;--
</code></pre>
<h1>
<a name="train_loader-train_loader" class="anchor" href="#train_loader-train_loader"></a>train_loader =train_loader;</h1>
<pre><code>outputs = []

for epoch in range(num_epochs):

    for data in train_loader:
        img, _ = data
        recon = model(img)
        loss = criterion(recon, img)
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

    print('Epoch:{}, Loss:{:.4f}'.format(epoch+1, float(loss)))
    outputs.append((epoch, img, recon),)
return outputs
</code></pre>
<p>model =  ConvAutoencoder()<br>
max_epochs = 5<br>
outputs = train(model, num_epochs=max_epochs)</p>

username: students
cooked: <p>i think my question is so long…that i do again… i want to change one hidden layer in test phase (after train phase) as one row and column repeat to above row and column and other hidden layers do not change (other trained weights do not changed). please discuss about this by details.</p>
<p>thanks a lot…</p>