def trainModel(nHiddenNeurons, nInputNeurons, P0, T0): iw = np.random.uniform(-1, 1, (nHiddenNeurons, nInputNeurons)) bias = np.random.uniform(-1, 1, (1, nHiddenNeurons)) H0 = SigActFun(bias, P0, iw) m = np.linalg.pinv(np.dot(np.transpose(H0), H0)) beta = np.dot(np.linalg.pinv(H0), T0) model = Model(iw, bias, m, beta) return model
def calculateErrors(self, ensemble, Ptemp, Ttemp, n): errors = [] for modl in ensemble: Htemp = SigActFun(modl.Bias, Ptemp, modl.IW) Ytemp = np.dot(Htemp, modl.beta) e = mean_squared_error(Ttemp, Ytemp) errors.append(e) worstModel = errors.index(max(errors)) return worstModel
def calculateErrors(self, ensemble, Ptemp, Ttemp, n): errors = [] i = 0 for modl in ensemble: Htemp = SigActFun(modl.Bias, Ptemp, modl.IW) Ytemp = np.dot(Htemp, modl.beta) e = mean_squared_error(Ttemp, Ytemp) errors.append(e) modl.errores[n] = e # totalErrors[i,n] = e i += 1 # modl.errores.append(mean_squared_error(Ttemp, Ytemp)) # totalErrors[10,n] = np.mean(errors) worstModel = errors.index(max(errors)) return worstModel
def DOER(N0, nHiddenNeurons, Block, nModels, alfa, windowSize, dataset, trBlock): ##### Load dataset try: data = np.genfromtxt(dataSet, dtype=float) except Exception: print('An error ecurred while loading data') T = np.array(data[:, 0]) P = np.rray(data[:, 1:data.shape[1]]) nInputNeurons = data.shape[1] - 1 nTrainingData = data.shape[0] ##### Step 1 Initialization Phase P0 = P[0:N0, :] T0 = T[0:N0] PT = np.array(P[N0 - trBlock:nTrainingData, :]) TT = np.array(T[N0 - trBlock:nTrainingData]) Real = np.array(T[N0 - 1:nTrainingData - 1]) Y = np.zeros_like(Real) Error = np.zeros_like(Real) start_time = time() errorSum = 0 ensemble = [] ensemOutput = [] ensemble.append(trainModel(nHiddenNeurons, nInputNeurons, P0, T0)) nModelsCounter = 0 ##### Step 2 Sequential Learning Phase for n in range(trBlock, PT.shape[0]): Ptemp1 = np.array(PT[(n - windowSize):n]) Ttemp1 = np.array(TT[(n - windowSize):n]) # print("Ptemp1: ", Ptemp1, "Ttemp1: ", Ttemp1) SumW = 0 SumYW = 0 for modl in ensemble: H = SigActFun(modl.Bias, Ptemp1, modl.IW) g = np.dot(H, modl.beta) if nModelsCounter < 11: ensemOutput.append(g[-1]) SumW += modl.w SumYW += g[-1] * modl.w nModelsCounter += 1 modl.calculateError(g[-1], Ttemp1[-1], trBlock) modl.calculateMSE(trBlock) modl.calculateWeight(ensemble) K = np.dot(modl.M, np.transpose(H)) Q = np.linalg.inv(np.eye(windowSize) + np.dot(H, K)) R = np.dot(K, Q) S = np.dot(H, modl.M) M = modl.M - np.dot(R, S) beta = modl.beta + np.dot(np.dot(M, np.transpose(H)), (Ttemp1 - np.dot(H, modl.beta))) modl.beta = beta modl.M = M nModelsCounter = 0 modelObj.rank(ensemble) Y[n - trBlock] = SumYW / SumW # print("Y: ", (n-60), "n: ",n, "Y[n-60]: ",Y[n-60]) ensemOutput = [] errorSum += np.power((Ttemp1[-1] - Y[n - trBlock]), 2) Error[n - trBlock] = errorSum / ((n - trBlock) + 1) if Ttemp1[-1] != 0: a = abs(Y[n - trBlock] - Ttemp1[-1] / Ttemp1[-1]) if a > alfa: Ptemp2 = np.array(PT[(n - trBlock):n]) Ttemp2 = np.array(TT[(n - trBlock):n]) newModel = trainModel(nHiddenNeurons, nInputNeurons, Ptemp2, Ttemp2) if len(ensemble) < nModels: ensemble.append(newModel) else: ensemble = modelObj.replaceModels(ensemble, newModel) ##### Time in seconds end_time = time() totalTime = end_time - start_time hours, rest = divmod(totalTime, 3600) minutes, seconds = divmod(rest, 60) compTime = np.round(seconds, 5) ##### Acurracy MSE mse = mean_squared_error(Real, Y) accuracy = np.round(mse, 5) last_Real = Real[-200:] last_Y = Y[-200:] last_accuracy = mean_squared_error(last_Real, last_Y) return accuracy, compTime, Error, last_accuracy
def EOS(N0, nHiddenNeurons, Block, nModels, trBlock, maxWindowSize, dataset): modelObj = Model() ##### Load dataset try: data = np.genfromtxt(dataset, dtype = float) except Exception: print('An error ecurred while loading data') T = np.array(data[:,0]) P = np.array(data[:,1:data.shape[1]]) nInputNeurons = data.shape[1]-1 nTrainingData = data.shape[0] ##### Step 1 Initialization Phase P0 = P[0:N0,:] T0 = T[0:N0] PT = np.array(P[N0-maxWindowSize:nTrainingData,:]) TT = np.array(T[N0-maxWindowSize:nTrainingData]) Real = np.array(T[N0-1:nTrainingData-1]) Y = np.zeros_like(Real) Error = np.zeros_like(Real) start_time = time() errorSum = 0 ensemble = [] ensemOutput = [] for n in range(1, 11): b = random.randrange(maxWindowSize, N0) P0 = np.array(P[(N0 - b):N0]) T0 = np.array(T[(N0 - b):N0]) ensemble.append(trainModel(nHiddenNeurons, nInputNeurons, P0, T0)) nModelsCounter = 0 ##### Step 2 Sequential Learning Phase for n in range(maxWindowSize, PT.shape[0]): windowSize = maxWindowSize Ptemp1 = np.array(PT[(n-windowSize):n]) Ttemp1 = np.array(TT[(n-windowSize):n]) for modl in ensemble: beta = modl.beta IW = modl.IW Bias = modl.Bias M = modl.M H = SigActFun(Bias, Ptemp1, IW) g = np.dot(H,beta) if nModelsCounter < 10: ensemOutput.append(g[-1]) nModelsCounter += 1 modl.calculateError(g[-1],Ttemp1[-1], trBlock) modl.calculateMSE(trBlock) K = np.dot(M,np.transpose(H)) Q = np.linalg.inv(np.eye(windowSize) + np.dot(H, K)) R = np.dot(K, Q) S = np.dot(H, M) M = M - np.dot(R, S) beta = beta + np.dot(np.dot(M,np.transpose(H)),(Ttemp1-np.dot(H,beta))) modl.beta = beta modl.M = M nModelsCounter = 0 modelObj.rank(ensemble) Y[n-maxWindowSize] = np.average(ensemOutput) ensemOutput = [] errorSum += np.power((Ttemp1[-1]-Y[n-maxWindowSize]), 2) Error[n-maxWindowSize] = errorSum/((n-maxWindowSize)+1) if n % trBlock == 0 and n != 0: Ptemp2 = np.array(PT[(n - trBlock):n]) Ttemp2 = np.array(TT[(n - trBlock):n]) newModel = trainModel(nHiddenNeurons, nInputNeurons, Ptemp2, Ttemp2) worstModel = modelObj.calculateErrors(ensemble, Ptemp2, Ttemp2, i) if len(ensemble) < nModels: ensemble.append(newModel) else: ensemble = modelObj.replaceModels(ensemble, worstModel, newModel) ##### Time in seconds end_time = time() totalTime = end_time - start_time hours, rest = divmod(totalTime,3600) minutes, seconds = divmod(rest, 60) compTime = np.round(seconds,5) ##### Acurracy MSE mse = mean_squared_error(Real, Y) accuracy = np.round(mse,5) last_Real = Real[-200:] last_Y = Y[-200:] last_accuracy = mean_squared_error(last_Real, last_Y) return accuracy, compTime, Error, last_accuracy
def OSELM(N0, nHiddenNeurons, Block, windowSize, dataSet): ##### Load dataset try: data = np.genfromtxt(dataSet, dtype=float) except Exception: print('An error ecurred while loading data') T = np.array(data[:, 0]) P = np.array(data[:, 1:data.shape[1]]) nInputNeurons = data.shape[1] - 1 nTrainingData = data.shape[0] ##### Step 1 Initialization Phase P0 = P[0:N0, :] T0 = T[0:N0] PT = np.array(P[N0 - windowSize:nTrainingData, :]) TT = np.array(T[N0 - windowSize:nTrainingData]) Real = np.array(T[N0 - 1:nTrainingData - 1]) Y = np.zeros_like(Real) start_time = time() # Input weights randomly chosen from the range [-1,1] IW = np.random.uniform(-1, 1, (nHiddenNeurons, nInputNeurons)) # Biases randomly chosen from the range [-1,1] Bias = np.random.uniform(-1, 1, (1, nHiddenNeurons)) H0 = SigActFun(Bias, P0, IW) M = np.linalg.pinv(np.dot(np.transpose(H0), H0)) beta = np.dot(np.linalg.pinv(H0), T0) for n in range(windowSize, PT.shape[0]): Ptemp1 = np.array(PT[(n - windowSize):n]) Ttemp1 = np.array(TT[(n - windowSize):n]) H = SigActFun(Bias, Ptemp1, IW) g = np.dot(H, beta) Y[n - windowSize] = g[-1] K = np.dot(M, np.transpose(H)) Q = np.linalg.inv(np.eye(windowSize) + np.dot(H, K)) R = np.dot(K, Q) S = np.dot(H, M) M = M - np.dot(R, S) beta = beta + np.dot(np.dot(M, np.transpose(H)), (Ttemp1 - np.dot(H, beta))) ##### Time in seconds end_time = time() totalTime = end_time - start_time hours, rest = divmod(totalTime, 3600) minutes, seconds = divmod(rest, 60) compTime = np.round(seconds, 5) ##### Acurracy MSE mse = mean_squared_error(Real, Y) accuracy = np.round(mse, 5) return accuracy, compTime
def EOS(N0, nHiddenNeurons, Block, nModels, trBlock, windowSize, dataSet): modelObj = Model() ##### Load dataset try: data = np.genfromtxt(dataSet, dtype = float) except Exception: print('An error ecurred while loading data') T = np.array(data[:,0]) P = np.array(data[:,1:data.shape[1]]) nInputNeurons = data.shape[1]-1 nTrainingData = data.shape[0] ##### Step 1 Initialization Phase P0 = P[0:N0,:] T0 = T[0:N0] PT = np.array(P[N0-windowSize:nTrainingData,:]) TT = np.array(T[N0-windowSize:nTrainingData]) Real = np.array(T[N0-1:nTrainingData-1]) Y = np.zeros_like(Real) start_time = time() ensemble = [] ensemOutput = [] ensemble.append(trainModel(nHiddenNeurons, nInputNeurons, P0, T0)) ##### Step 2 Sequential Learning Phase for n in range(windowSize, PT.shape[0]): Ptemp1 = np.array(PT[(n-windowSize):n]) Ttemp1 = np.array(TT[(n-windowSize):n]) i = 0 for modl in ensemble: beta = modl.beta IW = modl.IW Bias = modl.Bias M = modl.M H = SigActFun(Bias, Ptemp1, IW) g = np.dot(H,beta) ensemOutput.append(g[windowSize-1]) # e = modl.calculateError(g[windowSize-1],Ttemp1[windowSize-1], windowSize) # totalErrors[i,n-windowSize] = e # i += 1 K = np.dot(M,np.transpose(H)) Q = np.linalg.inv(np.eye(windowSize) + np.dot(H, K)) R = np.dot(K, Q) S = np.dot(H, M) M = M - np.dot(R, S) beta = beta + np.dot(np.dot(M,np.transpose(H)),(Ttemp1-np.dot(H,beta))) modl.beta = beta modl.M = M Y[n-windowSize] = np.average(ensemOutput) ensemOutput = [] if n % trBlock == 0 and n != 0 : Ptemp2 = np.array(PT[(n - trBlock):n]) Ttemp2 = np.array(TT[(n - trBlock):n]) newModel = trainModel(nHiddenNeurons, nInputNeurons, Ptemp2, Ttemp2) worstModel = modelObj.calculateErrors(ensemble, Ptemp2, Ttemp2, i) if len(ensemble) < nModels: ensemble.append(newModel) else: ensemble = modelObj.replaceModels(ensemble, worstModel, newModel) ##### Time in seconds end_time = time() totalTime = end_time - start_time hours, rest = divmod(totalTime,3600) minutes, seconds = divmod(rest, 60) compTime = np.round(seconds,5) ##### Acurracy MSE mse = mean_squared_error(Real, Y) accuracy = np.round(mse,5) return accuracy, compTime