Pytorch语音信号处理常用函数总结

Pytorch学习

学习pytorch辅导书:Pytorch手册
常用函数查询:Pytorch文档

后续学习更新。

针对语音处理的pytorch常用函数

一般用到如下:

import os
import torch
import numpy as np
from torch.utils.data import Dataset, DataLoader #训练数据组织
from hparams import hparams #预处理及网络需要的参数
import librosa #特征提取
import soundfile as sf #读音频
import random

语音增强–数据预处理

在这里插入图片描述
预处理得到(D维,T帧)数据,而网络输入需要(T,D)
其中每次输入网络的不是一帧,而是多帧,所以需要拼帧:
在这里插入图片描述
比如每5帧拼一起,作为一个整体,来预测中间一帧:
在这里插入图片描述
想象一下,每一帧数据D维,每5帧放一起,假设共13帧,即得到(9帧D维5)的数据。

根据这一部分,主要的pytorch的函数就是:

torch.from_numpy

用来将数组array转换为张量Tensor。

X_train = torch.from_numpy(noisy_LPS)

具体用法:

a=np.array([1,2,3,4])
print(a)
#[1 2 3 4]

print(torch.from_numpy(a))
#tensor([1, 2, 3, 4], dtype=torch.int32)

torch.unfold

torch.unfold(dimension,size,step)

按照指定维度dimension,以一定的间隔step将原始张量按size 大小进行分片,然后返回重整后的张量。

示例:

>>> a = torch.arange(16).view(4, 4)
>>> a
tensor([[ 0,  1,  2,  3],#[]里逗号隔开的就是列
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]])
>>> b = a.unfold(0, 3, 1)      #0行1列    
 # 按照行,以每3个元素,跨步为1进行展开
>>> b
tensor([[[ 0,  4,  8],
         [ 1,  5,  9],
         [ 2,  6, 10],
         [ 3,  7, 11]],

        [[ 4,  8, 12],
         [ 5,  9, 13],
         [ 6, 10, 14],
         [ 7, 11, 15]]])
>>> b.shape

torch.transpose

torch.transpose(tensor,dim_0,dim_1)

转置操作,交换两个维度。
示例:

a=torch.tensor([[[1,2,3],[4,5,6]],
                [[7,8,9],[10,11,12]]])
b=torch.transpose(a,1,2) #置换1,2维,0维不变

#输出b:
#tensor([[[ 1,  4],
         [ 2,  5],
         [ 3,  6]],

        [[ 7, 10],
         [ 8, 11],
         [ 9, 12]]])

#a的shape: torch.Size([2, 2, 3])
#b的shape: torch.Size([2, 3, 2])

torch.view

用于改变tensor的形状。
示例:

a = torch.tensor([1, 2, 3, 4, 5, 6])
b = a.view(2, 3)

#输出:
>>a : tensor([1, 2, 3, 4, 5, 6])
>>b : tensor([[1, 2, 3],
              [4, 5, 6]])

-1表示一个不确定的数,就是如果你确定想要reshape成什么维度,把不确定,或懒得算的那个维度置为-1,其他的需要写上

示例:

c = a.view(-1,3)

#输出:
tensor([[1, 2, 3],
        [4, 5, 6]])

c = a.view(3,-1)

#输出
tensor([[1, 2],
        [3, 4],
        [5, 6]])

深入学习:tensor.view()、tensor.reshape()、tensor.resize_() 三者的区别

torch.cat

outputs = torch.cat(inputs, dim) 

把多个tensor进行拼接。
inputs : 待连接的张量序列,可以是任意相同Tensor类型的python 序列
dim : 选择哪一维, 必须在0到len(inputs[0])之间,沿着此维连接张量序列。

示例:

# x1
x1 = torch.tensor([[11,21,31],[21,31,41]],dtype=torch.int)
x1.shape # torch.Size([2, 3])
# x2
x2 = torch.tensor([[12,22,32],[22,32,42]],dtype=torch.int)
x2.shape  # torch.Size([2, 3])

'合并,得到inputs为2个形状为[2 , 3]的矩阵 '
inputs = [x1, x2]
print(inputs)
'打印查看'
[tensor([[11, 21, 31],
         [21, 31, 41]], dtype=torch.int32),
 tensor([[12, 22, 32],
         [22, 32, 42]], dtype=torch.int32)]


torch.cat(inputs, dim=0).shape# 按第0维拼接
Out[1]: torch.Size([4,  3])

torch.cat(inputs, dim=1).shape# 按第一维拼接
Out[2]: torch.Size([2, 6])

torch.cat(inputs, dim=2).shape #报错
IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值