Usually this is done with some criterion like mean-squared error or cross-entropy loss and an optimizer like stochastic gradient descent.
The MNIST example is a good starting point for a typical training loop: examples/main.py at 2639cf050493df9d3cbf065d45e6025733add0f4 · pytorch/examples · GitHub