from sklearn import datasets, cross_validation from sknn.mlp import Classifier, Layer, Convolution # Load the data and split it into subsets for training and testing. digits = datasets.load_digits() X = digits.images y = digits.target X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2) # Create a neural network that uses convolution to scan the input images. nn = Classifier( layers=[ Convolution('Rectifier', channels=12, kernel_shape=(3, 3), border_mode='full'), Convolution('Rectifier', channels=10, kernel_shape=(3, 3), border_mode='valid'), Convolution('Rectifier', channels=4, kernel_shape=(3, 3), border_mode='valid'), Layer('Rectifier', units=64), Layer('Softmax')], learning_rate=0.002, valid_size=0.2, n_stable=10, verbose=True) nn.fit(X_train, y_train) # Determine how well it does on training data and unseen test data. print('\nTRAIN SCORE', nn.score(X_train, y_train)) print('TEST SCORE', nn.score(X_test, y_test))
X_train = img Y_train = cat X_test = test print N print X_train.shape,' -- ',X_test.shape print Y_train.shape print '____________' print X_train[:5] print Y_train[:5] print Y_train[-5:] nn = Classifier( layers=[ Convolution("Rectifier",channels=48,kernel_shape=(5,5),pool_shape=(3,3)), Convolution("Rectifier",channels=96,kernel_shape=(11,11),pool_shape=(5,5)), Layer("Rectifier",units=128), Layer("Rectifier",units=128), Layer("Softmax")], learning_rate=0.001, verbose=True) #train(nn, X_train, Y_train, './model', n_epoch=100, save_part=5) #train(nn, X_train, Y_train, './skt_again', n_epoch=100, save_part=5) #-----TESTING-----# nn = pickle.load(open('./skt_again','r')) print 'Network loaded' print nn print '--------'
features = test.columns.tolist() features.remove('is_train') features.remove('Label') # Neural Network, Stochastic Gradient Descent is sensitive to feature scaling, so it is highly recommended to scale your data. scaler = StandardScaler() for col in features: scaler.fit(list(train[col])+list(test[col])) train[col] = scaler.transform(train[col]) test[col] = scaler.transform(test[col]) # Define Classifier myNNClassifier = Classifier( layers=[ Convolution("Rectifier", channels=16, kernel_shape=(5,5)), Convolution("Rectifier", channels=16, kernel_shape=(5,5)), Convolution("Rectifier", channels=16, kernel_shape=(5,5)), Layer('Rectifier', units=100), Layer('Softmax')], learning_rate=0.01, learning_rule='momentum', learning_momentum=0.9, batch_size=100, valid_size=0.01, n_stable=20, n_iter=200, verbose=True) # Train myNNClassifier.fit(np.array(train[list(features)]), train[goal])
#print '\rLoading:',i-(N_TRAIN),'/',N_TRAIN_TEST,'.', test_images_full_info.append(load_imgs('train/' + str(i) + '.png')) print '\n' #Preprossesing Test Images log.info('Preprosseing Test images.') ZZ = np.array(test_images_full_info) Z = np.zeros((len(ZZ), 1, 32, 32)) for i in range(len(ZZ)): Z[i] = datagen.standardize(ZZ[i]) y_test = np.array(labels[N_TRAIN + 1:N_TRAIN + 1 + N_TRAIN_TEST]) log.info('Starting Classifier.') nn = Classifier( layers=[ Convolution("Rectifier", channels=64, kernel_shape=(3, 3)), Native(lasagne.Conv2DLayer, num_filters=32, filter_size=(3, 3), nonlinearity=nl.leaky_rectify), Native(lasagne.MaxPool2DLayer, pool_size=(2, 2)), Native(lasagne.DropoutLayer, p=0.25), Native(lasagne.Conv2DLayer, num_filters=64, filter_size=(3, 3), nonlinearity=nl.leaky_rectify), Native(lasagne.Conv2DLayer, num_filters=64, filter_size=(3, 3), nonlinearity=nl.leaky_rectify), Native(lasagne.MaxPool2DLayer, pool_size=(2, 2)),
sys.path.append('/usr/local/lib/python2.7/dist-packages') import numpy as np #from sklearn import svm from sklearn.decomposition import PCA import pandas as pd from sknn.mlp import Classifier,Layer from sknn.mlp import Convolution training = np.loadtxt('./train.csv',delimiter=',',dtype=int) validation = np.loadtxt('./test.csv',delimiter=',',dtype=int) labels = training[:,0] traindata = np.delete(training,0,1) pca = PCA() pca.fit(traindata,labels) pcafittrain = pca.transform(traindata) pcafittest = pca.transform(validation) print "pca done" c = Classifier(layers=[Convolution("Rectifier", channels=18, pool_shape=(2,2),kernel_shape=(5,5), border_mode='valid'),Layer('Rectifier', units=200),Layer('Softmax')],learning_rate=0.00001,learning_rule='nesterov', learning_momentum=0.9, batch_size=300, valid_size=0.0, normalize='L1',n_stable=10, n_iter=10, verbose=True) c.fit(pcafittrain,labels) print "fitting done" result = c.predict(pcafittest).ravel() print "result" print result x = list(range(1,28001)) df = pd.DataFrame({"ImageID" : x, "Label" : result}) df.to_csv('outo.csv',index=False)