2020-05-29 11:49:23 +02:00
#%% Change working directory from the workspace root to the ipynb file location. Turn this addition off with the DataScience.changeDirOnImportExport setting
# ms-python.python added
import os
try :
os . chdir ( os . path . join ( os . getcwd ( ) , ' docs \\ images ' ) )
print ( os . getcwd ( ) )
except :
pass
#%%
import music21
from music21 . midi import MidiFile
import numpy as np
import matplotlib . pyplot as plt
import mido
#%% [markdown]
# # midi messages
#%%
filepath = ' /home/altarin/praca-magisterska/docs/images/seq2seq_generated_midi_7.mid '
mid = mido . MidiFile ( filepath )
for i , track in enumerate ( mid . tracks ) :
for msg in track :
print ( msg )
#%% [markdown]
# # Regresja liniowa
#%%
x = np . arange ( 0 , 10 )
y = np . arange ( 0 , 10 ) + np . random . random ( 10 ) - 0.5
y_hat = np . arange ( 0 , 10 )
plt . scatter ( x , y , c = ' k ' )
plt . plot ( x , y_hat , c = ' r ' )
# plt.labels
# plt.savefig('linear_reg.png')
#%%
from math import exp
x = np . arange ( - 10 , 10 , 0.1 )
# y = np.arange(0,10)
tanh = lambda x : ( exp ( x ) - exp ( - x ) ) / ( exp ( x ) + exp ( - x ) )
2020-06-17 19:21:57 +02:00
sigmoid = lambda x : 1 / ( 1 + exp ( - x ) )
2020-05-29 11:49:23 +02:00
y_hat = [ tanh ( yy ) for yy in x ]
# plt.scatter(x, y, c='k')
plt . plot ( x , y_hat )
2020-06-17 19:21:57 +02:00
fig = plt . Figure ( figsize = ( 20 , 30 ) )
plt . show ( )
2020-05-29 11:49:23 +02:00
# plt.labels
2020-06-17 19:21:57 +02:00
# plt.savefig('tanh.png')
2020-05-29 11:49:23 +02:00
#%%
# Gradient descent
fx = x ^ 2
dx = 2 x + c
#%%
2020-06-17 19:21:57 +02:00
2020-05-29 11:49:23 +02:00
func = lambda x : x * * 2
func_dx = lambda x : 2 * x
x = np . arange ( - 20 , 20 , 0.1 )
y = func ( x )
point_x = - 6
point_y = func ( point_x )
2020-06-17 19:21:57 +02:00
# styczna = lambda x: slope*x + intercept
2020-05-29 11:49:23 +02:00
2020-06-17 19:21:57 +02:00
# dx = styczna(x)
2020-05-29 11:49:23 +02:00
learning_points_xs = np . arange ( point_x , 0 , 0.8 )
learning_points_ys = func ( learning_points_xs )
2020-06-17 19:21:57 +02:00
fig , ax = plt . subplots ( figsize = ( 3 , 3 ) )
2020-05-29 11:49:23 +02:00
ax . plot ( x , y , c = ' k ' )
for px in learning_points_xs [ 0 : 1 ] :
slope = func_dx ( px )
intercept = - px * * 2
styczna = lambda x : slope * x + intercept
dx = styczna ( x )
2020-06-17 19:21:57 +02:00
# ax.plot(x, dx, c='r', zorder=1)
2020-05-29 11:49:23 +02:00
ax . scatter ( x = point_x , y = point_y , c = ' r ' , zorder = 6 ) #start
2020-06-17 19:21:57 +02:00
ax . scatter ( x = 0 , y = 0 , c = ' g ' , zorder = 6 ) #min
ax . scatter ( x = learning_points_xs , y = learning_points_ys , c = ' y ' , zorder = 5 )
2020-05-29 11:49:23 +02:00
plt . ylim ( ( - 20 , 80 ) )
2020-06-17 19:21:57 +02:00
plt . xlim ( ( - 10 , 10 ) )
2020-05-29 11:49:23 +02:00
# plt.xlabel('x')
# plt.ylabel('f(x) = x^2')
2020-06-17 19:21:57 +02:00
fig . savefig ( ' gradient_descent_2_long.png ' , dpi = 300 )
2020-05-29 11:49:23 +02:00
# https://towardsdatascience.com/understanding-the-mathematics-behind-gradient-descent-dde5dc9be06e?
# https://medium.com/code-heroku/gradient-descent-for-machine-learning-3d871fa48b4c
2020-06-15 18:20:53 +02:00
#%% sound wave
#import the pyplot and wavfile modules
import matplotlib . pyplot as plot
from scipy . io import wavfile
# Read the wav file (mono)
2020-06-17 19:21:57 +02:00
samplingFrequency , signalData = wavfile . read ( r ' docs \ images \ foo.wav ' )
2020-06-15 18:20:53 +02:00
# Plot the signal read from wav file
# plot.title('Spectrogram of a wav file with piano music')
plot . plot ( signalData )
plot . xlabel ( ' Próbki ' )
plot . ylabel ( ' Amplituda ' )
2020-06-17 19:21:57 +02:00
# plot.savefig('waveform_axis.png')
fig = plt . Figure ( figsize = ( 2 , 15 ) )
plot . show ( )
2020-06-15 18:20:53 +02:00
# print(samplingFrequency)
2020-05-29 11:49:23 +02:00
2020-06-16 14:19:12 +02:00
# %%
import pandas as pd
import matplotlib . pyplot as plt
import os
# %%
cols = [ ' epoch ' , ' val_loss ' , ' loss ' ]
guitar_df = pd . read_csv ( ' offspring_history/guitar_history.csv ' , header = None )
bass_df = pd . read_csv ( ' offspring_history/bass_history.csv ' , header = None )
drums_df = pd . read_csv ( ' offspring_history/drums_history.csv ' , header = None )
melody_df = pd . read_csv ( ' offspring_history/melody_history.csv ' , header = None )
guitar_df . columns = cols
bass_df . columns = cols
drums_df . columns = cols
melody_df . columns = cols
fig , axs = plt . subplots ( 2 , 2 , figsize = ( 10 , 10 ) )
l11 , l21 = axs [ 0 ] [ 0 ] . plot ( guitar_df [ [ ' loss ' , ' val_loss ' ] ] )
l21 , l22 = axs [ 0 ] [ 1 ] . plot ( bass_df [ [ ' loss ' , ' val_loss ' ] ] )
l31 , l32 = axs [ 1 ] [ 0 ] . plot ( drums_df [ [ ' loss ' , ' val_loss ' ] ] )
l41 , l42 = axs [ 1 ] [ 1 ] . plot ( melody_df [ [ ' loss ' , ' val_loss ' ] ] )
2020-05-29 11:49:23 +02:00
2020-06-16 14:19:12 +02:00
axs [ 0 ] [ 0 ] . set_title ( ' Guitar ' )
axs [ 0 ] [ 1 ] . set_title ( ' Bass ' )
axs [ 1 ] [ 0 ] . set_title ( ' Drums ' )
axs [ 1 ] [ 1 ] . set_title ( ' Melody ' )
2020-06-15 18:20:53 +02:00
2020-06-16 14:19:12 +02:00
axs [ 0 ] [ 0 ] . set_xlabel ( ' epochs ' )
axs [ 0 ] [ 1 ] . set_xlabel ( ' epochs ' )
axs [ 1 ] [ 0 ] . set_xlabel ( ' epochs ' )
axs [ 1 ] [ 1 ] . set_xlabel ( ' epochs ' )
2020-06-15 18:20:53 +02:00
2020-06-16 14:19:12 +02:00
axs [ 0 ] [ 0 ] . set_ylabel ( ' loss ' )
axs [ 0 ] [ 1 ] . set_ylabel ( ' loss ' )
axs [ 1 ] [ 0 ] . set_ylabel ( ' loss ' )
axs [ 1 ] [ 1 ] . set_ylabel ( ' loss ' )
axs [ 0 ] [ 0 ] . legend ( labels = ( ' loss ' , ' val_loss ' ) )
2020-06-15 18:20:53 +02:00
2020-06-16 14:19:12 +02:00
plt . savefig ( ' training_losses.png ' )
2020-06-15 18:20:53 +02:00
#%%