diff --git a/docs/api/paddle/io/RandomSampler_cn.rst b/docs/api/paddle/io/RandomSampler_cn.rst index 6b1df9b6918..da91031c18d 100644 --- a/docs/api/paddle/io/RandomSampler_cn.rst +++ b/docs/api/paddle/io/RandomSampler_cn.rst @@ -11,7 +11,7 @@ RandomSampler ::::::::: - **data_source** (Dataset) - 此参数必须是 :ref:`cn_api_paddle_io_Dataset` 或 :ref:`cn_api_paddle_io_IterableDataset` 的一个子类实例或实现了 ``__len__`` 的 Python 对象,用于生成样本下标。默认值为 None。 - **replacement** (bool,可选) - 如果为 ``False`` 则会采样整个数据集,如果为 ``True`` 则会按 ``num_samples`` 指定的样本数采集。默认值为 ``False`` 。 - - **num_samples** (int,可选) - 如果 ``replacement`` 设置为 ``True`` 则按此参数采集对应的样本数。默认值为 None,不启用。 + - **num_samples** (int,可选) - 按此参数采集对应的样本数。默认值为 None,此时设为 ``data_source`` 的长度。 - **generator** (Generator,可选) - 指定采样 ``data_source`` 的采样器。默认值为 None,不启用。 返回 diff --git a/docs/api/paddle/io/random_split_cn.rst b/docs/api/paddle/io/random_split_cn.rst index f8896a11da6..853ca17fab4 100644 --- a/docs/api/paddle/io/random_split_cn.rst +++ b/docs/api/paddle/io/random_split_cn.rst @@ -11,7 +11,7 @@ random_split :::::::::::: - **dataset** (Dataset) - 此参数必须是 ``paddle.io.Dataset`` 或 ``paddle.io.IterableDataset`` 的一个子类实例或实现了 ``__len__`` 的 Python 对象,用于生成样本下标。默认值为 None。 - - **lengths** (list) - 总和为原数组长度的,子集合长度数组。 + - **lengths** (list) - 总和为原数组长度,表示子集合长度数组;或总和为 1.0,表示子集合长度占比的数组。 - **generator** (Generator,可选) - 指定采样 ``data_source`` 的采样器。默认值为 None。 返回 diff --git a/docs/api/paddle/nn/Layer_cn.rst b/docs/api/paddle/nn/Layer_cn.rst index 12d1c7655ab..95ec2539d26 100644 --- a/docs/api/paddle/nn/Layer_cn.rst +++ b/docs/api/paddle/nn/Layer_cn.rst @@ -228,11 +228,15 @@ sublayers(include_self=False) COPY-FROM: paddle.nn.Layer.sublayers -clear_gradients() +clear_gradients(set_to_zero=True) ''''''''' 清除该层所有参数的梯度。 +**参数** + + - **set_to_zero** (bool,可选) - 是否将可训练参数的梯度设置为 0 ,若为 False 则设为 None。默认值:True。 + **返回** 无 diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.Module.zero_grad.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.Module.zero_grad.md index 73c15410c27..0d3e7ac1676 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.Module.zero_grad.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/nn/torch.nn.Module.zero_grad.md @@ -1,4 +1,4 @@ -## [torch 参数更多]torch.nn.Module.zero_grad +## [参数不一致]torch.nn.Module.zero_grad ### [torch.nn.Module.zero_grad](https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.zero_grad) @@ -9,13 +9,23 @@ torch.nn.Module.zero_grad(set_to_none=True) ### [paddle.nn.Layer.clear_gradients](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/Layer_cn.html#clear-gradients) ```python -paddle.nn.Layer.clear_gradients() +paddle.nn.Layer.clear_gradients(set_to_zero=True) ``` -PyTorch 相比 Paddle 支持更多其他参数,具体如下: +PyTorch 的 `Module.zero_grad` 参数与 Paddle 的 `Layer.clear_gradients` 参数用法刚好相反,具体如下: ### 参数映射 | PyTorch | PaddlePaddle | 备注 | | ----------- | ------------ | ------------------------------------------------ | -| set_to_none | - | 是否设置为 None,Paddle 无此参数,暂无转写方式。 | +| set_to_none | set_to_zero | 设置如何清空梯度,PyTorch 默认 set_to_none 为 True,Paddle 默认 set_to_zero 为 True,两者功能刚好相反,Paddle 需设置为 False。 | + +### 转写示例 + +```python +# PyTorch 写法 +torch.nn.Module.zero_grad(set_to_none=True) + +# Paddle 写法 +paddle.nn.Layer.clear_gradients(set_to_zero=False) +``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/utils/torch.utils.data.RandomSampler.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/utils/torch.utils.data.RandomSampler.md index 1d3fe88583d..36902daceb2 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/utils/torch.utils.data.RandomSampler.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/utils/torch.utils.data.RandomSampler.md @@ -20,5 +20,5 @@ paddle.io.RandomSampler(data_source, replacement=False, num_samples=None, genera | ----------- | ------------ | -------------------------------------------------------------------- | | data_source | data_source | Dataset 或 IterableDataset 的一个子类实例或实现了 `__len__` 的 Python 对象。 | | replacement | replacement | 如果为 False 则会采样整个数据集。 | -| num_samples | num_samples | 如果 replacement 设置为 True 则按此参数采集对应的样本数。 | +| num_samples | num_samples | 按此参数采集对应的样本数。 | | generator | generator | 指定采样 data_source 的采样器。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/utils/torch.utils.data.random_split.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/utils/torch.utils.data.random_split.md index a4046adc75d..9baa9243a7b 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/utils/torch.utils.data.random_split.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/utils/torch.utils.data.random_split.md @@ -1,4 +1,4 @@ -## [ 参数不一致 ]torch.utils.data.random_split +## [ 参数完全一致 ]torch.utils.data.random_split ### [torch.utils.data.random_split](https://pytorch.org/docs/stable/data.html?highlight=torch+utils+data+random_split#torch.utils.data.random_split) ```python @@ -15,27 +15,12 @@ paddle.io.random_split(dataset, generator=None) ``` -两者参数除 lengths 外用法一致,具体如下: -### 参数差异 +两者功能一致,参数完全一致,具体如下: + +### 参数映射 + | PyTorch | PaddlePaddle | 备注 | | ------------- | ------------ |---------------------------------------------------------------------| | dataset | dataset | 表示可迭代数据集。 | -| lengths | lengths | PyTorch:可为子集合长度列表,列表总和为原数组长度。也可为子集合所占比例列表,列表总和为 1.0。PaddlePaddle: 子集合长度列表,列表总和为原数组长度 | +| lengths | lengths | 可为子集合长度列表,列表总和为原数组长度。也可为子集合所占比例列表,列表总和为 1.0。 | | generator | generator | 指定采样 data_source 的采样器。默认值为 None。 | - -### 转写示例 -#### lenghts: 子集合长度列表 -```python -# PyTorch 写法 -lengths = [0.3, 0.3, 0.4] -datasets = torch.utils.data.random_split(dataset, - lengths, - generator=torch.manual_seed(0)) - -# Paddle 写法 -lengths = [0.3, 0.3, 0.4] -lengths = [length * len(dataset) for length in lengths] -datasets = paddle.io.random_split(dataset, - lengths, - generator=paddle.seed(0)) -```