This section trains a simple neural network model to judge whether the sound is male or female; This is a simple example of neural network classification.
DataSet fields: Sound properties and labels;
The code is as follows:
#coding =utf-8 import OS import requests import pandas as PD import NumPy as NP import random import TensorFlow as TF from Sklearn.cross_validation Import Train_test_split #下载数据集 if not os.path.exists (' voice.csv '): url = ' Http://blog.topspe Edsnail.com/wp-content/uploads/2016/12/voice.csv ' data = requests.get (URL). Content with open (' Voice.csv ', ' WB ') as F:f.write (data) Voice_data = Pd.read_csv (' voice.csv ') #读取数据 voice_data = voice_data.values #分类特征与类别 voices = Voi Ce_data[:,:-1] labels = voice_data[:,-1:] #male或female #将分类转换为one-hot labels_tmp = [] for label in labels:tmp=[] I F label[0] = = ' Male ': tmp = [1.0,0.0] else:tmp = [0.0,1.0] labels_tmp.append (tmp) labels = Np.arr Ay (labels_tmp) #shuffle voices_tmp = [] labels_tmp = [] Index_shuf = [i-I in range (Len (voices))] Random.shuffle (index _SHUF) for I in Index_shuf:voices_tmp.append (Voices[i]) labels_tmp.append (labels[i]) voices = Np.array (Voices_tm P) labels = Np.array (LabELS_TMP) #划分数据集: train_x, test_x, train_y, test_y = Train_test_split (Voices, labels, test_size=0.1) batch_size = N_BATC h = Len (train_x)//batch_size x = Tf.placeholder (Dtype=tf.float32, Shape=[none,voices.shape[-1]]) Y = Tf.placeholder (dty Pe= Tf.float32, shape=[none,2]) #3层前馈 def neural_network (): W1 = tf. Variable (Tf.random_normal ([voices.shape[-1],512],stddev=0.5)) B1 = tf. Variable (Tf.random_normal ([+])) output = Tf.matmul (x,w1) +b1 w2 = tf. Variable (Tf.random_normal ([512,1024],stddev=.5)) b2 = tf. Variable (Tf.random_normal ([1024x768])) output = Tf.nn.softmax (Tf.matmul (OUTPUT,W2) + b2) W3 = tf. Variable (Tf.random_normal ([1024,2])) B3 = tf. Variable (Tf.random_normal ([2])) output = Tf.nn.softmax (Tf.matmul (output, W3) + B3) return output #训练神经网络: def tra In_neural_network (): output = neural_network () cost = Tf.reduce_mean (Tf.reduce_sum (tf.nn.softmax_cross_entropy_wit H_logits (output,y))) LR = tf. Variable (0.001, Dtype=tf.float32, trainable=False) opt = Tf.train.AdamOptimizer (LEARNING_RATE=LR) var_list = [t for T in Tf.trainable_variables ()] train_s TEP = Opt.minimize (cost,var_list=var_list) #储存模型 with TF. Session () as Sess:sess.run (Tf.global_variables_initializer ()) for epoch in range: Sess. Run (Tf.assign (LR, 0.001* (0.97**epoch))) for batch in range (N_batch): Voice_batch = Train_x[bat
Ch*batch_size: (batch+1) * (batch_size)] Label_batch = train_y[batch*batch_size: (batch+1) * (batch_size)] _, Loss = Sess.run ([Train_step,cost], feed_dict={x:voice_batch,y:label_batch}) print (epoch, b Atch, loss) #准确率 prediction = tf.equal (Tf.argmax (output,1), Tf.argmax (y,1)) accuracy = Tf.reduce_m EAN (Tf.cast (prediction,dtype=tf.float32)) accuracy = Sess.run (accuracy,feed_dict={x:test_x, y:test_y}) pr
int ("Precision ratio:%f"%accuracy) train_neural_network ()
This is a three-layer neural network model with the following results: