def CreateTFGraphTrain(self): #Tensorflow 4 CNN Model #Classifier model; Architecture of the CNN ws = [('C', [3, 3, 3, 10], [1, 1, 1, 1]), ('P', [1, 4, 4, 1], [1, 2, 2, 1]), ('C', [3, 3, 10, 5], [1, 1, 1, 1]), ('P', [1, 4, 4, 1], [1, 2, 2, 1]), ('F', 32), ('F', 16), ('F', 2)] ims = [self.ss[1] // self.m, self.ss[0] // self.n, 3] #Images from skimage are of shape (height, width, 3) hmcIms = 30 * 100 * 3 #Number of pixels in health/mana checker images carg = {'batchSize': 40, 'learnRate': 1e-3, 'maxIter': 40, 'reg': 6e-4, 'tol': 25e-3, 'verbose': True} self.OC = CNNC(ims, ws, name = 'obcnn', **carg) self.OC.RestoreClasses(['C', 'O']) #Used to detect enemies self.EC = CNNC(ims, ws, name = 'encnn', **carg) self.EC.RestoreClasses(['N', 'E', 'I']) #CNN for detecting movement self.MC = CNNC(ims, ws, name = 'mvcnn', **carg) self.MC.RestoreClasses(['Y', 'N']) #Classifier for lightning-warp self.LC = CNNC(ims, ws, name = 'lwcnn', **carg) self.LC.RestoreClasses(['Y', 'N']) #Regressor for health and mana bar checker self.HR = MLPR([hmcIms, 1], maxIter = 0, name = 'hrmlp') self.MR = MLPR([hmcIms, 1], maxIter = 0, name = 'mrmlp') if not self.Restore(): print('Model could not be loaded.') self.TFS = self.LC.GetSes() self.DIM = LoadDataset() self.FitCModel(self.OC, {'Closed/Dried Lake':'C', 'Closed/Oasis':'C', 'Open/Dried Lake':'O', 'Open/Oasis':'O', 'Enemy/Dried Lake':'O', 'Enemy/Oasis':'O'}) self.FitCModel(self.EC, {'Open/Dried Lake':'N', 'Open/Oasis':'N', 'Enemy/Dried Lake':'Y', 'Enemy/Oasis':'Y', 'Item/Dried Lake':'N'}) self.FitCModel(self.MC, {'Move/Dried Lake':'Y', 'NoMove/Dried Lake':'N'}) #self.LC.Reinitialize() self.FitCModel(self.LC, {'LW/Dried Lake':'Y', 'LW/Oasis':'Y', 'NLW/Dried Lake':'N', 'NLW/Oasis':'N'}) self.FitRModel(self.HR, 'HR') self.FitRModel(self.MR, 'MR') self.Save()
def T9(): ''' Tests if multiple MLPRs can be created without affecting each other ''' A = np.random.rand(32, 4) Y = (A.sum(axis=1)**2).reshape(-1, 1) m1 = MLPR([4, 4, 1], maxIter=16) m1.fit(A, Y) s1 = m1.score(A, Y) m2 = MLPR([4, 4, 1], maxIter=16) m2.fit(A, Y) s2 = m1.score(A, Y) if s1 != s2: return False return True
def T8(): ''' Tests if multiple MLPRs can be created without affecting each other ''' A = np.random.rand(32, 4) Y = (A.sum(axis=1)**2).reshape(-1, 1) m1 = MLPR([4, 4, 1], maxIter=16) m1.fit(A, Y) R1 = m1.GetWeightMatrix(0) m2 = MLPR([4, 4, 1], maxIter=16) m2.fit(A, Y) R2 = m1.GetWeightMatrix(0) if (R1 != R2).any(): return False return True
def T13(): ''' Tests restoring a model from file ''' m1 = MLPR([4, 4, 1], maxIter=16, name='t12ann1') rv = m1.RestoreModel('./', 't12ann1') return rv
def T14(): ''' Tests saving and restore a model ''' A = np.random.rand(32, 4) Y = (A.sum(axis=1)**2).reshape(-1, 1) m1 = MLPR([4, 4, 1], maxIter=16, name='t12ann1') m1.fit(A, Y) m1.SaveModel('./t12ann1') R1 = m1.GetWeightMatrix(0) ANN.Reset() m1 = MLPR([4, 4, 1], maxIter=16, name='t12ann2') m1.RestoreModel('./', 't12ann1') R2 = m1.GetWeightMatrix(0) if (R1 != R2).any(): return False return True
def Main(): if len(sys.argv) <= 1: return A, Y = GenerateData(ns = 2048) #Create layer sizes; make 6 layers of nf neurons followed by a single output neuron L = [A.shape[1]] * 6 + [1] print('Layer Sizes: ' + str(L)) if sys.argv[1] == 'theano': print('Running theano benchmark.') from TheanoANN import TheanoMLPR #Create the Theano MLP tmlp = TheanoMLPR(L, batchSize = 128, learnRate = 1e-5, maxIter = 100, tol = 1e-3, verbose = True) MakeBenchDataSample(tmlp, A, Y, 16, 'TheanoSampDat.csv') print('Done. Data written to TheanoSampDat.csv.') if sys.argv[1] == 'theanogpu': print('Running theano GPU benchmark.') #Set optional flags for the GPU #Environment flags need to be set before importing theano os.environ["THEANO_FLAGS"] = "device=gpu" from TheanoANN import TheanoMLPR #Create the Theano MLP tmlp = TheanoMLPR(L, batchSize = 128, learnRate = 1e-5, maxIter = 100, tol = 1e-3, verbose = True) MakeBenchDataSample(tmlp, A, Y, 16, 'TheanoGPUSampDat.csv') print('Done. Data written to TheanoGPUSampDat.csv.') if sys.argv[1] == 'tensorflow': print('Running tensorflow benchmark.') from TFANN import MLPR #Create the Tensorflow model mlpr = MLPR(L, batchSize = 128, learnRate = 1e-5, maxIter = 100, tol = 1e-3, verbose = True) MakeBenchDataSample(mlpr, A, Y, 16, 'TfSampDat.csv') print('Done. Data written to TfSampDat.csv.') if sys.argv[1] == 'plot': print('Displaying results.') try: T1 = np.loadtxt('TheanoSampDat.csv', delimiter = ',', skiprows = 1) except OSError: T1 = None try: T2 = np.loadtxt('TfSampDat.csv', delimiter = ',', skiprows = 1) except OSError: T2 = None try: T3 = np.loadtxt('TheanoGPUSampDat.csv', delimiter = ',', skiprows = 1) except OSError: T3 = None fig, ax = mpl.subplots(1, 2) if T1 is not None: PlotBenchmark(T1[:, 0], T1[:, 1], ax[0], '# Samples', 'Train', 'Theano') PlotBenchmark(T1[:, 0], T1[:, 2], ax[1], '# Samples', 'Test', 'Theano') if T2 is not None: PlotBenchmark(T2[:, 0], T2[:, 1], ax[0], '# Samples', 'Train', 'Tensorflow') PlotBenchmark(T2[:, 0], T2[:, 2], ax[1], '# Samples', 'Test', 'Tensorflow') if T3 is not None: PlotBenchmark(T3[:, 0], T3[:, 1], ax[0], '# Samples', 'Train', 'Theano GPU') PlotBenchmark(T3[:, 0], T3[:, 2], ax[1], '# Samples', 'Test', 'Theano GPU') mpl.show()
def T12(): ''' Tests saving a model to file ''' A = np.random.rand(32, 4) Y = (A.sum(axis=1)**2).reshape(-1, 1) m1 = MLPR([4, 4, 1], maxIter=16, name='t12ann1') m1.fit(A, Y) m1.SaveModel('./t12ann1') return True
def score(self, A, y): #Number of neurons in the input layer i = 1 #Number of neurons in the output layer o = 1 #Number of neurons in the hidden layers h = 32 #The list of layer sizes layers = [i, h, h, h, h, h, h, h, h, h, o] mlpr = MLPR(layers, maxItr = 1000, tol = 0.40, reg = 0.001, verbose = True)
def T1(): ''' Tests basic functionality of MLPR ''' A = np.random.rand(32, 4) Y = np.random.rand(32, 1) a = MLPR([4, 4, 1], maxIter=16, name='mlpr1') a.fit(A, Y) a.score(A, Y) a.predict(A) return True
def CreateTFGraphTest(self): #Tensorflow 4 CNN Model #Classifier model; Architecture of the CNN ws = [('C', [3, 3, 3, 10], [1, 1, 1, 1]), ('P', [1, 4, 4, 1], [1, 2, 2, 1]), ('C', [3, 3, 10, 5], [1, 1, 1, 1]), ('P', [1, 4, 4, 1], [1, 2, 2, 1]), ('F', 32), ('F', 16), ('F', 2)] ims = [self.ss[1] // self.m, self.ss[0] // self.n, 3] #Image size for CNN model hmcIms = 30 * 100 * 3 #Number of pixels in health/mana checker images self.I1 = tf.placeholder("float", [self.NSS] + ims, name = 'S_I1') #Previous image placeholder self.I2 = tf.placeholder("float", [self.NSS] + ims, name = 'S_I2') #Current image placeholder self.TV = tf.placeholder("float", [self.NSS, 2], name = 'S_TV') #Target values for binary classifiers self.LWI = tf.placeholder("float", [2] + ims, name = 'S_LWI') self.LWTV = tf.placeholder("float", [2, 2], name = 'S_LWTV') self.HRI = tf.placeholder("float", [1, hmcIms], name = 'S_HRI') self.MRI = tf.placeholder("float", [1, hmcIms], name = 'S_MRI') self.RTV = tf.placeholder("float", [1, 1], name = 'S_RTV') Z = tf.zeros([self.NSS] + ims, name = "S_Z") #Completely black grid of image cells wcnd = tf.abs(self.I1 - self.I2) > 16 #Where condition ID = tf.where(wcnd, self.I2, Z, name = 'S_ID') #Difference between images #Used to detect Obstacles; carg = {'batchSize': self.NSS, 'learnRate': 1e-3, 'maxIter': 2, 'reg': 6e-4, 'tol': 1e-2, 'verbose': True} self.OC = CNNC(ims, ws, name = 'obcnn', X = self.I2, Y = self.TV, **carg) self.OC.RestoreClasses(['C', 'O']) #Used to detect enemies self.EC = CNNC(ims, ws, name = 'encnn', X = self.I2, Y = self.TV, **carg) self.EC.RestoreClasses(['N', 'E', 'I']) #CNN for detecting movement self.MC = CNNC(ims, ws, name = 'mvcnn', X = ID, Y = self.TV, **carg) self.MC.RestoreClasses(['Y', 'N']) #Classifier for lightning-warp self.LC = CNNC(ims, ws, name = 'lwcnn', X = self.LWI, Y = self.LWTV, **carg) self.LC.RestoreClasses(['Y', 'N']) #Regressor for health-bar checker self.HR = MLPR([hmcIms, 1], name = 'hrmlp', X = self.HRI, Y = self.RTV, **carg) self.MR = MLPR([hmcIms, 1], name = 'mrmlp', X = self.MRI, Y = self.RTV, **carg) if not self.Restore(): print('Model could not be loaded.') self.TFS = self.LC.GetSes()
print(stockData2) print(scale(stockDataASC)) # to get only specific columns of data from a array use : # data[:, [1, 9]] where data is array and you want columns 1, 9 (index start from 0) # scale the stock data, volume to ease the calculations and fit within the data range # Number of neurons in the input layer # 4 neurons to indicate the candle stick doji patterns # 4 neurons to indicate the previous most tested highs which are greater than previous day closing prices # 4 neurons to indicate the previous most tested lows which are less than previous day closing prices # 5 neurons to indicate the previous day open, close, high and low prices, and volume i = 17 # Number of neurons in the output layer # 5 neurons to indicate the current day open, close, high and low prices and volume o = 5 #Number of neurons in the hidden layers h = 17 #The list of layer sizes layers = [i, h, h, h, h, h, h, h, h, h, o] mlpr = MLPR(layers, maxIter=1000, tol=0.40, reg=0.001, verbose=True) mlpr.fit() #Begin prediction yHat = mlpr.predict(A) #Plot the results mpl.plot(A, Y, c='#b0403f') mpl.plot(A, yHat, c='#5aa9ab') mpl.show()