How could I choose cost function in Qiskit TwoLayerQNN?

So here is the problem, I've found that in a TwoLayerQNN, the backward gradient is only to minimize the expectation of observable I've chosen. But I'm not going to minimize the predict of the input, instead, I need to minimize the cost function. So how can I choose my own cost function without using NeuralNetworkClassifier? thanks.

Quantum neural networks in Qiskit Machine Learning do not store weights, they only provide a framework for computations of forward and backward passes. They are not aware of any algorithms that can be applied on top of them. Thus, they are not aware of any optimization processes. So, they don't have any references to loss/cost functions. While algorithms, like the above mentioned NeuralNetworkClassifer, and everything in the qiskit_machine_learning.algorithms represent trainable models, so they know what to optimize and how to do this. If you need a custom cost function, so you should consider one of the algorithms.