[Experimental exercise] Speech feature signal classification based on BP neural network (Python implementation) original content

topic

Using BP neural network method to complete the classification of data

data set

The four files data1.data, data2.data, data3.data, and data4.data respectively represent 4 groups (500×25) of voice data, each group has 25 dimensions, the first dimension is the category identification, and the last 24 dimensions are voice feature signal. The 4 groups of voice signals can be identified by 1, 2, 3, and 4.

Require

Training set: 1500 sets of data are randomly selected as training data.

Test set: The remaining 500 sets of data are used as test data.

According to the BP neural network, the data classification model is established to realize the classification of speech signals.

Evaluation index: Discuss the correct rate of BP neural network classification results.

# -*- coding: utf-8 -*- #
"""
@Project    :NIR-Mathematical-Modeling-Tool 
@File       :main.py 
@Author     :ZAY
@Time       :2023/6/4 15:44
@Annotation : " "
"""

if __name__ == "__main__":

    store_path = './/model//BPNet.pt'
    txt_path = './/Result//BPNet.txt'

    # 下载四类语音信号
    c1 = scio.loadmat('.//Data//data1.mat')
    c2 = scio.loadmat('.//Data//data2.mat')
    c3 = scio.loadmat('.//Data//data3.mat')
    c4 = scio.loadmat('.//Data//data4.mat')

    # print(type(c1)) # <class 'dict'>
    c1 = c1.get('c1')
    c2 = c2.get('c2')
    c3 = c3.get('c3')
    c4 = c4.get('c4')

    c1 = np.array(c1)
    c1 = np.array(c1)
    c1 = np.array(c1)
    c1 = np.array(c1)
    # print(c1.shape) # (500, 25)

    # scio.savemat(New_path, {'A': a, b}) # 以字典的形式保存

    # 四个特征信号矩阵合成一个矩阵
    data = np.vstack((c1[:, :], c2[:, :])) # 按垂直方向(行顺序)堆叠数组构成一个新的数组
    data = np.vstack((data[:, :], c3[:, :]))
    data = np.vstack((data[:, :], c4[:, :]))
    # print(data.shape) # (2000, 25)

    # 从1到2000间随机排序
    k = np.random.rand(2000)
    n = np.argsort(k) # 将k中的元素从小到大排列,提取其在排列前对应的index(索引)输出。
    print(n)

    # 输入输出数据
    input_data = data[:, 1:25]
    output_data = data[:, 0]
    # output_data = np.expand_dims(output_data,axis = 1)
    # print(output_data.shape) # (2000, 1)
    # print(output_data)

    # 把输出label从[1,2,3,4]变成[0,1,2,3]
    for i in range(2000):
        output_data[i] = output_data[i] - 1

    # 把输出从1维变成4维
    # output = np.zeros((2000, 4))
    # for i in range(2000):
    #     if output_data[i] == 1:
    #         output[i, :] = [1, 0, 0, 0]
    #     elif output_data[i] == 2:
    #         output[i, :] = [0, 1, 0, 0]
    #     elif output_data[i] == 3:
    #         output[i, :] = [0, 0, 1, 0]
    #     elif output_data[i] == 4:
    #         output[i, :] = [0, 0, 0, 1]

    output = output_data

    # 随机提取1500个样本为训练样本,500个样本为预测样本
    input_train = input_data[n[:1400], :]
    output_train = output[n[:1400]]
    input_val = input_data[n[1400:1500], :]
    output_val = output[n[1400:1500]]
    input_test = input_data[n[1500:], :]
    output_test = output[n[1500:]]

    # print(input_train.shape) # (1400, 24)
    # print(output_train.shape) # (1400, 1)
    # print(input_val.shape) # (100, 24)
    # print(output_val.shape) # (100, 1)
    # print(input_test.shape) # (500, 24)
    # print(output_test.shape) # (500, 1)

    # 升维用于其他网络 例如CNN
    # input_train = input_train[:, np.newaxis, :]
    # input_val = input_val[:, np.newaxis, :]
    # 输入数据归一化
    # inputn, inputps = MapMinMaxApplier.mapminmax(input_train)  # #默认为-1,1

    cal_ds = MyDataset(input_train, output_train)  # TrainSet TensorData
    val_ds = MyDataset(input_val, output_val)  # TestSet TensorData
    test_ds = MyDataset(input_test, output_test)

    modeltrian(path=store_path, data_train = cal_ds, data_test = val_ds)  

    modeltest(path=store_path, data_test = test_ds, txt_path = txt_path)

BPNet network

# -*- coding: utf-8 -*- #
"""
@Project    :NIR-Mathematical-Modeling-Tool 
@File       :BPNet.py 
@Author     :ZAY
@Time       :2023/6/4 15:56
@Annotation : " "
"""

import torch
import torch.nn as nn


class BPNet(nn.Module):
    def __init__(self):
        super(BPNet, self).__init__()
        self.fc1 = nn.Sequential(
            nn.Linear(24, 96),
            nn.ReLU()
        )
        # self.fc2 = nn.Sequential(
        #     nn.Linear(100, 50),
        #     nn.Sigmoid()
        # )

        self.predict = nn.Linear(96, 4)
        self.act = nn.ReLU()

    def forward(self, x):
        x = self.act(self.predict(self.fc1(x)))
        return x


# net = BPNet()
# print(net)

Test Results

DATE:_2023-06-05_18-33-10, TEST:Acc= 0.8661, train_time = 0:00:08.596997, test_time = 0:00:00.006983
Current model parameters: 0.002788 M

Please private message for experimental data and complete code 

Guess you like

Origin blog.csdn.net/Next_SummerAgain/article/details/131056320