Generating ideal input from a backpropagation of a pre-trained model

Is that possible to generate an input which seems ideal w.r.t. model by backpropagation of the model?

Here I have such arch for classification sequences into 2 categories:

class SDNet(nn.Module):
    def __init__(self, input_size=2, hidden_layer_size=10, output_size=1):
        super().__init__()
        self.hidden_layer_size = hidden_layer_size

        self.lstm = nn.LSTM(input_size, hidden_layer_size)

        self.linear = nn.Linear(hidden_layer_size, output_size)
        self.sigmoid = nn.Sigmoid()

        self.hidden_cell = None

    def forward(self, input_seq):

        lstm_out, self.hidden_cell = self.lstm(input_seq.view(len(input_seq) ,1, -1), self.hidden_cell)
        out = self.linear(lstm_out.view(len(input_seq), -1))[-1]
        out = self.sigmoid(out)
        return out
    
    def init_hidden(self):
        self.hidden_cell = (torch.zeros(1,1,self.hidden_layer_size),
                            torch.zeros(1,1,self.hidden_layer_size))

    def evaluate(self, seq):
	    #rnn.eval()
	    self.init_hidden()
	    seq = torch.tensor(seq, dtype = torch.float)
	    with torch.no_grad():
	            y_pred = self(seq)
	    return y_pred.item()

Can I take an output like for example 1 or 0 (or near it cause an activation is sigmoid) and generate an input for the model which will give such a prediction???

You could probably set requires_grad=True in your input, calculate the gradients using your desired target, and optimize the input instead of the model parameters.

input_tensor = torch.tensor(two_line_seq, dtype = torch.float, requires_grad=True)

?.backward(torch.tensor([1.]))
input_tensor += input_tensor.grad

But from which tensor I must make backpropagation?

Suppose I have to do it with last output of the net then we have

input_tensor = torch.tensor(two_line_seq, dtype = torch.float, requires_grad=True)
pred = rnn(input_tensor)
single_loss = loss(pred, label)
single_loss.backward(torch.tensor([1.]))
input_tensor += input_tensor.grad

but torch.tensor([1.]) is the default argument for backward so I’m confused…

Can you tell on which tensor I have to work backwards?
Is that have to be model’s loss?

You could pick a specific class of your pretrained model and use the standard training routine.
However, this should result in some pattern-like input image, but I assume that’s what you are looking for.

Also, this blogpost might be interesting to read for some ideas.