{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"05a7e0382f0469b8a1ffea641af517af5d1464c6","collapsed":false,"_cell_guid":"22d1443c-d944-4606-b9b3-c0d62ba57686","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-02-01T05:49:22.130026Z","iopub.execute_input":"2023-02-01T05:49:22.130572Z","iopub.status.idle":"2023-02-01T05:49:22.168192Z","shell.execute_reply.started":"2023-02-01T05:49:22.130532Z","shell.execute_reply":"2023-02-01T05:49:22.166515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Import stuff\n\nimport numpy as np\nimport random\nimport itertools\nimport librosa\nimport IPython.display as ipd\nimport matplotlib.pyplot as plt\n\n%matplotlib inline","metadata":{"_uuid":"5afa490db6d79308f6de183ec0be35304ff97c5c","_cell_guid":"69d385d7-44b0-4b14-a406-e78ee04b1b4b","execution":{"iopub.status.busy":"2023-02-01T05:49:22.170815Z","iopub.execute_input":"2023-02-01T05:49:22.171452Z","iopub.status.idle":"2023-02-01T05:49:22.182564Z","shell.execute_reply.started":"2023-02-01T05:49:22.171399Z","shell.execute_reply":"2023-02-01T05:49:22.180919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_audio_file(file_path):\n    input_length = 16000\n    data = librosa.core.load(file_path)[0] #, sr=16000\n    if len(data)>input_length:\n        data = data[:input_length]\n    else:\n        data = np.pad(data, (0, max(0, input_length - len(data))), \"constant\")\n    return data\ndef plot_time_series(data):\n    fig = plt.figure(figsize=(14, 8))\n    plt.title('Raw wave ')\n    plt.ylabel('Amplitude')\n    plt.plot(np.linspace(0, 1, len(data)), data)\n    plt.show()","metadata":{"_uuid":"b232201620caaa349d7f07d74780d1242ab7a4fc","collapsed":false,"_cell_guid":"06dfa34d-cf83-4eb7-855c-b298bb4daed0","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-02-01T05:49:22.184481Z","iopub.execute_input":"2023-02-01T05:49:22.185371Z","iopub.status.idle":"2023-02-01T05:49:22.195697Z","shell.execute_reply.started":"2023-02-01T05:49:22.185328Z","shell.execute_reply":"2023-02-01T05:49:22.194507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = load_audio_file(\"/kaggle/input/samples/tasnetmi-samples-master/simulations/snr30dBser-14.2dB/music/000000_BLSTM.wav\")\nplot_time_series(data)","metadata":{"_uuid":"9d28c1dfbb211c875f026ab73dba231ea572e238","collapsed":false,"_cell_guid":"b9db3341-0145-4f9c-a33f-7c10951ff6c5","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-02-01T05:49:22.198344Z","iopub.execute_input":"2023-02-01T05:49:22.198760Z","iopub.status.idle":"2023-02-01T05:49:22.601372Z","shell.execute_reply.started":"2023-02-01T05:49:22.198691Z","shell.execute_reply":"2023-02-01T05:49:22.599904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Hear it ! \nipd.Audio(data, rate=16000)","metadata":{"_uuid":"c698649bd96c4ccc232526ea9b9681f3d358369f","collapsed":false,"_cell_guid":"6f70aea2-f984-4aad-ba54-5861134bc117","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-02-01T05:49:22.602699Z","iopub.execute_input":"2023-02-01T05:49:22.603078Z","iopub.status.idle":"2023-02-01T05:49:22.613102Z","shell.execute_reply.started":"2023-02-01T05:49:22.603044Z","shell.execute_reply":"2023-02-01T05:49:22.611737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adding white noise \nwn = np.random.randn(len(data))\ndata_wn = data + 0.005*wn\nplot_time_series(data_wn)\n# We limited the amplitude of the noise so we can still hear the word even with the noise, \n#which is the objective\nipd.Audio(data_wn, rate=16000)","metadata":{"_uuid":"f45ff5f8132b8516522464d3f90277f0197fa71c","collapsed":false,"_cell_guid":"8a346d98-18c4-4be8-802d-a94a8c3fd088","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-02-01T05:49:22.615029Z","iopub.execute_input":"2023-02-01T05:49:22.615482Z","iopub.status.idle":"2023-02-01T05:49:22.986983Z","shell.execute_reply.started":"2023-02-01T05:49:22.615410Z","shell.execute_reply":"2023-02-01T05:49:22.985562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shifting the sound\ndata_roll = np.roll(data, 1600)\nplot_time_series(data_roll)\nipd.Audio(data_roll, rate=16000)","metadata":{"_uuid":"875d393f58b7948752802d897783d054dc6b676f","collapsed":false,"_cell_guid":"3dd85e75-3d49-4187-9e2f-14b2638a13c2","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-02-01T05:49:22.988658Z","iopub.execute_input":"2023-02-01T05:49:22.989162Z","iopub.status.idle":"2023-02-01T05:49:23.292730Z","shell.execute_reply.started":"2023-02-01T05:49:22.989125Z","shell.execute_reply":"2023-02-01T05:49:23.291201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# stretching the sound\ndef stretch(data, rate=1):\n    input_length = 16000\n    data = librosa.effects.time_stretch(data, rate)\n    if len(data)>input_length:\n        data = data[:input_length]\n    else:\n        data = np.pad(data, (0, max(0, input_length - len(data))), \"constant\")\n\n    return data\n\n\ndata_stretch =stretch(data, 0.8)\nprint(\"This makes the sound deeper but we can still hear 'off' \")\nplot_time_series(data_stretch)\nipd.Audio(data_stretch, rate=16000)\n\ndata_stretch =stretch(data, 1.2)\nprint(\"Higher frequencies  \")\nplot_time_series(data_stretch)\nipd.Audio(data_stretch, rate=16000)","metadata":{"_uuid":"f786049cbbd24ee4fbfd8beb0fc3601e8d2d2051","collapsed":false,"_cell_guid":"128047ec-5389-4a41-a37d-3b973ce0daeb","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-02-01T05:49:23.296941Z","iopub.execute_input":"2023-02-01T05:49:23.297358Z","iopub.status.idle":"2023-02-01T05:49:23.922787Z","shell.execute_reply.started":"2023-02-01T05:49:23.297322Z","shell.execute_reply":"2023-02-01T05:49:23.921544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# You can now plug all those transformations in your keras data generator and see your LB rank go up :D","metadata":{"_uuid":"872e68bc7de37759466996b48fad430fae7243bc","_cell_guid":"bf197741-38a1-487c-b99c-2585dde6f311","execution":{"iopub.status.busy":"2023-02-01T05:49:23.924356Z","iopub.execute_input":"2023-02-01T05:49:23.924757Z","iopub.status.idle":"2023-02-01T05:49:23.929850Z","shell.execute_reply.started":"2023-02-01T05:49:23.924724Z","shell.execute_reply":"2023-02-01T05:49:23.928424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/tuneshigh/features_3_sec.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:23.931280Z","iopub.execute_input":"2023-02-01T05:49:23.931643Z","iopub.status.idle":"2023-02-01T05:49:24.196036Z","shell.execute_reply.started":"2023-02-01T05:49:23.931611Z","shell.execute_reply":"2023-02-01T05:49:24.194575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/tuneshigh/features_30_sec.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:24.198109Z","iopub.execute_input":"2023-02-01T05:49:24.198549Z","iopub.status.idle":"2023-02-01T05:49:24.248893Z","shell.execute_reply.started":"2023-02-01T05:49:24.198514Z","shell.execute_reply":"2023-02-01T05:49:24.247498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:24.250733Z","iopub.execute_input":"2023-02-01T05:49:24.251182Z","iopub.status.idle":"2023-02-01T05:49:24.259051Z","shell.execute_reply.started":"2023-02-01T05:49:24.251138Z","shell.execute_reply":"2023-02-01T05:49:24.257798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:24.260401Z","iopub.execute_input":"2023-02-01T05:49:24.260734Z","iopub.status.idle":"2023-02-01T05:49:24.273925Z","shell.execute_reply.started":"2023-02-01T05:49:24.260705Z","shell.execute_reply":"2023-02-01T05:49:24.272711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio =\"/kaggle/input/samples/tasnetmi-samples-master/simulations/snr30dBser-14.2dB/music/000002_BLSTM.wav\"\ndata,sr=librosa.load(audio)\nprint(type(data),type(sr))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:24.275269Z","iopub.execute_input":"2023-02-01T05:49:24.275663Z","iopub.status.idle":"2023-02-01T05:49:24.371923Z","shell.execute_reply.started":"2023-02-01T05:49:24.275629Z","shell.execute_reply":"2023-02-01T05:49:24.370579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"librosa.load(audio,sr=45600)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:24.373462Z","iopub.execute_input":"2023-02-01T05:49:24.374142Z","iopub.status.idle":"2023-02-01T05:49:24.533093Z","shell.execute_reply.started":"2023-02-01T05:49:24.374098Z","shell.execute_reply":"2023-02-01T05:49:24.531159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Taking Short-time Fourier transform of the signal\ny = librosa.stft(data)  \nS_db = librosa.amplitude_to_db(np.abs(y), ref=np.max)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:24.535542Z","iopub.execute_input":"2023-02-01T05:49:24.536223Z","iopub.status.idle":"2023-02-01T05:49:24.555911Z","shell.execute_reply.started":"2023-02-01T05:49:24.536170Z","shell.execute_reply":"2023-02-01T05:49:24.554788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,4))\nlibrosa.display.waveshow(data,color=\"#2B4F72\", alpha = 0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:24.557616Z","iopub.execute_input":"2023-02-01T05:49:24.558078Z","iopub.status.idle":"2023-02-01T05:49:25.077135Z","shell.execute_reply.started":"2023-02-01T05:49:24.558041Z","shell.execute_reply":"2023-02-01T05:49:25.076225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Spectrogram of the audio\nstft=librosa.stft(data)\nstft_db=librosa.amplitude_to_db(abs(stft))\nplt.figure(figsize=(7,6))\nlibrosa.display.specshow(stft_db,sr=sr,x_axis='time',y_axis='hz')\nplt.colorbar()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:25.078650Z","iopub.execute_input":"2023-02-01T05:49:25.079090Z","iopub.status.idle":"2023-02-01T05:49:25.559226Z","shell.execute_reply.started":"2023-02-01T05:49:25.079056Z","shell.execute_reply":"2023-02-01T05:49:25.558110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectral_rolloff=librosa.feature.spectral_rolloff(y=data,sr=sr)[0]\nplt.figure(figsize=(7,6))\nlibrosa.display.waveshow(data,sr=sr,alpha=0.4,color=\"#2B4F72\")","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:25.563880Z","iopub.execute_input":"2023-02-01T05:49:25.564208Z","iopub.status.idle":"2023-02-01T05:49:26.113676Z","shell.execute_reply.started":"2023-02-01T05:49:25.564179Z","shell.execute_reply":"2023-02-01T05:49:26.112253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa.display as lplt\nchroma = librosa.feature.chroma_stft(y=data,sr=sr)\nplt.figure(figsize=(7,4))\nlplt.specshow(chroma,sr=sr,x_axis=\"time\",y_axis=\"chroma\",cmap=\"BuPu\")\nplt.colorbar()\nplt.title(\"Chroma Features\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:26.115089Z","iopub.execute_input":"2023-02-01T05:49:26.115586Z","iopub.status.idle":"2023-02-01T05:49:26.989150Z","shell.execute_reply.started":"2023-02-01T05:49:26.115542Z","shell.execute_reply":"2023-02-01T05:49:26.985908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start=1000\nend=1200\nplt.figure(figsize=(12,4))\nplt.plot(data[start:end],color=\"#2B4F72\")","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:26.990677Z","iopub.execute_input":"2023-02-01T05:49:26.991432Z","iopub.status.idle":"2023-02-01T05:49:27.284821Z","shell.execute_reply.started":"2023-02-01T05:49:26.991399Z","shell.execute_reply":"2023-02-01T05:49:27.283691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printing the number of times signal crosses the x-axis\nzero_cross_rate=librosa.zero_crossings(data[start:end],pad=False)\nprint(\"The number of zero_crossings are :\", sum(zero_cross_rate))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.286409Z","iopub.execute_input":"2023-02-01T05:49:27.286907Z","iopub.status.idle":"2023-02-01T05:49:27.293045Z","shell.execute_reply.started":"2023-02-01T05:49:27.286876Z","shell.execute_reply":"2023-02-01T05:49:27.291967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Finding misssing values\n# Find all columns with any NA values\nprint(\"Columns containing missing values\",list(df.columns[df.isnull().any()]))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.294360Z","iopub.execute_input":"2023-02-01T05:49:27.294788Z","iopub.status.idle":"2023-02-01T05:49:27.313580Z","shell.execute_reply.started":"2023-02-01T05:49:27.294754Z","shell.execute_reply":"2023-02-01T05:49:27.312454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nclass_encod=df.iloc[:,-1]\nconverter=LabelEncoder()\ny=converter.fit_transform(class_encod)\ny","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.315033Z","iopub.execute_input":"2023-02-01T05:49:27.315373Z","iopub.status.idle":"2023-02-01T05:49:27.332754Z","shell.execute_reply.started":"2023-02-01T05:49:27.315342Z","shell.execute_reply":"2023-02-01T05:49:27.331206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#features\nprint(df.iloc[:,:-1])","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.334593Z","iopub.execute_input":"2023-02-01T05:49:27.334977Z","iopub.status.idle":"2023-02-01T05:49:27.362875Z","shell.execute_reply.started":"2023-02-01T05:49:27.334943Z","shell.execute_reply":"2023-02-01T05:49:27.361687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Drop the column filename as it is no longer required for training\ndf=df.drop(labels=\"filename\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.364574Z","iopub.execute_input":"2023-02-01T05:49:27.365711Z","iopub.status.idle":"2023-02-01T05:49:27.372559Z","shell.execute_reply.started":"2023-02-01T05:49:27.365665Z","shell.execute_reply":"2023-02-01T05:49:27.371364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#scaling\nfrom sklearn.preprocessing import StandardScaler\nfit=StandardScaler()\nX=fit.fit_transform(np.array(df.iloc[:,:-1],dtype=float))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.374262Z","iopub.execute_input":"2023-02-01T05:49:27.375080Z","iopub.status.idle":"2023-02-01T05:49:27.388980Z","shell.execute_reply.started":"2023-02-01T05:49:27.375030Z","shell.execute_reply":"2023-02-01T05:49:27.387472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np \nimport matplotlib.pyplot as plt\nimport scipy\nimport os\nimport pickle\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\nfrom IPython.display import Audio\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential \nfrom pathlib import Path\nimport soundfile as sf\nimport scipy.signal as sig\n# splitting 70% data into training set and the remaining 30% to test set\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.390791Z","iopub.execute_input":"2023-02-01T05:49:27.391480Z","iopub.status.idle":"2023-02-01T05:49:27.401271Z","shell.execute_reply.started":"2023-02-01T05:49:27.391418Z","shell.execute_reply":"2023-02-01T05:49:27.400023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.402933Z","iopub.execute_input":"2023-02-01T05:49:27.403901Z","iopub.status.idle":"2023-02-01T05:49:27.414952Z","shell.execute_reply.started":"2023-02-01T05:49:27.403855Z","shell.execute_reply":"2023-02-01T05:49:27.413883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.416517Z","iopub.execute_input":"2023-02-01T05:49:27.417162Z","iopub.status.idle":"2023-02-01T05:49:27.428260Z","shell.execute_reply.started":"2023-02-01T05:49:27.417122Z","shell.execute_reply":"2023-02-01T05:49:27.427206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Applying K nearest Neighbour algorithm to predict the results\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nclf1=KNeighborsClassifier(n_neighbors=3)\nclf1.fit(X_train,y_train)\ny_pred=clf1.predict(X_test)\nprint(\"Training set score: {:.3f}\".format(clf1.score(X_train, y_train)))\nprint(\"Test set score: {:.3f}\".format(clf1.score(X_test, y_test)))\ncf_matrix = confusion_matrix(y_test, y_pred)\nsns.set(rc = {'figure.figsize':(8,3)})\nsns.heatmap(cf_matrix, annot=True)\nprint(classification_report(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:27.429859Z","iopub.execute_input":"2023-02-01T05:49:27.430521Z","iopub.status.idle":"2023-02-01T05:49:28.319643Z","shell.execute_reply.started":"2023-02-01T05:49:27.430482Z","shell.execute_reply":"2023-02-01T05:49:28.318356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Applying Support Vector Machines to predict the results\nfrom sklearn.svm import SVC\nsvclassifier = SVC(kernel='rbf', degree=8)\nsvclassifier.fit(X_train, y_train)\nprint(\"Training set score: {:.3f}\".format(svclassifier.score(X_train, y_train)))\nprint(\"Test set score: {:.3f}\".format(svclassifier.score(X_test, y_test)))\ny_pred = svclassifier.predict(X_test)\ncf_matrix3 = confusion_matrix(y_test, y_pred)\nsns.set(rc = {'figure.figsize':(9,4)})\nsns.heatmap(cf_matrix3, annot=True)\nprint(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:28.321114Z","iopub.execute_input":"2023-02-01T05:49:28.321476Z","iopub.status.idle":"2023-02-01T05:49:29.107207Z","shell.execute_reply.started":"2023-02-01T05:49:28.321420Z","shell.execute_reply":"2023-02-01T05:49:29.105761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the model using the following parameters\n# metrics = accuracy\n# epochs = 600\n# loss = sparse_categorical_crossentropy\n# batch_size = 256\n# optimizer = adam\n\ndef train_model(model,epochs,optimizer):\n    batch_size=256\n    model.compile(optimizer=optimizer,loss='sparse_categorical_crossentropy',metrics='accuracy')\n    return model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=epochs,batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:29.108782Z","iopub.execute_input":"2023-02-01T05:49:29.109129Z","iopub.status.idle":"2023-02-01T05:49:29.115666Z","shell.execute_reply.started":"2023-02-01T05:49:29.109097Z","shell.execute_reply":"2023-02-01T05:49:29.114507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Validation_plot(history):\n    print(\"Validation Accuracy\",max(history.history[\"val_accuracy\"]))\n    pd.DataFrame(history.history).plot(figsize=(12,6))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:29.116900Z","iopub.execute_input":"2023-02-01T05:49:29.117232Z","iopub.status.idle":"2023-02-01T05:49:29.125535Z","shell.execute_reply.started":"2023-02-01T05:49:29.117202Z","shell.execute_reply":"2023-02-01T05:49:29.124449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We used different layers to train the neural network by importing keras library from tensorflow framework \n# for input and hidden neurons we use the most widly used activation function which is relu where as for output neurons we uses softmax activation function\nmodel=tf.keras.models.Sequential([\n    tf.keras.layers.Flatten(input_shape=(X.shape[1],)),\n    tf.keras.layers.Dropout(0.2),\n    \n    tf.keras.layers.Dense(512,activation='relu'),\n    keras.layers.Dropout(0.2),\n    \n    tf.keras.layers.Dense(256,activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    \n    tf.keras.layers.Dense(128,activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    \n    tf.keras.layers.Dense(64,activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    \n    tf.keras.layers.Dense(32,activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    \n    tf.keras.layers.Dense(10,activation='softmax'),\n])\n\noptimizer = tf.keras.optimizers.Adam(learning_rate=0.000146)\nmodel.compile(optimizer=optimizer,\n             loss=\"sparse_categorical_crossentropy\",\n              metrics=[\"accuracy\"])\nmodel.summary()\nmodel_history=train_model(model=model,epochs=600,optimizer='adam')","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:49:29.126991Z","iopub.execute_input":"2023-02-01T05:49:29.127308Z","iopub.status.idle":"2023-02-01T05:50:17.203771Z","shell.execute_reply.started":"2023-02-01T05:49:29.127279Z","shell.execute_reply":"2023-02-01T05:50:17.202924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss,test_acc=model.evaluate(X_test,y_test,batch_size=256)\nprint(\"The test loss is \",test_loss)\nprint(\"The best accuracy is: \",test_acc*100)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:50:17.205417Z","iopub.execute_input":"2023-02-01T05:50:17.206539Z","iopub.status.idle":"2023-02-01T05:50:17.282769Z","shell.execute_reply.started":"2023-02-01T05:50:17.206503Z","shell.execute_reply":"2023-02-01T05:50:17.281407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The plot dipicts how training and testing data performed\nValidation_plot(model_history)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:50:17.284687Z","iopub.execute_input":"2023-02-01T05:50:17.285039Z","iopub.status.idle":"2023-02-01T05:50:17.595172Z","shell.execute_reply.started":"2023-02-01T05:50:17.285001Z","shell.execute_reply":"2023-02-01T05:50:17.594033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample testing\nsample = X_test\nsample = sample[np.newaxis, ...]\nprediction = model.predict(X_test)\npredicted_index = np.argmax(prediction, axis = 1)\nprint(\"Expected Index: {}, Predicted Index: {}\".format(y_test, predicted_index))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:50:17.596924Z","iopub.execute_input":"2023-02-01T05:50:17.597734Z","iopub.status.idle":"2023-02-01T05:50:17.752735Z","shell.execute_reply.started":"2023-02-01T05:50:17.597683Z","shell.execute_reply":"2023-02-01T05:50:17.751425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting the confusion matrix for analizing the true positives and negatives\nimport seaborn as sn\nimport matplotlib.pyplot as plt\npred_x = model.predict(X_test)\nfrom sklearn.metrics import confusion_matrix\n\ncm = confusion_matrix(y_test,predicted_index )\ncm","metadata":{"execution":{"iopub.status.busy":"2023-02-01T05:50:17.754071Z","iopub.execute_input":"2023-02-01T05:50:17.754521Z","iopub.status.idle":"2023-02-01T05:50:17.833864Z","shell.execute_reply.started":"2023-02-01T05:50:17.754484Z","shell.execute_reply":"2023-02-01T05:50:17.832483Z"},"trusted":true},"execution_count":null,"outputs":[]}]}