In [1]:
import collections
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import sklearn
import seaborn as sns
from sklearn import svm
from my_rbm import Rbm
import six.moves.cPickle as pickle
import sys
from pandas import *
from sklearn.preprocessing import OneHotEncoder
%matplotlib inline
In [2]:
def unpickle(file):
fo = open(file, 'rb')
dict = pickle.load(fo, encoding='latin-1')
fo.close()
return dict
def from_flat_to_3d(image):
# print(image.shape)
return np.dstack((image[0:1024].reshape(32,32),
image[1024:2048].reshape(32,32),
image[2048:3072].reshape(32,32)))
cifar_test = unpickle('cifar-10-batches-py/test_batch')
cifar_test['data'] = cifar_test['data'].astype(np.float32) / 255
cifar_test['data_3d'] = np.array([from_flat_to_3d(image) for image in cifar_test['data']])
cifar = unpickle('cifar-10-batches-py/data_batch_1')
for i in range(2, 6):
tmp = unpickle('cifar-10-batches-py/data_batch_' + str(i))
cifar['data'] = np.vstack((cifar['data'], tmp['data']))
cifar['labels'] = np.concatenate((cifar['labels'], tmp['labels']))
cifar['data'] = cifar['data'].astype(np.float32) / 255
cifar['data_3d'] = np.array([from_flat_to_3d(image) for image in cifar['data']])
# cifar['data_bw'] = (cifar['data'][:,0:1024] + cifar['data'][:,1024:2048] + cifar['data'][:, 2048:3072]) / 3
# cifar_test['data_bw'] = (cifar_test['data'][:,0:1024] + cifar_test['data'][:,1024:2048] + cifar_test['data'][:, 2048:3072]) / 3
enc = OneHotEncoder()
cifar['labels_oh'] = enc.fit_transform(cifar['labels'].reshape(-1, 1))
cifar['labels_oh'] = cifar['labels_oh'].toarray()
cifar_test['labels'] = np.array(cifar_test['labels'])
cifar_test['labels_oh'] = enc.fit_transform(cifar_test['labels'].reshape(-1, 1))
cifar_test['labels_oh'] = cifar_test['labels_oh'].toarray()
# pca = PCA(whiten=True)
# cifar['data_bw_whitened'] = pca.fit_transform(cifar['data_bw'])
# cifar_test['data_bw_whitened'] = pca.fit_transform(cifar_test['data_bw'])
In [ ]:
num_hidden = 763
num_epochs=10
rbm = Rbm(num_hidden=num_hidden, num_classes=10, num_features=3072, learning_rate=0.001)
rbm.init_rbm()
rbm.fit(cifar['data'], cifar_test['data'], num_epochs=num_epochs)num_hidden = 761
num_epochs=10
rbm = Rbm(num_hidden=num_hidden, num_classes=10, num_features=3072, learning_rate=0.001)
rbm.init_rbm()
rbm.fit(cifar['data'], cifar_test['data'], num_epochs=num_epochs)
In [ ]:
last_output = np.empty((0, rbm.get_h_prob_out([cifar['data'][0]]).shape[1]))
last_output.shape
for i in range(cifar['data'].shape[0]):
last_output = np.vstack((last_output, rbm.get_h_prob_out([cifar['data'][i]])))
if i % 1000 == 0:
print('done with: %s' % i)
In [ ]:
last_output_test = np.empty((0, rbm.get_h_prob_out([cifar_test['data'][0]]).shape[1]))
last_output_test.shape
for i in range(cifar_test['data'].shape[0]):
last_output_test = np.vstack((last_output_test, rbm.get_h_prob_out([cifar_test['data'][i]])))
if i % 999 == 0:
print('done with: %s' % (i + 1))
In [ ]:
clf = svm.SVC(decision_function_shape='ovr', kernel='poly', degree=3, coef0=1.0)
clf.fit(last_output, cifar['labels'])
clf.score(last_output_test, cifar_test['labels'])
In [3]:
clf = svm.SVC(decision_function_shape='ovr', kernel='poly', degree=3, coef0=1.0)
clf.fit(cifar['data'], cifar['labels'])
clf.score(cifar_test['data'], cifar_test['labels'])
Out[3]:
In [ ]: