Neural network reproducibility and metaheuristics

username: AaronPonceSandoval
cooked: <p>hi, i have a problem with the neural network optimization reproducibility with the black hole algorithm (metaheuristics). I am maximizing the accuracy by exploring the learning ratio, but at the time of reproducing the results I am not able to obtain the same. My optimal result is in iteration 20 of the black hole algorithm. I do not understand why I cannot reproduce the results of the network in an isolated way. I am using the seed as follows, I am attaching code. In advance thank you very much for the help.</p>
<pre><code class="lang-auto">import random
from numpy.random import seed
torch.manual_seed(1)
torch.cuda.manual_seed_all(1)
np.random.seed(1)
random.seed(1)
seed(1)
</code></pre>
<pre><code class="lang-auto">class BlackHole:


    in_features = 23520
    n_etiquetas = 4

    tipo = "clasificacion"
    directorio_de_entrenamiento = "parches_2_patologo2_aumentado_10"
    rutas_csv_entrenamiento = "train_a.csv"
    directorio_de_validacion = "parches_2_patologo2_aumentado_10"
    rutas_csv_validacion = "validacion_a.csv"

    train_simple_dataset = SimpleDataLoader(rutas_csv_entrenamiento,directorio_de_entrenamiento, tipo,"entrenamiento")
    dataloader_entrenamiento = DataLoader(train_simple_dataset, batch_size=60)

    valid_simple_dataset = SimpleDataLoader(rutas_csv_validacion,directorio_de_validacion, tipo,"validacion")
    dataloader_validacion = DataLoader(valid_simple_dataset, batch_size=60)

    model = Net(in_features,n_etiquetas)
    criterion = nn.CrossEntropyLoss()

    if torch.cuda.is_available():
        model.cuda()

    params =model.parameters()
    epocas = 30

    Total_de_Estrellas = 5
    Dimencion_de_vector = 1
    Iteraciones = 100
    constelacion = []
    ancho = 224
    alto = 224
    directorio = "./union_imagenes/"
    registos_excel = "./nuevo_conjunto.xlsx"
   

    def run(self):
        inicio_mh = time.time()
     
        print("Creando Poblacion")
        blackhole_mov = self.Create_blackhole_mov()
        self.constelacion = self.creacion_constelacion()
        print("Iniciando problacion")
        for iteracion in range(self.Iteraciones):
            print("ITERACION: {}".format(iteracion))
            self.constelacion = self.fit_constelacion()
            print('constelacion 0', self.constelacion)
            blackhole = np.copy(self.constelacion[self.detectar_agujero()])
            if(np.float64(blackhole[1])&gt;np.float64(blackhole_mov[1])):
                blackhole = blackhole_mov
            print("blackhole de poblacion: ",blackhole)
            wandb.log({"blackhole poblacion":  blackhole[1],"epoch": iteracion})


            self.constelacion = np.copy(self.movimiento(blackhole[0]))
            self.constelacion = self.fit_constelacion()
            print("Poblacion con movimiento: {}".format(self.constelacion))
            blackhole_mov = np.copy(self.constelacion[self.detectar_agujero()])
            print("blackhole de poblacion mov: ",blackhole_mov)
            if(np.float64(blackhole[1])&lt;np.float64(blackhole_mov[1])):
                blackhole_mov = blackhole
            print("BlackHole Ganador!:".format(blackhole_mov))
            wandb.log({"blackhole mov":  blackhole_mov[1],"epoch": iteracion})

            
            
            blackhole_mov[2]=self.horizonte_evento(blackhole_mov)

            self.constelacion = self.Absorcion(blackhole_mov)

        fin_mh  =time.time()
        total = (fin_mh-inicio_mh)/60
        print("Tiempo utilizado: {}".format(total))
        print('Best Black Hole Found: ',' '.join(map(str,blackhole_mov[0])),'Fit:', blackhole_mov[1],'Radius',blackhole_mov[2],'loss', blackhole_mov[3])
        return blackhole_mov

    """
        Metodos de Creacion
    """
    
    def creacion_estrellas(self):
        position = np.append(np.float64(np.random.uniform(0.0001,0.0002,size=(1, self.Dimencion_de_vector))),np.random.randint(100, size=1))
        metrica = 0
        radio = 0
        loss = 0
        acc = 0
        area_bajo_acc = 0
        star = [position,metrica,radio,loss,acc,area_bajo_acc]
        return np.array(star)

    def creacion_constelacion(self):
        constelacion = []
        for i in range(self.Total_de_Estrellas):
            constelacion.append(self.creacion_estrellas())
        return np.array(constelacion)
    
    def Create_blackhole_mov(self):
        position = np.zeros(self.Dimencion_de_vector)
        fit = 10000
        radio = 0
        star = [position,fit,radio]
        return np.array(star)

    """
        Metodos relacionados con Movimiento de Constelacion
    """
    
    def movimiento(self, blackhole):
        for i in range(self.Total_de_Estrellas):
            self.constelacion[i,0] = self.constelacion[i,0] + np.random.uniform()*(blackhole - self.constelacion[i,0])        
        return self.constelacion
    
    def distancia(self,blackhole,constellation):
        return np.sum((blackhole[0]-constellation[0])&lt;blackhole[2])


    """
        Metodos Relacionados Con Absorcion
    """

    def Absorcion(self,blackhole):
        index=int(np.where(self.constelacion[:,1] == min(self.constelacion[:,1]))[0][0])
        for i in range(self.Total_de_Estrellas):
            if(i == index):
                None
            else:
                if(self.distancia(blackhole,self.constelacion[i])==self.Dimencion_de_vector):
                    self.constelacion[i]= self.creacion_estrellas()
        return self.constelacion

    def horizonte_evento(self,blackhole):
        return np.float64((np.divide(blackhole[1], np.sum(self.constelacion[:,1], dtype=np.float64) ,dtype=np.float64  )))
 
    """
        Metodos Relacionados con Fit
    """
    def fitX(self,importancia):
        #return VGG16m().modelo(importancia[0],self.x_train,self.y_train,self.x_test,self.y_test)
        print("importancia: ",importancia)
        return VGG16m().run(importancia[0],self.x_test,self.x_train,self.y_test,self.y_train)

    def fit_constelacion(self):
        for i in range(self.Total_de_Estrellas):
            print("Estrella {}:".format(i))
            print("constelacion[i]: {} ".format(self.constelacion[i]))
            self.constelacion[i][1], self.constelacion[i][3],self.constelacion[i][4],self.constelacion[i][5] = self.fit(self.constelacion[i])
        return self.constelacion
    
    def detectar_agujero(self):
        return int(np.where(self.constelacion[:,1] == min(self.constelacion[:,1]))[0][0])

    def fit(self, lr):
        print(lr)
        dataloader_entrenamiento = self.dataloader_entrenamiento
        model = self.model
        criterion = self.criterion
        epocas = self.epocas

        optimizador = optim.Adagrad(model.parameters(), lr=lr[0][0], lr_decay=0, weight_decay=0, initial_accumulator_value=0, eps=1e-10)
        inicio = time.time()
        for epoch in range(0,epocas):
            #entrenamiento.train(dataloader_entrenamiento, model, criterion, optimizador, epoch)
            model = entrenamiento.train(dataloader_entrenamiento, model, criterion,
                                    optimizador, epoch, losses,accuracy_,recall_,precision_,kappa_,accuracy,precision,recall,cohenkappa,enviar_wand)
            #entrenamiento.validacion(dataloader_validacion, model,criterion, epoch)
            #entrenamiento.validacion(dataloader_validacion, model, criterion, epoch,val_accuracy_,val_recall_,val_precision_,val_kappa_,val_losses,val_cohenkappa,val_accuracy,enviar_wand)
            model,total_accuracy,lossbh = entrenamiento.validacion(dataloader_entrenamiento, model, criterion,
                                    epoch, losses,val_accuracy,val_precision,val_recall,val_cohenkappa,enviar_wand)
            scheduler.step()
        print("Entrenamiento 1 Finalizado")
        print("Resultados Acc:{} Loss:{}".format(total_accuracy.item(),lossbh.item()))
        fin = time.time()
        total = (fin - inicio)/60
        print("Tiempo de Entrenamiento: {}".format(total))
        return -(total_accuracy.item()),lossbh.item(), 0, 0
</code></pre>

username: ptrblck
cooked: <p>The <a href="https://pytorch.org/docs/stable/notes/randomness.html">reproducibility docs</a> give you more information e.g. on how to enable deterministic algorithms etc., which might be helpful.</p>