{ "cells": [ { "cell_type": "markdown", "id": "1e32df1c", "metadata": { "origin_pos": 0 }, "source": [ "# Documentation\n" ] }, { "cell_type": "markdown", "id": "f118a26b", "metadata": { "origin_pos": 2, "tab": [ "pytorch" ] }, "source": [ "Do những hạn chế về độ dài của cuốn sách này, chúng ta không thể giới thiệu mọi hàm và lớp PyTorch (và có lẽ bạn sẽ không muốn chúng tôi). Các tài liệu API và các hướng dẫn bổ sung và ví dụ cung cấp nhiều tài liệu ngoài cuốn sách. Trong phần này, chúng tôi cung cấp cho bạn một số hướng dẫn để khám phá API PyTorch.\n" ] }, { "cell_type": "markdown", "id": "100c6ddd", "metadata": { "origin_pos": 4 }, "source": [ "## Tìm tất cả các hàm và lớp học trong một mô-đun\n", "\n", "Để biết các hàm và lớp nào có thể được gọi trong một mô-đun, chúng ta gọi hàm `dir`. Ví dụ, chúng ta có thể (** truy vấn tất cả các thuộc tính trong mô-đun để tạo ra số ngẫu nhiên**):\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "11641fcd", "metadata": { "attributes": { "classes": [], "id": "", "n": "1" }, "execution": { "iopub.execute_input": "2022-04-26T12:26:35.602700Z", "iopub.status.busy": "2022-04-26T12:26:35.602421Z", "iopub.status.idle": "2022-04-26T12:26:36.341254Z", "shell.execute_reply": "2022-04-26T12:26:36.340336Z" }, "origin_pos": 6, "tab": [ "pytorch" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['AbsTransform', 'AffineTransform', 'Bernoulli', 'Beta', 'Binomial', 'CatTransform', 'Categorical', 'Cauchy', 'Chi2', 'ComposeTransform', 'ContinuousBernoulli', 'CorrCholeskyTransform', 'Dirichlet', 'Distribution', 'ExpTransform', 'Exponential', 'ExponentialFamily', 'FisherSnedecor', 'Gamma', 'Geometric', 'Gumbel', 'HalfCauchy', 'HalfNormal', 'Independent', 'IndependentTransform', 'Kumaraswamy', 'LKJCholesky', 'Laplace', 'LogNormal', 'LogisticNormal', 'LowRankMultivariateNormal', 'LowerCholeskyTransform', 'MixtureSameFamily', 'Multinomial', 'MultivariateNormal', 'NegativeBinomial', 'Normal', 'OneHotCategorical', 'OneHotCategoricalStraightThrough', 'Pareto', 'Poisson', 'PowerTransform', 'RelaxedBernoulli', 'RelaxedOneHotCategorical', 'ReshapeTransform', 'SigmoidTransform', 'SoftmaxTransform', 'StackTransform', 'StickBreakingTransform', 'StudentT', 'TanhTransform', 'Transform', 'TransformedDistribution', 'Uniform', 'VonMises', 'Weibull', 'Wishart', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'bernoulli', 'beta', 'biject_to', 'binomial', 'categorical', 'cauchy', 'chi2', 'constraint_registry', 'constraints', 'continuous_bernoulli', 'dirichlet', 'distribution', 'exp_family', 'exponential', 'fishersnedecor', 'gamma', 'geometric', 'gumbel', 'half_cauchy', 'half_normal', 'identity_transform', 'independent', 'kl', 'kl_divergence', 'kumaraswamy', 'laplace', 'lkj_cholesky', 'log_normal', 'logistic_normal', 'lowrank_multivariate_normal', 'mixture_same_family', 'multinomial', 'multivariate_normal', 'negative_binomial', 'normal', 'one_hot_categorical', 'pareto', 'poisson', 'register_kl', 'relaxed_bernoulli', 'relaxed_categorical', 'studentT', 'transform_to', 'transformed_distribution', 'transforms', 'uniform', 'utils', 'von_mises', 'weibull', 'wishart']\n" ] } ], "source": [ "import torch\n", "\n", "print(dir(torch.distributions))" ] }, { "cell_type": "markdown", "id": "a9fe9648", "metadata": { "origin_pos": 8 }, "source": [ "Nói chung, chúng ta có thể bỏ qua các hàm bắt đầu và kết thúc bằng `__` (các đối tượng đặc biệt trong Python) hoặc các hàm bắt đầu bằng một `_` duy nhất (thường là hàm nội bộ). Dựa trên hàm hoặc tên thuộc tính còn lại, chúng ta có thể nguy hiểm đoán rằng mô-đun này cung cấp các phương pháp khác nhau để tạo ra các số ngẫu nhiên, bao gồm lấy mẫu từ phân phối thống nhất (`uniform`), phân phối bình thường (`normal`), và phân phối đa phương thức (`multinomial`). \n", "\n", "## Tìm cách sử dụng các hàm và lớp cụ thể\n", "\n", "Đối với các hướng dẫn cụ thể hơn về cách sử dụng một hàm hoặc lớp nhất định, chúng ta có thể gọi hàm `help`. Ví dụ, chúng ta hãy [** khám phá các hướng dẫn sử dụng cho hàm `ones` của tensor**].\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "27449c85", "metadata": { "execution": { "iopub.execute_input": "2022-04-26T12:26:36.344877Z", "iopub.status.busy": "2022-04-26T12:26:36.344449Z", "iopub.status.idle": "2022-04-26T12:26:36.349889Z", "shell.execute_reply": "2022-04-26T12:26:36.349055Z" }, "origin_pos": 10, "tab": [ "pytorch" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function ones:\n", "\n", "ones(...)\n", " ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor\n", " \n", " Returns a tensor filled with the scalar value `1`, with the shape defined\n", " by the variable argument :attr:`size`.\n", " \n", " Args:\n", " size (int...): a sequence of integers defining the shape of the output tensor.\n", " Can be a variable number of arguments or a collection like a list or tuple.\n", " \n", " Keyword arguments:\n", " out (Tensor, optional): the output tensor.\n", " dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.\n", " Default: if ``None``, uses a global default (see :func:`torch.set_default_tensor_type`).\n", " layout (:class:`torch.layout`, optional): the desired layout of returned Tensor.\n", " Default: ``torch.strided``.\n", " device (:class:`torch.device`, optional): the desired device of returned tensor.\n", " Default: if ``None``, uses the current device for the default tensor type\n", " (see :func:`torch.set_default_tensor_type`). :attr:`device` will be the CPU\n", " for CPU tensor types and the current CUDA device for CUDA tensor types.\n", " requires_grad (bool, optional): If autograd should record operations on the\n", " returned tensor. Default: ``False``.\n", " \n", " Example::\n", " \n", " >>> torch.ones(2, 3)\n", " tensor([[ 1., 1., 1.],\n", " [ 1., 1., 1.]])\n", " \n", " >>> torch.ones(5)\n", " tensor([ 1., 1., 1., 1., 1.])\n", "\n" ] } ], "source": [ "help(torch.ones)" ] }, { "cell_type": "markdown", "id": "a82ab083", "metadata": { "origin_pos": 12 }, "source": [ "Từ tài liệu, chúng ta có thể thấy rằng hàm `ones` tạo ra một tensor mới với hình dạng được chỉ định và đặt tất cả các phần tử thành giá trị của 1. Bất cứ khi nào có thể, bạn nên (** chạy một bài kiểm tra nhanh**) để xác nhận giải thích của bạn:\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "1c2a5fbf", "metadata": { "execution": { "iopub.execute_input": "2022-04-26T12:26:36.352843Z", "iopub.status.busy": "2022-04-26T12:26:36.352626Z", "iopub.status.idle": "2022-04-26T12:26:36.363958Z", "shell.execute_reply": "2022-04-26T12:26:36.363319Z" }, "origin_pos": 14, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "tensor([1., 1., 1., 1.])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.ones(4)" ] }, { "cell_type": "markdown", "id": "2ae857b9", "metadata": { "origin_pos": 16 }, "source": [ "Trong sổ ghi chép Jupyter, chúng ta có thể sử dụng `? `để hiển thị tài liệu trong một cửa sổ khác. Ví dụ: `list? `sẽ tạo nội dung gần giống với `help(list)`, hiển thị nó trong một cửa sổ trình duyệt mới. Ngoài ra, nếu chúng ta sử dụng hai dấu hỏi, chẳng hạn như `list?? `, mã Python thực hiện hàm cũng sẽ được hiển thị. \n", "\n", "## Tóm tắt\n", "\n", "* Tài liệu chính thức cung cấp rất nhiều mô tả và ví dụ vượt ra ngoài cuốn sách này.\n", "* Chúng ta có thể tra cứu tài liệu cho việc sử dụng API bằng cách gọi các hàm `dir` và `help`, hoặc `? ` and `?? `trong máy tính xách tay Jupyter.\n", "\n", "## Bài tập\n", "\n", "1. Tra cứu tài liệu cho bất kỳ chức năng hoặc lớp học nào trong khuôn khổ học sâu. Bạn cũng có thể tìm thấy tài liệu trên trang web chính thức của khuôn khổ?\n" ] }, { "cell_type": "markdown", "id": "16088e91", "metadata": { "origin_pos": 18, "tab": [ "pytorch" ] }, "source": [ "[Discussions](https://discuss.d2l.ai/t/39)\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }