GatoBooster

intento so

Nov 9th, 2021
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.76 KB | None | 0 0
  1. # My Utility : auxiliars functions
  2. #my_utility
  3. import pandas as pd
  4. import numpy  as np
  5.  
  6. import random as rd
  7. import math
  8.  
  9. #Initialize  Swarm
  10. #Nh = nodos ocultos
  11. #Np = numero de particulas
  12. #d = largo del enjambre
  13. def iniSwarm(Nh, Np, d):
  14.     X = np.zeros((Np,(Nh*d)), dtype=float)
  15.     for i in range(Np):
  16.         wh = randW(Nh,d)
  17.         a = np.reshape(wh, (1, Nh*d))
  18.         X[i] = a
  19.     return(X)
  20.  
  21. # Initialize random weights
  22. def randW(next_, prev_):
  23.     w = np.random.random((next_, prev_))
  24.     r = np.sqrt(6 / ( next_ + prev_ ))
  25.     w = ( w * 2 * r ) - r
  26.     return(w)
  27.  
  28. # MLP-fitness
  29. #x =
  30. #y =
  31. #X =
  32. #C =
  33. #param[0] Max. Iterations -> 200
  34. #param[1] Number of nodes -> 10
  35. #param[2] Number of particle -> 20
  36. #param[3] Penalidad C of the Pinv -> 100
  37. def mlp_Fitness(x, y, X, C):  
  38.    
  39.     nh = int(X.shape[1]/x.shape[0])
  40.     W2 = np.zeros((X.shape[0], nh), dtype=float) #matriz de peso para la capa de salida
  41.     MSE = np.zeros(X.shape[0] , dtype=float)
  42.     for i in range(X.shape[0]):
  43.         p = X[i]
  44.         W1 = np.reshape(p,(x.shape[1],nh))
  45.         print(W1.shape,x.shape)
  46.         H = act_function(x, W1)
  47.         W2[i] = w2_pinv(H, y, C)
  48.         #ze = np.matmul(W2[i], H)
  49.         #MSE[i] = math.sqrt(MSE(y, ze))
  50.     return(W2, MSE)
  51.  
  52. # Update Particle fitness
  53. #param[2] = num_p = Número de particulas
  54. #X = Enjambre
  55. #P['Pos']   = Best particle position  
  56. #P['Fit']   = Best particle fitness
  57. #P['gBest'] = Best global solution
  58. #P['gFit'] = Best global Fitness
  59. #w2Best = Mejor peso
  60. #MSE = Error cuadratico medio
  61. def upd_pFitness(X, P, new_pfitness, new_beta):    
  62.     for i in range(X.shape[0]):  
  63.         if (P['Fit'][i] > new_pfitness[i]):
  64.             P['Fit'][i] = new_pfitness[i]
  65.             P['Pos'][i][:] = X[i, :]
  66.            
  67.     new_gfitness = min(P['Fit'])
  68.     idx = np.argmin(P['gFit'])
  69.  
  70.     if (new_gfitness < P['gFit']):
  71.         P['gFit'] = new_gfitness
  72.         P['gBest'] = P['Pos'][idx][:]
  73.         w2Best = new_beta[idx][:]
  74.     return(P, w2Best)      
  75.  
  76. '''# Update Velocity of PSO
  77. def upd_veloc():
  78.    #complete
  79.    return(...)
  80. '''
  81. # Update Swarm of QPSO
  82. def upd_swarm(P, iTer, alpha, X):
  83.     avg_t = 0
  84.     mBest = P['Pos'].mean(axis = 0)
  85.    
  86.     for i in range(X.shape[0]):
  87.         for j in range(X.shape[1]):
  88.             phi = rd.random()
  89.             u = rd.random()
  90.                        
  91.             P['Pos'][i][j]  = ( phi * P['Pos'][i][j] + (1 - phi) * P['gBest'][j])
  92.            
  93.             z = alpha[iTer] * math.abs( mBest[j] - X[i][j] ) * math.log( 1 / u )
  94.            
  95.             avg_t += z
  96.            
  97.             if rd.random() > 0.5:
  98.                 X[i][j] = P['Pos'][i][j] + z
  99.             else:
  100.                 X[i][j] = P['Pos'][i][j] - z    
  101.        
  102.         avg_t = avg_t / ( X.shape[0] * X.shape[1] )
  103.        
  104.     return(P, X)
  105.  
  106.  
  107. # Update W2 via P-inverse
  108. def w2_pinv(H, y, C):
  109.     #print(f"H {H} y {y} C {C}")
  110.     yh = np.matmul(np.transpose(y), np.transpose(H))
  111.     hh = np.matmul(H, np.transpose(H))
  112.     hh += np.eye(hh.shape[0]) / C
  113.     w2 = np.matmul(np.transpose(yh), np.linalg.pinv(hh))  
  114.     return(w2)
  115.  
  116. # Feed-forward of ANN
  117. def forward(xv,w1,w2):    
  118.     param = load_config()
  119.     w1 = w1.reshape((param[1],xv.shape[0]+1))
  120.     H = act_function(xv, w1)
  121.     zv = np.matmul(w2,H)
  122.    
  123.     for number in range(len(zv)):
  124.         if zv[number] < 0:
  125.             zv[number] = -1
  126.         else:
  127.             zv[number] = 1
  128.     return(zv)
  129.  
  130. # activation function
  131. def act_function(x_n, w_j):
  132.     z = np.matmul(w_j,x_n)
  133.     print(z.shape)
  134.     for number in z:
  135.         number = np.exp(-1*(number*number))
  136.     print(z.shape)
  137.     return z
  138.  
  139. def distance_e(x,w):
  140.     d = 0
  141.     w = w.transpose()
  142.     print(x.shape, w.shape)
  143.    
  144.     d = np.where(x.shape[0] == w.shape[0], ((x-w)**2)**(1/2), 0)
  145.     print(f"d = {d}")
  146.     #d = ( (x[0] – w[0])**2 + (x[1] – w[1])**2 )**(1/2)
  147.     return d
  148.  
  149. # Measure
  150. def metrica(y_expected,y_get):
  151.     #se llama con ut.metrica(yv,zv)
  152.     confusion_matrix(y_expected,y_get)
  153.     vp = 0
  154.     fp = 0
  155.     fn = 0
  156.     vn = 0
  157.  
  158.     for i in range(len(y_expected)):
  159.         if y_expected[i] == 1 and y_get[i] == 1:
  160.             vp += 1
  161.         if y_expected[i] == -1 and y_get[i] == -1:
  162.             vn += 1
  163.         if y_expected[i] == 1 and y_get[i] == -1:
  164.             fn += 1
  165.         if y_expected[i] == -1 and y_get[i] == 1:
  166.             fp += 1
  167.  
  168.     accuracy_normal = vp/(vp+fp)
  169.     recall_normal = vp/(vp+fn)
  170.  
  171.     f_score_normal = 2*(accuracy_normal*recall_normal/(accuracy_normal+recall_normal))
  172.    
  173.     accuracy_attack = vn/(vn+fn)
  174.     recall_attack = vn/(vn + fp)
  175.     f_score_attack = 2*(accuracy_attack*recall_attack/(accuracy_attack + recall_attack))
  176.  
  177.     pd.DataFrame(accuracy_normal, f_score_normal, f_score_attack).to_csv("fscore_ann.csv")
  178.  
  179.  
  180. #Confusion matrix
  181. def confusion_matrix(a,b):
  182.     pd.DataFrame(a, b).to_csv("cmatrix_ann.csv")
  183.  
  184.  
  185. # Norm of the data
  186. def norm_data(df_data = pd.DataFrame()):
  187.     a = 0.01
  188.     b = 0.99
  189.     df_data = pd.DataFrame(df_data)
  190.     # extraer datos para normalizar
  191.     X = df_data.loc[:, df_data.columns != 41]
  192.     Y = df_data.loc[:, df_data.columns == 41]
  193.  
  194.     #normalizar los datos mediante la fórmula
  195.     X_normalized = ( X - X.min() ) / ( X.max() - X.min() )
  196.     X_normalized *= ( b - a ) + a
  197.     X_normalized = X_normalized.fillna(0)
  198.  
  199.     #Exportar datos
  200.     X_normalized[41] = Y
  201.     return(X_normalized)
  202. #
  203.  
  204. #------------------------------------------------------------------------
  205. #      LOAD-SAVE
  206. #-----------------------------------------------------------------------
  207. # Configuration of the ANN
  208. def load_config():      
  209.     param = np.genfromtxt("config_ann.csv",delimiter=',',dtype=None)    
  210.     par=[]    
  211.     par.append(np.int16(param[0])) # Max. Iterations
  212.     par.append(np.int16(param[1])) # Number of nodes
  213.     par.append(np.int16(param[2])) # Number of particle
  214.     par.append(np.float(param[3])) # Penalidad C of the Pinv.
  215.     return(par)
  216. # Load data
  217.  
  218. def load_data(fname):
  219.     x  = pd.read_csv(fname, header = None)
  220.     #x  = np.array(x)
  221.     #down
  222.     df_data = norm_data(x)
  223.     x = df_data.loc[:, df_data.columns != 41]
  224.     y = df_data.loc[:, df_data.columns == 41]
  225.     return(x.to_numpy(), y.to_numpy())
  226.  
  227. # save ANN's weights in numpy format and cost in csv format
  228. def save_w(w1,w2,cost):
  229.     #complete    
  230.     np.savez("pesos_ann", w1 = w1, w2 = w2)
  231.  
  232.     pd.DataFrame(cost).to_csv("cost_ann.csv") #SUJETO A MODIFICACIONES
  233.    
  234. #load AN's weight in numpy format
  235. def load_w():
  236.     #complete}
  237.     open("pesos_ann.npz", "w").close()
  238.     data = np.load("pesos_ann.npz")
  239.  
  240.     data_weight = {data}
  241.     #fomatear los datos
  242.     data_weight = [data[key] for key in data]
  243.     w1 = data_weight[0]
  244.     w2 = data_weight[1]
  245.  
  246.     return(w1,w2)
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
Advertisement
Add Comment
Please, Sign In to add comment