pytorch 使用cnn模型 声调识别代码
时间: 2023-07-16 13:15:37 浏览: 90
以下是使用CNN模型进行声调识别的PyTorch代码。该代码使用了一个简单的卷积神经网络,可以对输入的声音进行分类,以确定其所属的声调。
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data_utils
import numpy as np
import librosa
# 配置训练参数
batch_size = 64
num_epochs = 10
learning_rate = 0.001
# 加载数据
train_data = np.load('train_data.npy')
train_labels = np.load('train_labels.npy')
test_data = np.load('test_data.npy')
test_labels = np.load('test_labels.npy')
# 转换为PyTorch张量
train_data = torch.from_numpy(train_data).float()
train_labels = torch.from_numpy(train_labels).long()
test_data = torch.from_numpy(test_data).float()
test_labels = torch.from_numpy(test_labels).long()
# 创建数据集和数据加载器
train_dataset = data_utils.TensorDataset(train_data, train_labels)
train_loader = data_utils.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dataset = data_utils.TensorDataset(test_data, test_labels)
test_loader = data_utils.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# 定义CNN模型
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv1d(1, 32, 3, padding=1)
self.pool1 = nn.MaxPool1d(2, stride=2)
self.conv2 = nn.Conv1d(32, 64, 3, padding=1)
self.pool2 = nn.MaxPool1d(2, stride=2)
self.conv3 = nn.Conv1d(64, 128, 3, padding=1)
self.pool3 = nn.MaxPool1d(2, stride=2)
self.fc1 = nn.Linear(128 * 16, 512)
self.fc2 = nn.Linear(512, 4)
self.relu = nn.ReLU()
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.relu(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.relu(x)
x = self.pool3(x)
x = x.view(-1, 128 * 16)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
# 创建模型并定义损失函数和优化器
model = CNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
for i, (data, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(data.unsqueeze(1))
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch + 1, num_epochs, i + 1, len(train_loader), loss.item()))
# 测试模型
with torch.no_grad():
correct = 0
total = 0
for data, labels in test_loader:
outputs = model(data.unsqueeze(1))
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the model on the test set: {} %'.format(100 * correct / total))
```
在这个例子中,我们创建了一个简单的卷积神经网络,它有三个卷积层和两个全连接层。我们使用了ReLU作为激活函数,并使用了交叉熵损失函数进行训练。我们使用Adam优化器来优化模型参数。
在训练期间,我们使用了PyTorch的数据加载器来加载数据。我们还使用了一个循环,以便在每个epoch的末尾测试模型的准确性。
最后,我们使用测试数据集评估了模型的准确性。
阅读全文