第2章準備
2.7.2. Specific Functions and Classes2.7.2 特定の関数とクラス
For specific instructions on how to use a given function or class, we can invoke the
help function. As an example, let’s explore the usage instructions for tensors’ ones function.英語のヒント
ある関数やクラスの具体的な使い方の説明を見るには、
help関数を呼び出せばよい。例として、テンソルのones関数の使い方の説明を調べてみよう。help(torch.ones)Help on built-in function ones in module torch:
ones(...)
ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
Returns a tensor filled with the scalar value 1, with the shape defined
by the variable argument size.
Args:
size (int...): a sequence of integers defining the shape of the output tensor.
Can be a variable number of arguments or a collection like a list or tuple.
Keyword arguments:
out (Tensor, optional): the output tensor.
dtype (torch.dtype, optional): the desired data type of returned tensor.
Default: if None, uses a global default (see torch.set_default_tensor_type()).
layout (torch.layout, optional): the desired layout of returned Tensor.
Default: torch.strided.
device (torch.device, optional): the desired device of returned tensor.
Default: if None, uses the current device for the default tensor type
(see torch.set_default_tensor_type()). device will be the CPU
for CPU tensor types and the current CUDA device for CUDA tensor types.
requires_grad (bool, optional): If autograd should record operations on the
returned tensor. Default: False.
Example::
>>> torch.ones(2, 3)
tensor([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> torch.ones(5)
tensor([ 1., 1., 1., 1., 1.])From the documentation, we can see that the
ones function creates a new tensor with the specified shape and sets all the elements to the value of 1. Whenever possible, you should run a quick test to confirm your interpretation:英語のヒント
文書から、
ones関数は指定した形状の新しいテンソルを作り、すべての要素を値1にすることがわかる。できるかぎり、手早くテストを走らせて、自分の解釈を確かめるべきである。torch.ones(4)tensor([1., 1., 1., 1.])In the Jupyter notebook, we can use
? to display the document in another window. For example, list? will create content that is almost identical to help(list), displaying it in a new browser window. In addition, if we use two question marks, such as list??, the Python code implementing the function will also be displayed.英語のヒント
Jupyterノートブックでは、
?を使って別のウィンドウに文書を表示できる。たとえばlist?は、help(list)とほぼ同じ内容を作り、新しいブラウザのウィンドウに表示する。さらに、list??のように疑問符を二つ使うと、その関数を実装しているPythonのコードも表示される。The official documentation provides plenty of descriptions and examples that are beyond this book. We emphasize important use cases that will get you started quickly with practical problems, rather than completeness of coverage. We also encourage you to study the source code of the libraries to see examples of high-quality implementations of production code. By doing this you will become a better engineer in addition to becoming a better scientist.
英語のヒント
公式の文書には、この本の範囲を超える説明や例がたくさんある。私たちが重点を置くのは、網羅することではなく、実際の問題にすぐ取りかかれるような大事な使い方である。また、ライブラリのソースコードを調べて、実運用のコードの質の高い実装例を見ることも勧めたい。そうすれば、よりよい科学者になるだけでなく、よりよい技術者にもなれる。