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