Hear your model’s training

Krish Shah
Analytics Vidhya
Published in
3 min readJul 1, 2020

--

Listen to what your models want to say to you..HAHA!

Everyone on this planet who is into data science visualizes the model training with a graph in python like below

Training plots for MNIST model

but I had an absurd thought this morning, “Why not hear your models training and figure out if it had overfitted or not”.So this post guides you to my absurd idea of hearing your trained model using very few steps. it’s actually the first module of my first ever python library I’m planning to make. Hope this interests you

Here’s a sample of validation loss and accuracy with training loss and accuracy

Validation accuracy
Validation Loss
Accuracy
Loss

Step 1: Train a model:

Model training

Here, for example, sake I’m training a simple model for MNIST dataset and logging the data[loss+accuracy data ] to a CSV file using a callback function inbuild in Tensorflow

Step 2: Hear your model

If you need more detail on how to train a model do refer to my previous post:

Now you are ready with model history into CSV file lets build code for hearing

import pandas as pd
import numpy as np
from scipy.interpolate import interp1d
import sounddevice as sd
from math import pi
from scipy.io import wavfile
  1. Pandas used for reading the log file containing training information
  2. Numpy for generating cosine values for audio
  3. Interp1d for mapping the data into the frequency range
  4. sound device for hearing the NumPy cosine values
  5. pi for using the value of pi[3.14……]
  6. Wavfile for saving the audio in wave format
MAX_DURATION=0.15
MIN_VAL,MAX_VAL = 1000,3000
SAMPLING_RATE = 22050

we set up the sampling rate for an audio file, min and max frequency range we wanna hear and max duration for which a particular frequency is heard

df = pd.read_csv('model_log.csv')
l=df['loss'].values

we now read the CSV file and for example sake, we read the loss values for each epoch for model training

m = interp1d([min(l),max(l)],[MIN_VAL,MAX_VAL])
list_of_f = [m(x) for x in l]
x=[]
t=np.arange(0,MAX_DURATION,1./SAMPLING_RATE)

where m is an object of class interp1d used to map our loss values into frequency range mentioned before. list comprehension is used to map all values using the object m. An empty array is initialized to append the results of every frequency for an audio file. t is the nd array of values spaced evenly for every frequency for time 0.15 sec

for f in list_of_f:
cos_wav=10000*np.cos(2*pi*f*t) #generated signals
x=np.hstack((x,cos_wav))

wavfile.write('loss.wav',SAMPLING_RATE,x.astype(np.dtype('i2')))

Now here we generate cos wave for each frequency for a particular time range and append or stack to the list and finally save the audio file. u might be thinking what is np.dtype(‘i2’), its actually converting int64 value to i2 format which is understood for wave file or else u might get into errors

refer to this StackOverflow post for understanding the err which might occur:

import IPython.display as ipd
ipd.Audio('loss.wav')

Here how u play your file in Jupyter notebook.

now repeat the steps for all parameters in the log file

I hope you liked the post. Do share if you find it insightful

--

--