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

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??

#Define the Convolutional Autoencoder

class ConvAutoencoder(nn.Module):

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

#Instantiate the model

model = ConvAutoencoder()

print(model)

Would you like to zero out the output activation of self.conv3 in one row and column?
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).

Thanks for your guidance. If I have a problem, I will ask the question again…

for making mask i used from this code but have error:
mask= torch.cat([torch.ones([8,8,2,3]), torch.zeros([8,8,1,3])],0)

error: RuntimeError: Sizes of tensors must match except in dimension 0. Got 2 and 1 in dimension 2 (The offending index is 1)

dimension of output activation of self.conv3 is [8,8,3,3] and i would to repeat end row and column to one before the last row and column.

thanks for you guidance…

The specified dimension doesn’t seem right, so you should use:

mask = torch.cat([torch.ones([8,8,2,3]), torch.zeros([8,8,1,3])], 2)

instead. This will create a mask tensor in the shape [8, 8, 3, 3].

thanks alot…
i have another question…
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…

thanks for helping…

You could directly assign the tensors as desired, e.g.:

x = torch.randn(4, 4)
x[1, 2] = x[0, 1]

Note, that you might need to wrap it in a with torch.no_grad() guard, in case you are manipulating parameters of a model.

can you help me to achieve this goal for this code???

#Define the Convolutional Autoencoder
class ConvAutoencoder(nn.Module):
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))  ***# 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

model = ConvAutoencoder()
print(model)

def train(model, num_epochs=5, batch_size=64, learning_rate=1e-3):

torch.manual_seed(42)

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

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

                             lr=learning_rate, 

                             weight_decay=1e-5) # <--

train_loader =train_loader;

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

model = ConvAutoencoder()
max_epochs = 5
outputs = train(model, num_epochs=max_epochs)

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.

thanks a lot…