Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
02538dfc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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):
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__
(
...
...
python/paddle/audio/functional/functional.py
浏览文件 @
02538dfc
...
...
@@ -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
)
...
...
python/paddle/audio/functional/window.py
浏览文件 @
02538dfc
...
...
@@ -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
...
...
python/paddle/autograd/autograd.py
浏览文件 @
02538dfc
...
...
@@ -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
:
...
...
python/paddle/autograd/backward_mode.py
浏览文件 @
02538dfc
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录