-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandson_DNN_ practice.py
151 lines (104 loc) · 3.4 KB
/
Handson_DNN_ practice.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 23 20:30:18 2020
@author: VISHWESH
"""
import numpy as np
import tensorflow as tf
from tensorflow import keras
from sklearn.preprocessing import StandardScaler
train,test= keras.datasets.mnist.load_data()
X_train = train[0]/255.0
y_train = train[1]
X_test = test[0]/255.0
y_test = test[1]
'''
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
'''
#%%
import matplotlib.pyplot as plt
plt.imshow(X_train[1], cmap="binary")
plt.axis('off')
plt.show()
#%%
'''
from sklearn.decomposition import PCA
X_flat = X_train.reshape(60000,784)
pca = PCA(n_components=0.95)
X_reduced_pca = pca.fit_transform(X_flat)
'''
#%%
keras.backend.clear_session()
np.random.seed(42)
tf.random.set_seed(42)
model = keras.models.Sequential([
keras.layers.Flatten(input_shape = X_train.shape[1:]),
keras.layers.Dense(300,activation="relu"),
keras.layers.Dense(100,activation="relu"),
keras.layers.Dense(10,activation="softmax")
])
model.summary()
#%%
opt = keras.optimizers.SGD(learning_rate=2e-1)
model.compile(loss="sparse_categorical_crossentropy",
optimizer=opt,
metrics="accuracy"
)
#%%
import os
root_logdir = os.path.join(os.curdir, "my_logs")
def get_run_logdir():
import time
run_id = time.strftime("run_%Y_%m_%d-%H_%M_%S") + " lr=2e-1"
return os.path.join(root_logdir, run_id)
run_logdir = get_run_logdir()
tensorboard_cb = keras.callbacks.TensorBoard(run_logdir,histogram_freq=1)
#%%
#checkpoint_cb = keras.callbacks.ModelCheckpoint("my_mnist_model.h5", save_best_only=True) creates checkpoint and saves best model
early_stopping_cb = keras.callbacks.EarlyStopping(patience=10,
restore_best_weights=True)
history = model.fit(X_train,y_train, epochs=100, validation_split = 0.2,
callbacks=[early_stopping_cb,tensorboard_cb])
model.save("seq_model_mnist_1.h5")
#%% tsne
'''
from sklearn.manifold import TSNE
np.random.seed(42)
X_flat = X_train.reshape(60000,784)
m = 10000
idx = np.random.permutation(60000)[:m]
X = X_flat[idx]
y = y_train[idx]
tsne = TSNE(n_components=2, random_state=42)
X_reduced = tsne.fit_transform(X)
plt.figure(figsize=(13,10))
plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, cmap="jet")
plt.axis('off')
plt.colorbar()
plt.show()'''
#%%
from sklearn.model_selection import cross_val_score, cross_val_predict
#cv_score=cross_val_score(svm_rbf_clf,X_train_scaled[:10000],y_train[:10000],cv=5,scoring="accuracy")
from sklearn.metrics import confusion_matrix,precision_score,recall_score
#%%
import pandas as pd
plt.plot(pd.DataFrame(history.history))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.show()
#%% training acc
pred = model.predict(X_train)
pred_classes =(np.argmax(pred,axis=1))
cnf_matrix = confusion_matrix(y_train,pred_classes)
precision_training = precision_score(y_train,pred_classes,average="weighted")
recall_training = recall_score(y_train,pred_classes,average="weighted")
#%% its wrong way!!!
'''
X_flat_test = X_test.reshape(10000,784)
pca = PCA(n_components=154)
X_test_reduced_pca = pca.fit_transform(X_flat_test)
'''
model.evaluate(X_test, y_test)
# %tensorboard --logdir=./my_logs --port=6006