tools/utilities/pythonlibs/audio/training/train_classifier.py
def fit(self, training_data, validation_data, batch_size=64, num_epochs=30, learning_rate=0.001, weight_decay=0, device=None):
        """
        Perform the training.  This is not called "train" because the base class already defines
        that method with a different meaning.  The base class "train" method puts the Module into
        "training mode".
        """
        print("Training keyword spotter using {} rows of featurized training input...".format(training_data.num_rows))
        start = time.time()
        loss_function = nn.NLLLoss()
        optimizer = optim.RMSprop(self.parameters(), lr=learning_rate, weight_decay=weight_decay)
        #optimizer = optim.Adam(model.parameters(), lr=0.0001)

        last_accuracy = 0
        for epoch in range(num_epochs):
            self.train()
            for i_batch, (audio, labels) in enumerate(training_data.get_data_loader(batch_size)):

                if not self.batch_first:
                    audio = audio.transpose(1, 0) # GRU wants seq,batch,feature
                    
                if device:
                    audio = audio.to(device)
                    labels = labels.to(device)

                # Also, we need to clear out the hidden state,
                # detaching it from its history on the last instance.
                self.init_hidden()
                
                # Before the backward pass, use the optimizer object to zero all of the
                # gradients for the variables it will update (which are the learnable
                # weights of the model). This is because by default, gradients are
                # accumulated in buffers( i.e, not overwritten) whenever .backward()
                # is called. Checkout docs of torch.autograd.backward for more details.
                optimizer.zero_grad()

                # Run our forward pass.
                keyword_scores = self(audio)

                # Compute the loss, gradients
                loss = loss_function(keyword_scores, labels)
                
                # Backward pass: compute gradient of the loss with respect to all the learnable
                # parameters of the model. Internally, the parameters of each Module are stored
                # in Tensors with requires_grad=True, so this call will compute gradients for
                # all learnable parameters in the model.            
                loss.backward()

                # Calling the step function on an Optimizer makes an update to its parameters
                # applying the gradients we computed during back propagation
                optimizer.step()

            # Find the best prediction in each sequence and return it's accuracy
            passed, total, rate = self.evaluate(validation_data, batch_size, device)
            print("Epoch {}, Loss {}, Validation Accuracy {:.3f}".format(epoch, loss.item(), rate * 100))
        
        end = time.time()
        print("Trained in {:.2f} seconds".format(end - start))

        Perform the training.  This is not called "train" because the base class already defines
        that method with a different meaning.  The base class "train" method puts the Module into
        "training mode".
        
Perform the training.  This is not called "train" because the base class already defines
that method with a different meaning.  The base class "train" method puts the Module into
"training mode".
Perform the training . This is not called " train " because the base class already defines that method with a different meaning . The base class " train " method puts the Module into " training mode " .
def fit(self, training_data, validation_data, batch_size=64, num_epochs=30, learning_rate=0.001, weight_decay=0, device=None):
        print("Training keyword spotter using {} rows of featurized training input...".format(training_data.num_rows))
        start = time.time()
        loss_function = nn.NLLLoss()
        optimizer = optim.RMSprop(self.parameters(), lr=learning_rate, weight_decay=weight_decay)
        last_accuracy = 0
        for epoch in range(num_epochs):
            self.train()
            for i_batch, (audio, labels) in enumerate(training_data.get_data_loader(batch_size)):
                if not self.batch_first:
                    audio = audio.transpose(1, 0) 
                if device:
                    audio = audio.to(device)
                    labels = labels.to(device)
                self.init_hidden()
                optimizer.zero_grad()
                keyword_scores = self(audio)
                loss = loss_function(keyword_scores, labels)
                loss.backward()
                optimizer.step()
            passed, total, rate = self.evaluate(validation_data, batch_size, device)
            print("Epoch {}, Loss {}, Validation Accuracy {:.3f}".format(epoch, loss.item(), rate * 100))
        end = time.time()
        print("Trained in {:.2f} seconds".format(end - start))
def fit ( self , training_data , validation_data , batch_size = 64 , num_epochs = 30 , learning_rate = 0.001 , weight_decay = 0 , device = None ) : print ( "Training keyword spotter using {} rows of featurized training input..." . format ( training_data . num_rows ) ) start = time . time ( ) loss_function = nn . NLLLoss ( ) optimizer = optim . RMSprop ( self . parameters ( ) , lr = learning_rate , weight_decay = weight_decay ) last_accuracy = 0 for epoch in range ( num_epochs ) : self . train ( ) for i_batch , ( audio , labels ) in enumerate ( training_data . get_data_loader ( batch_size ) ) : if not self . batch_first : audio = audio . transpose ( 1 , 0 ) if device : audio = audio . to ( device ) labels = labels . to ( device ) self . init_hidden ( ) optimizer . zero_grad ( ) keyword_scores = self ( audio ) loss = loss_function ( keyword_scores , labels ) loss . backward ( ) optimizer . step ( ) passed , total , rate = self . evaluate ( validation_data , batch_size , device ) print ( "Epoch {}, Loss {}, Validation Accuracy {:.3f}" . format ( epoch , loss . item ( ) , rate * 100 ) ) end = time . time ( ) print ( "Trained in {:.2f} seconds" . format ( end - start ) )
Perform the training.
Perform the training .
"""
        Perform the training.  This is not called "train" because the base class already defines
        that method with a different meaning.  The base class "train" method puts the Module into
        "training mode".
        """ #optimizer = optim.Adam(model.parameters(), lr=0.0001) # GRU wants seq,batch,feature # Also, we need to clear out the hidden state, # detaching it from its history on the last instance. # Before the backward pass, use the optimizer object to zero all of the # gradients for the variables it will update (which are the learnable # weights of the model). This is because by default, gradients are # accumulated in buffers( i.e, not overwritten) whenever .backward() # is called. Checkout docs of torch.autograd.backward for more details. # Run our forward pass. # Compute the loss, gradients # Backward pass: compute gradient of the loss with respect to all the learnable # parameters of the model. Internally, the parameters of each Module are stored # in Tensors with requires_grad=True, so this call will compute gradients for # all learnable parameters in the model.             # Calling the step function on an Optimizer makes an update to its parameters # applying the gradients we computed during back propagation # Find the best prediction in each sequence and return it's accuracy
self
training_data
validation_data
batch_size
num_epochs
learning_rate
weight_decay
device