未验证 提交 02538dfc 编写于 作者: L LoneRanger 提交者: GitHub

[xdoctest] reformat example code with google style in No.16-No.20 (#56296)

* fix sample codes

* fix bug

* fix bug

* fix bug
上级 6f93f9c7
...@@ -43,18 +43,18 @@ class Spectrogram(nn.Layer): ...@@ -43,18 +43,18 @@ class Spectrogram(nn.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.audio.features import Spectrogram >>> from paddle.audio.features import Spectrogram
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
feature_extractor = Spectrogram(n_fft=512, window = 'hann', power = 1.0) >>> feature_extractor = Spectrogram(n_fft=512, window = 'hann', power = 1.0)
feats = feature_extractor(waveform) >>> feats = feature_extractor(waveform)
""" """
def __init__( def __init__(
...@@ -128,18 +128,18 @@ class MelSpectrogram(nn.Layer): ...@@ -128,18 +128,18 @@ class MelSpectrogram(nn.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.audio.features import MelSpectrogram >>> from paddle.audio.features import MelSpectrogram
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
feature_extractor = MelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0) >>> feature_extractor = MelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0)
feats = feature_extractor(waveform) >>> feats = feature_extractor(waveform)
""" """
def __init__( def __init__(
...@@ -231,18 +231,18 @@ class LogMelSpectrogram(nn.Layer): ...@@ -231,18 +231,18 @@ class LogMelSpectrogram(nn.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.audio.features import LogMelSpectrogram >>> from paddle.audio.features import LogMelSpectrogram
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
feature_extractor = LogMelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0) >>> feature_extractor = LogMelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0)
feats = feature_extractor(waveform) >>> feats = feature_extractor(waveform)
""" """
def __init__( def __init__(
...@@ -335,18 +335,18 @@ class MFCC(nn.Layer): ...@@ -335,18 +335,18 @@ class MFCC(nn.Layer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.audio.features import MFCC >>> from paddle.audio.features import MFCC
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
feature_extractor = MFCC(sr=sample_rate, n_fft=512, window = 'hann') >>> feature_extractor = MFCC(sr=sample_rate, n_fft=512, window = 'hann')
feats = feature_extractor(waveform) >>> feats = feature_extractor(waveform)
""" """
def __init__( def __init__(
......
...@@ -34,12 +34,12 @@ def hz_to_mel( ...@@ -34,12 +34,12 @@ def hz_to_mel(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
val = 3.0 >>> val = 3.0
htk_flag = True >>> htk_flag = True
mel_paddle_tensor = paddle.audio.functional.hz_to_mel( >>> mel_paddle_tensor = paddle.audio.functional.hz_to_mel(
paddle.to_tensor(val), htk_flag) ... paddle.to_tensor(val), htk_flag)
""" """
if htk: if htk:
...@@ -90,13 +90,13 @@ def mel_to_hz( ...@@ -90,13 +90,13 @@ def mel_to_hz(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
val = 3.0
htk_flag = True
mel_paddle_tensor = paddle.audio.functional.mel_to_hz(
paddle.to_tensor(val), htk_flag)
>>> val = 3.0
>>> htk_flag = True
>>> mel_paddle_tensor = paddle.audio.functional.mel_to_hz(
... paddle.to_tensor(val), htk_flag)
...
""" """
if htk: if htk:
return 700.0 * (10.0 ** (mel / 2595.0) - 1.0) return 700.0 * (10.0 ** (mel / 2595.0) - 1.0)
...@@ -142,15 +142,15 @@ def mel_frequencies( ...@@ -142,15 +142,15 @@ def mel_frequencies(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
n_mels = 64 >>> n_mels = 64
f_min = 0.5 >>> f_min = 0.5
f_max = 10000 >>> f_max = 10000
htk_flag = True >>> htk_flag = True
paddle_mel_freq = paddle.audio.functional.mel_frequencies( >>> paddle_mel_freq = paddle.audio.functional.mel_frequencies(
n_mels, f_min, f_max, htk_flag, 'float64') ... n_mels, f_min, f_max, htk_flag, 'float64')
""" """
# 'Center freqs' of mel bands - uniformly spaced between limits # 'Center freqs' of mel bands - uniformly spaced between limits
min_mel = hz_to_mel(f_min, htk=htk) min_mel = hz_to_mel(f_min, htk=htk)
...@@ -174,11 +174,11 @@ def fft_frequencies(sr: int, n_fft: int, dtype: str = 'float32') -> Tensor: ...@@ -174,11 +174,11 @@ def fft_frequencies(sr: int, n_fft: int, dtype: str = 'float32') -> Tensor:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sr = 16000 >>> sr = 16000
n_fft = 128 >>> n_fft = 128
fft_freq = paddle.audio.functional.fft_frequencies(sr, n_fft) >>> fft_freq = paddle.audio.functional.fft_frequencies(sr, n_fft)
""" """
return paddle.linspace(0, float(sr) / 2, int(1 + n_fft // 2), dtype=dtype) return paddle.linspace(0, float(sr) / 2, int(1 + n_fft // 2), dtype=dtype)
...@@ -211,11 +211,11 @@ def compute_fbank_matrix( ...@@ -211,11 +211,11 @@ def compute_fbank_matrix(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
n_mfcc = 23 >>> sr = 23
n_mels = 51 >>> n_fft = 51
paddle_dct = paddle.audio.functional.create_dct(n_mfcc, n_mels) >>> fbank = paddle.audio.functional.compute_fbank_matrix(sr, n_fft)
""" """
if f_max is None: if f_max is None:
...@@ -276,11 +276,11 @@ def power_to_db( ...@@ -276,11 +276,11 @@ def power_to_db(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
val = 3.0 >>> val = 3.0
decibel_paddle = paddle.audio.functional.power_to_db( >>> decibel_paddle = paddle.audio.functional.power_to_db(
paddle.to_tensor(val)) ... paddle.to_tensor(val))
""" """
if amin <= 0: if amin <= 0:
raise Exception("amin must be strictly positive") raise Exception("amin must be strictly positive")
...@@ -320,10 +320,10 @@ def create_dct( ...@@ -320,10 +320,10 @@ def create_dct(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
n_mfcc = 23 >>> n_mfcc = 23
n_mels = 257 >>> n_mels = 257
dct = paddle.audio.functional.create_dct(n_mfcc, n_mels) >>> dct = paddle.audio.functional.create_dct(n_mfcc, n_mels)
""" """
n = paddle.arange(n_mels, dtype=dtype) n = paddle.arange(n_mels, dtype=dtype)
k = paddle.arange(n_mfcc, dtype=dtype).unsqueeze(1) k = paddle.arange(n_mfcc, dtype=dtype).unsqueeze(1)
......
...@@ -352,13 +352,13 @@ def get_window( ...@@ -352,13 +352,13 @@ def get_window(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
n_fft = 512 >>> n_fft = 512
cosine_window = paddle.audio.functional.get_window('cosine', n_fft) >>> cosine_window = paddle.audio.functional.get_window('cosine', n_fft)
std = 7 >>> std = 7
gaussian_window = paddle.audio.functional.get_window(('gaussian',std), n_fft) >>> gaussian_window = paddle.audio.functional.get_window(('gaussian',std), n_fft)
""" """
sym = not fftbins sym = not fftbins
......
...@@ -501,21 +501,23 @@ def jacobian( ...@@ -501,21 +501,23 @@ def jacobian(
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x1 = paddle.randn([3, ]) >>> x1 = paddle.randn([3, ])
x2 = paddle.randn([3, ]) >>> x2 = paddle.randn([3, ])
x1.stop_gradient = False >>> x1.stop_gradient = False
x2.stop_gradient = False >>> x2.stop_gradient = False
y = x1 + x2 >>> y = x1 + x2
J = paddle.autograd.jacobian(y, (x1, x2)) >>> J = paddle.autograd.jacobian(y, (x1, x2))
J_y_x1 = J[0][:] # evaluate result of dy/dx1 >>> J_y_x1 = J[0][:] # evaluate result of dy/dx1
J_y_x2 = J[1][:] # evaluate result of dy/dx2 >>> J_y_x2 = J[1][:] # evaluate result of dy/dx2
print(J_y_x1.shape) # [3, 3] >>> print(J_y_x1.shape)
print(J_y_x2.shape) # [3, 3] [3, 3]
>>> print(J_y_x2.shape)
[3, 3]
""" """
if batch_axis is not None and batch_axis != 0: if batch_axis is not None and batch_axis != 0:
...@@ -583,25 +585,29 @@ def hessian( ...@@ -583,25 +585,29 @@ def hessian(
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x1 = paddle.randn([3, ]) >>> x1 = paddle.randn([3, ])
x2 = paddle.randn([4, ]) >>> x2 = paddle.randn([4, ])
x1.stop_gradient = False >>> x1.stop_gradient = False
x2.stop_gradient = False >>> x2.stop_gradient = False
y = x1.sum() + x2.sum() >>> y = x1.sum() + x2.sum()
H = paddle.autograd.hessian(y, (x1, x2)) >>> H = paddle.autograd.hessian(y, (x1, x2))
H_y_x1_x1 = H[0][0][:] # evaluate result of ddy/dx1x1 >>> H_y_x1_x1 = H[0][0][:] # evaluate result of ddy/dx1x1
H_y_x1_x2 = H[0][1][:] # evaluate result of ddy/dx1x2 >>> H_y_x1_x2 = H[0][1][:] # evaluate result of ddy/dx1x2
H_y_x2_x1 = H[1][0][:] # evaluate result of ddy/dx2x1 >>> H_y_x2_x1 = H[1][0][:] # evaluate result of ddy/dx2x1
H_y_x2_x2 = H[1][1][:] # evaluate result of ddy/dx2x2 >>> H_y_x2_x2 = H[1][1][:] # evaluate result of ddy/dx2x2
print(H_y_x1_x1.shape) # [3, 3] >>> print(H_y_x1_x1.shape)
print(H_y_x1_x2.shape) # [3, 4] [3, 3]
print(H_y_x2_x1.shape) # [4, 3] >>> print(H_y_x1_x2.shape)
print(H_y_x2_x2.shape) # [4, 4] [3, 4]
>>> print(H_y_x2_x1.shape)
[4, 3]
>>> print(H_y_x2_x2.shape)
[4, 4]
""" """
if batch_axis is None: if batch_axis is None:
......
...@@ -44,34 +44,39 @@ def backward(tensors, grad_tensors=None, retain_graph=False): ...@@ -44,34 +44,39 @@ def backward(tensors, grad_tensors=None, retain_graph=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
x = paddle.to_tensor([[1, 2], [3, 4]], dtype='float32', stop_gradient=False) >>> x = paddle.to_tensor([[1, 2], [3, 4]], dtype='float32', stop_gradient=False)
y = paddle.to_tensor([[3, 2], [3, 4]], dtype='float32') >>> y = paddle.to_tensor([[3, 2], [3, 4]], dtype='float32')
grad_tensor1 = paddle.to_tensor([[1,2], [2, 3]], dtype='float32') >>> grad_tensor1 = paddle.to_tensor([[1,2], [2, 3]], dtype='float32')
grad_tensor2 = paddle.to_tensor([[1,1], [1, 1]], dtype='float32') >>> grad_tensor2 = paddle.to_tensor([[1,1], [1, 1]], dtype='float32')
z1 = paddle.matmul(x, y) >>> z1 = paddle.matmul(x, y)
z2 = paddle.matmul(x, y) >>> z2 = paddle.matmul(x, y)
paddle.autograd.backward([z1, z2], [grad_tensor1, grad_tensor2], True) >>> paddle.autograd.backward([z1, z2], [grad_tensor1, grad_tensor2], True)
print(x.grad) >>> print(x.grad)
#[[12. 18.] Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
# [17. 25.]] [[12., 18.],
[17., 25.]])
x.clear_grad()
paddle.autograd.backward([z1, z2], [grad_tensor1, None], True) >>> x.clear_grad()
print(x.grad)
#[[12. 18.]
# [17. 25.]]
x.clear_grad() >>> paddle.autograd.backward([z1, z2], [grad_tensor1, None], True)
>>> print(x.grad)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[12., 18.],
[17., 25.]])
>>> x.clear_grad()
>>> paddle.autograd.backward([z1, z2])
>>> print(x.grad)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[10., 14.],
[10., 14.]])
paddle.autograd.backward([z1, z2])
print(x.grad)
#[[10. 14.]
# [10. 14.]]
""" """
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册