This is typically used to register a buffer that should not to be considered a model parameter. For example, BatchNorm’s `running_mean` is not a parameter, but is part of the persistent state.
The hook will be called every time after [`forward()`](#torch.nn.Module.forward"torch.nn.Module.forward") has computed an output. It should have the following signature:
The hook will be called every time before [`forward()`](#torch.nn.Module.forward"torch.nn.Module.forward") is invoked. It should have the following signature:
```py
...
...
@@ -479,9 +481,9 @@ hook(module, input) -> None
```
The hook should not modify the input.
此钩子函数不能进行会修改 input 这个参数的操作。
| Returns: | a handle that can be used to remove the added hook by calling `handle.remove()` |
Its signature is similar to [`torch.Tensor.to()`](tensors.html#torch.Tensor.to"torch.Tensor.to"), but only accepts floating point desired `dtype` s. In addition, this method will only cast the floating point parameters and buffers to `dtype` (if given). The integral parameters and buffers will be moved `device`, if that is given, but with dtypes unchanged. When `non_blocking` is set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.
此函数的函数签名跟[`torch.Tensor.to()`](tensors.html#torch.Tensor.to"torch.Tensor.to")函数的函数签名很相似,只不过这个函数`dtype`参数只接受浮点数类型的dtype,如float, double, half( floating point desired `dtype` s)。同时,这个方法只会将浮点数类型的参数和缓冲区(the floating point parameters and buffers)转化为`dtype`(如果输入参数中给定的话)的数据类型。而对于整数类型的参数和缓冲区(the integral parameters and buffers),即便输入参数中给定了`dtype`,也不会进行转换操作,而如果给定了 `device`参数,移动操作则会正常进行。当`non_blocking`参数被设置为True之后,此函数会尽可能地相对于 host 进行异步的 转换/移动 操作,比如,将存储在固定内存(pinned memory)上的CPU Tensors移动到CUDA设备上。
See below for examples.
例子在下面。
Note
This method modifies the module in-place.
这个方法对模块的修改都是in-place操作。
Parameters:
to()函数的参数:
***device** (`torch.device`) – the desired device of the parameters and buffers in this module
***dtype** (`torch.dtype`) – the desired floating point type of the floating point parameters and buffers in this module
***tensor** ([_torch.Tensor_](tensors.html#torch.Tensor"torch.Tensor")) – Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g. [`Dropout`](#torch.nn.Dropout"torch.nn.Dropout"), `BatchNorm`, etc.