aboutsummaryrefslogtreecommitdiff
path: root/lenet.py
diff options
context:
space:
mode:
Diffstat (limited to 'lenet.py')
-rw-r--r--lenet.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/lenet.py b/lenet.py
index 3d388de..3d9ed20 100644
--- a/lenet.py
+++ b/lenet.py
@@ -16,6 +16,7 @@ from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from classifier_metrics_impl import classifier_score_from_logits
from sklearn.utils import shuffle
+from sklearn.manifold import TSNE
def import_mnist():
from tensorflow.examples.tutorials.mnist import input_data
@@ -141,12 +142,12 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100,
model.save_weights('./weights.h5')
return model
-def test_classifier(model, x_test, y_true, conf_mat=False, pca=False):
+def test_classifier(model, x_test, y_true, conf_mat=False, pca=False, tsne=False):
x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant')
- y_pred = model.predict(x_test)
- logits = tf.convert_to_tensor(y_pred, dtype=tf.float32)
- inception_score = tf.keras.backend.eval(classifier_score_from_logits(logits))
- y_pred = np.argmax(y_pred, axis=1)
+ logits = model.predict(x_test)
+ tf_logits = tf.convert_to_tensor(logits, dtype=tf.float32)
+ inception_score = tf.keras.backend.eval(classifier_score_from_logits(tf_logits))
+ y_pred = np.argmax(logits, axis=1)
y_true = np.argmax(y_true, axis=1)
plot_example_errors(y_pred, y_true, x_test)
cm = confusion_matrix(y_true, y_pred)
@@ -158,16 +159,24 @@ def test_classifier(model, x_test, y_true, conf_mat=False, pca=False):
plt.show()
if pca:
set_pca = PCA(n_components=2)
- pca_rep = np.reshape(x_test, (x_test.shape[0], x_test.shape[1]*x_test.shape[2]))
- print(pca_rep.shape)
- pca_rep = set_pca.fit_transform(pca_rep)
- print(pca_rep.shape)
+ pca_rep = set_pca.fit_transform(logits)
pca_rep, y_tmp = shuffle(pca_rep, y_true, random_state=0)
- plt.scatter(pca_rep[:100, 0], pca_rep[:100, 1], c=y_true[:100], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10))
+ plt.scatter(pca_rep[:1000, 0], pca_rep[:1000, 1], c=y_true[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10))
plt.xlabel('component 1')
plt.ylabel('component 2')
plt.colorbar();
plt.show()
+ if tsne:
+ tsne = TSNE(n_components=2, random_state=0)
+ components = tsne.fit_transform(logits)
+ print(components.shape)
+ components, y_tmp = shuffle(components, y_true, random_state=0)
+ plt.scatter(components[:1000, 0], components[:1000, 1], c=y_true[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10))
+ plt.xlabel('component 1')
+ plt.ylabel('component 2')
+ plt.colorbar();
+ plt.show()
+
return accuracy_score(y_true, y_pred), inception_score
@@ -202,4 +211,4 @@ if __name__ == '__main__':
x_train, y_train, x_val, y_val, x_t, y_t = import_mnist()
print(y_t.shape)
model = train_classifier(x_train[:100], y_train[:100], x_val, y_val, epochs=3)
- print(test_classifier(model, x_t, y_t, pca=True))
+ print(test_classifier(model, x_t, y_t, pca=False, tsne=True))