Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
02538dfc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
02538dfc
编写于
8月 18, 2023
作者:
L
LoneRanger
提交者:
GitHub
8月 18, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[xdoctest] reformat example code with google style in No.16-No.20 (#56296)
* fix sample codes * fix bug * fix bug * fix bug
上级
6f93f9c7
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
143 addition
and
132 deletion
+143
-132
python/paddle/audio/features/layers.py
python/paddle/audio/features/layers.py
+42
-42
python/paddle/audio/functional/functional.py
python/paddle/audio/functional/functional.py
+34
-34
python/paddle/audio/functional/window.py
python/paddle/audio/functional/window.py
+5
-5
python/paddle/autograd/autograd.py
python/paddle/autograd/autograd.py
+36
-30
python/paddle/autograd/backward_mode.py
python/paddle/autograd/backward_mode.py
+26
-21
未找到文件。
python/paddle/audio/features/layers.py
浏览文件 @
02538dfc
...
@@ -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__
(
...
...
python/paddle/audio/functional/functional.py
浏览文件 @
02538dfc
...
@@ -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
)
...
...
python/paddle/audio/functional/window.py
浏览文件 @
02538dfc
...
@@ -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
...
...
python/paddle/autograd/autograd.py
浏览文件 @
02538dfc
...
@@ -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
:
...
...
python/paddle/autograd/backward_mode.py
浏览文件 @
02538dfc
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录