Advertisement
Guest User

perceptron

a guest
Jan 22nd, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. import argparse
  2. import os
  3. import wave
  4. import soundfile as np
  5. from pydub import AudioSegment
  6.  
  7. def dir_path(string):
  8.     if os.path.isdir(string):
  9.         return string
  10.     else:
  11.         raise NotADirectoryError(string)
  12.  
  13. # Make a prediction with weights
  14. def predict(row, weights):
  15.     activation = weights[0]
  16.     for i in range(len(row) - 1):
  17.         activation += weights[i + 1] * row[i]
  18.     return 1.0 if activation >= 0.0 else 0.0
  19.  
  20. # Estimate Perceptron weights using stochastic gradient descent
  21. def train_weights(train, l_rate, n_epoch):
  22.     weights = [0.0 for i in range(len(train[0]))]
  23.     for epoch in range(n_epoch):
  24.         sum_error = 0.0
  25.         for row in train:
  26.             prediction = predict(row, weights)
  27.             error = row[-1] - prediction
  28.             sum_error += error ** 2
  29.             weights[0] = weights[0] - l_rate * error
  30.             for i in range(len(row) - 1):
  31.                 weights[i + 1] = weights[i + 1] + l_rate * error * row[i]
  32.         print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error))
  33.         print(weights)
  34.     return weights
  35.  
  36. parser = argparse.ArgumentParser(description="Separating silent waves from louder waves")
  37. parser.add_argument('-p', '--path', type=dir_path, metavar='', required=True, help="give path to the file with waves.")
  38. args = parser.parse_args()
  39.  
  40. if __name__ == '__main__':
  41.     os.chdir(args.path)
  42.     print(os.getcwd() + "\n\n")
  43.     training_list = []
  44.     testing_list = []
  45.  
  46.     for root, sub, files in os.walk(args.path):
  47.         for f in files:
  48.  
  49.             if 'wav' in f:
  50.                 abs_path = os.path.abspath(root)
  51.                 # Populate training list
  52.                 if abs_path.endswith('silence_training'):
  53.                     x, fs = np.read(os.path.join(abs_path, f))
  54.                     training_list.append([x.max(),x.min(), 0])
  55.                 elif abs_path.endswith('voiced_training'):
  56.                     x, fs = np.read(os.path.join(abs_path, f))
  57.                     training_list.append([x.max(),x.min(), 1])
  58.                 # Populate testing list
  59.                 elif abs_path.endswith('silence'):
  60.                     x, fs = np.read(os.path.join(abs_path, f))
  61.                     testing_list.append([x.max(), x.min(), 0])
  62.                 elif abs_path.endswith('voiced'):
  63.                     x, fs = np.read(os.path.join(abs_path, f))
  64.                     testing_list.append([x.max(), x.min(), 1])
  65.  
  66.  
  67. l_rate = 0.1
  68. n_epoch = 1000
  69. weights = train_weights(training_list,l_rate, n_epoch)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement