2014-09-28 19:37:56 -04:00
|
|
|
from sklearn import *;
|
|
|
|
from sklearn import ensemble;
|
|
|
|
import numpy as np
|
|
|
|
import scipy as sp
|
2014-10-01 04:44:16 +02:00
|
|
|
from pybrain.datasets import SupervisedDataSet
|
|
|
|
from pybrain.tools.shortcuts import buildNetwork
|
|
|
|
from pybrain.supervised.trainers import BackpropTrainer
|
|
|
|
from pybrain.structure import LinearLayer, TanhLayer, SigmoidLayer, SoftmaxLayer, FeedForwardNetwork, BiasUnit
|
|
|
|
from pybrain.tools.neuralnets import NNregression, Trainer
|
2014-09-28 19:37:56 -04:00
|
|
|
|
2014-10-04 08:58:11 +02:00
|
|
|
def train_model(X, Y, profiles, metric):
|
2014-09-29 03:01:33 +02:00
|
|
|
#Preprocessing
|
2014-10-01 04:44:16 +02:00
|
|
|
Xmean = np.mean(X, axis=0)
|
|
|
|
Xstd = np.std(X, axis=0)
|
|
|
|
X = (X - Xmean)/Xstd
|
2014-10-03 09:29:45 +02:00
|
|
|
|
2014-10-01 04:44:16 +02:00
|
|
|
Ymax = np.max(Y)
|
|
|
|
Y = Y/Ymax
|
2014-09-28 19:37:56 -04:00
|
|
|
|
2014-10-01 04:44:16 +02:00
|
|
|
ref = np.argmax(np.bincount(np.argmax(Y, axis=1))) #most common profile
|
2014-09-29 03:01:33 +02:00
|
|
|
#Cross-validation data-sets
|
2014-10-03 09:29:45 +02:00
|
|
|
cut = int(0.800*X.shape[0]+1)
|
2014-10-01 04:44:16 +02:00
|
|
|
XTr = X[0:cut, :]
|
|
|
|
YTr = Y[0:cut, :]
|
|
|
|
XTe = X[cut:,:]
|
|
|
|
YTe = Y[cut:,:]
|
2014-09-29 03:01:33 +02:00
|
|
|
|
|
|
|
#Train the model
|
2014-10-01 04:44:16 +02:00
|
|
|
print("Training the model...")
|
2014-10-03 09:29:45 +02:00
|
|
|
clf = ensemble.RandomForestRegressor(40).fit(XTr,YTr)
|
2014-09-29 03:01:33 +02:00
|
|
|
|
|
|
|
#Evaluate the model
|
2014-10-01 04:44:16 +02:00
|
|
|
GFlops = np.empty(XTe.shape[0])
|
|
|
|
speedups = np.empty(XTe.shape[0])
|
|
|
|
optspeedups = np.empty(XTe.shape[0])
|
2014-09-29 03:01:33 +02:00
|
|
|
for i,x in enumerate(XTe):
|
2014-10-03 09:29:45 +02:00
|
|
|
predictions = clf.predict(x)
|
2014-10-01 04:44:16 +02:00
|
|
|
label = np.argmax(predictions)
|
|
|
|
speedups[i] = YTe[i,label]/YTe[i,ref]
|
|
|
|
optspeedups[i] = np.max(YTe[i,:])/YTe[i,ref]
|
|
|
|
GFlops[i] = YTe[i,ref]*Ymax
|
2014-09-29 03:01:33 +02:00
|
|
|
|
2014-10-01 04:44:16 +02:00
|
|
|
np.set_printoptions(precision=2)
|
|
|
|
print("-----------------")
|
|
|
|
print("Average testing speedup : %f (Optimal : %f)"%(sp.stats.gmean(speedups), sp.stats.gmean(optspeedups)))
|
2014-10-04 08:58:11 +02:00
|
|
|
print("Average %s: %f (Default %f, Optimal %f)"%(metric, np.mean(np.multiply(GFlops,speedups)), np.mean(GFlops), np.mean(np.multiply(GFlops,optspeedups))))
|
|
|
|
print("Minimum speedup is %f wrt %i %s"%(np.min(speedups), GFlops[np.argmin(speedups)], metric))
|
|
|
|
print("Maximum speedup is %f wrt %i %s"%(np.max(speedups), GFlops[np.argmax(speedups)], metric))
|
2014-10-03 09:29:45 +02:00
|
|
|
print("--------")
|