Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions python/paddle/audio/features/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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__(
Expand Down
68 changes: 34 additions & 34 deletions python/paddle/audio/functional/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)
>>> n_mfcc = 23
>>> n_mels = 51
>>> paddle_dct = paddle.audio.functional.create_dct(n_mfcc, n_mels)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个示例应该是写错了,没有用到 compute_fbank_matrix ~ 重新写一个吧 ~

"""

if f_max is None:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions python/paddle/audio/functional/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
66 changes: 36 additions & 30 deletions python/paddle/autograd/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Loading