I would like to use PyTorch as a scientific computation package. It has much to recommend it in that respect - its Tensors are basically GPU-accelerated numpy arrays, and its autograd
mechanism is potentially useful for a lot of things besides neural networks.
However, the available tutorials and documentation seem strongly geared towards quickly getting people up and running using it for machine learning. Although there is lots of good information available on the Tensor
and Variable
classes (and I understand that material reasonably well), the nn
and optim
packages always seem to be introduced by example rather than by explaining the API, which makes it hard to figure out exactly what's going on.
My main question at this point is whether I can use the optim
package without also using the nn
package, and if so how to do so. Of course I can always implement my simulations as subclasses of nn.Module
even though they are not neural networks, but I would like to understand what happens under the hood when I do this, and what benefits/drawbacks it would give for my particular application.
More broadly, I would appreciate pointers to any resource that gives more of a logical overview of the API (for nn
and optim
specifically), rather than just presenting examples.
This is a partial self-answer to the specific question about using optim
without using nn
. The answer is, yes, you can do that. In fact, from looking at the source code, the optim
package doesn't know anything about nn
and only cares about Variables and tensors.
The documentation gives the following incomplete example:
optimizer = optim.Adam([var1, var2], lr = 0.0001)
and then later:
for input, target in dataset:
optimizer.zero_grad()
output = model(input)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
The function model
isn't defined anywhere and looks like it might be something to do with nn
, but in fact it can just be a Python function that computes output
from input
using var1
and var2
as parameters, as long as all the intermediate steps are done using Variables so that it can be differentiated. The call to optimizer.step()
will update the values of var1
and var2
automatically.
In terms of the structure of PyTorch overall, it seems that optim
and nn
are independent of one another, with nn
being basically just a convenient way to chain differentiable functions together, along with a library of such functions that are useful in machine learning. I would still appreciate pointers to a good technical overview of the whole package, though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.