this is the full code
class DenoisingAutoencoder(nn.Module):
def init(self, in_features, out_features, activation=“relu”,
dropout=0.2, tied=False):
super(self.class, self).init()
self.weight = Parameter(torch.Tensor(out_features, in_features))
if tied:
self.deweight = self.weight.t()
else:
self.deweight = Parameter(torch.Tensor(in_features, out_features))
self.bias = Parameter(torch.Tensor(out_features))
self.vbias = Parameter(torch.Tensor(in_features))
    if activation=="relu":
        self.enc_act_func = nn.ReLU()
    elif activation=="sigmoid":
        self.enc_act_func = nn.Sigmoid()
    self.dropout = nn.Dropout(p=dropout)

    self.reset_parameters()

def reset_parameters(self):
    stdv = 1. / math.sqrt(self.weight.size(1))
    self.weight.data.uniform_(-stdv, stdv)
    self.bias.data.uniform_(-stdv, stdv)
    stdv = 1. / math.sqrt(self.deweight.size(1))
    self.deweight.data.uniform_(-stdv, stdv)
    self.vbias.data.uniform_(-stdv, stdv)

def forward(self, x):
    return self.dropout(self.enc_act_func(F.linear(x, self.weight, self.bias)))

def encode(self, x, train=True):
    if train:
        self.dropout.train()
    else:
        self.dropout.eval()
    return self.dropout(self.enc_act_func(F.linear(x, self.weight, self.bias)))

def encodeBatch(self, dataloader):
    use_cuda = torch.cuda.is_available()
    encoded = []
    for batch_idx, inputs in enumerate(dataloader):
        inputs = np.reshape(inputs, [-1, 512])
        inputs = torch.Tensor(inputs)
        #inputs = inputs.view(inputs.size(0), -1).float()
        if use_cuda:
            inputs = inputs.cuda()
        inputs = Variable(inputs)
        hidden = self.encode(inputs, train=False)
        encoded.append(hidden.data.cpu())

    encoded = torch.cat(encoded, dim=0)
    return encoded

def decode(self, x, binary=False):
    if not binary:
        return F.linear(x, self.deweight, self.vbias)
    else:
        return F.sigmoid(F.linear(x, self.deweight, self.vbias))

def fit(self, trainloader, validloader, lr=0.001, batch_size=128, num_epochs=10, corrupt=0.3,
    loss_type="mse"):
    """
    data_x: FloatTensor
    valid_x: FloatTensor
    """
    use_cuda = torch.cuda.is_available()
    if use_cuda:
        self.cuda()
    print("=====Denoising Autoencoding layer=======")
    optimizer = optim.Adam(filter(lambda p: p.requires_grad, self.parameters()), lr=lr)
    if loss_type=="mse":
        criterion = MSELoss()
    elif loss_type=="cross-entropy":
        criterion = BCELoss()

    # validate
    total_loss = 0.0
    total_num = 0
    for batch_idx, inputs in enumerate(validloader):
        #inputs = torch.Tensor(inputs)
        #inputs = np.reshape(inputs,[-1,512])
        #inputs=torch.Tensor(inputs)
        #inputs = inputs.view(inputs.size(0), -1).float()
        inputs= torch.from_numpy(inputs)
        print(type(inputs))
        if use_cuda:
            inputs = inputs.cuda()
        inputs = Variable(inputs)
        hidden = self.encode(inputs)
        if loss_type=="cross-entropy":
            outputs = self.decode(hidden, binary=True)
        else:
            outputs = self.decode(hidden)

        valid_recon_loss = criterion(outputs, inputs)
        total_loss += valid_recon_loss.data* len(inputs)
        total_num += inputs.size()[0]

    valid_loss = total_loss / total_num
    print("#Epoch 0: Valid Reconstruct Loss: %.3f" % (valid_loss))