-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainnn.py
47 lines (36 loc) · 1.58 KB
/
trainnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import keras
import pandas as pd
from keras.callbacks import ReduceLROnPlateau
from keras.layers import Activation, Conv2D, MaxPooling2D, Dropout, Flatten
from keras.layers import Dense
from keras.models import Sequential
from keras.utils import np_utils
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
import numpy as np
import pickle
le = preprocessing.LabelEncoder()
df_x = pd.read_csv('features_cat_6.csv')
X = df_x.iloc[:,1:519].values
y = df_x.iloc[:,520].values
pca = PCA(n_components = 'mle', svd_solver = 'full')
X = pca.fit_transform(X)
# X = pca.fit(X)
# pickle.dump(X, open('pca.model', 'wb+'))
le.fit(y)
y = le.transform(y)
y = np_utils.to_categorical(y)
print ("Label Encoded")
(trainData, testData, trainLabels, testLabels) = train_test_split(X, y, test_size=0.25, random_state=42)
model = Sequential()
model.add(Dense(30, input_dim=X.shape[1], init="uniform",activation="relu"))
model.add(Dense(40, activation="relu", kernel_initializer="uniform"))
model.add(Dense(20, activation="relu", kernel_initializer="uniform"))
model.add(Dense(6))
model.add(Activation("softmax"))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
learning_rate_function = ReduceLROnPlateau(monitor='val_acc',patience = 3, verbose = 1, factor = 0.7, min_lr = 0.00001)
history = model.fit(trainData, trainLabels, epochs=65, validation_data=(testData, testLabels)
,batch_size=128, verbose=2, shuffle = False, callbacks=[learning_rate_function])
model.save_weights('music_weight_cat6.h5')