original_docstring: 
      Performs the training of the POS-tagging task
      Prints the accuracy that is obtained from the test set
      
docstring: Performs the training of the POS-tagging task
  Prints the accuracy that is obtained from the test set
docstring_tokens: Performs the training of the POS - tagging task Prints the accuracy that is obtained from the test set
code: def train(data):
      '''
      Performs the training of the POS-tagging task
      Prints the accuracy that is obtained from the test set
      '''
  
      learning_rate = 1e-3
      batch_size = 64
      epochs = 500   
  
      loss = nn.CrossEntropyLoss()
  
      train_x = data['train'][0]
      train_y = data['train'][1]
      vocab = data['train'][2]
      dev_x = data['dev'][0]
      dev_y = data['dev'][1]
      test_x = data['test'][0]
      test_y = data['test'][1]
  
      model_DC = DC(train_x.size(1), len(vocab)).to(device)
      optimizer = torch.optim.Adam(model_DC.parameters(), lr=learning_rate)
      
      total_batches = int(len(train_x)/batch_size)+1    
      total_batches_dev = int(len(dev_x)/batch_size)+1
      total_batches_test = int(len(test_x)/batch_size)+1
      min_loss = np.inf
      patience = 0
      max_patience = 5
      
      for epoch in range(epochs):
          losses = []
          for i in range(total_batches):
              if i < total_batches-1:
                  features = train_x[i*batch_size:(i+1)*batch_size].to(dtype=torch.float, device=device)
                  labels = train_y[i*batch_size:(i+1)*batch_size].to(dtype=torch.long, device=device)
              else:
                  features = train_x[i*batch_size:].to(dtype=torch.float, device=device)
                  labels = train_y[i*batch_size:].to(dtype=torch.long, device=device)
  
  
              preds = model_DC(features)
              loss_out = loss(preds, labels)
  
              optimizer.zero_grad()
              loss_out.backward()
              optimizer.step()
              losses.append(loss_out.item())
              
          loss_train = np.mean(losses)
          
          print("\n---------------------------------------------------------")
          print("epoch: " + str(epoch+1))
          print("training set loss: " + str(loss_train))
          
          with torch.no_grad():
              losses = []
              for i in range(total_batches_dev):
                  if i < total_batches_dev-1:
                      features = dev_x[i*batch_size:(i+1)*batch_size].to(dtype=torch.float, device=device)
                      labels = dev_y[i*batch_size:(i+1)*batch_size].to(dtype=torch.long, device=device)
                  else:
                      features = dev_x[i*batch_size:].to(dtype=torch.float, device=device)
                      labels = dev_y[i*batch_size:].to(dtype=torch.long, device=device)
  
                  preds = model_DC(features)
                  loss_out = loss(preds, labels)
                  losses.append(loss_out.item())
              
              loss_dev = np.mean(losses)
              
              #Early Stopping
              if loss_dev>min_loss:
                  patience+=1
              else:
                  min_loss = loss_dev
                  patience = 0
  
              print("validation set loss: " + str(loss_dev) + ", patience: " + str(patience))
              print("---------------------------------------------------------")
  
              if patience==max_patience:
                  print("\n---------------------------------------------------------")
                  print("Early Stopping at epoch:", str(epoch+1))
                  break
  
      evaluate_pos(test_x, test_y, model_DC, total_batches_test, batch_size)
code_tokens: def train ( data ) : learning_rate = 1e-3 batch_size = 64 epochs = 500 loss = nn . CrossEntropyLoss ( ) train_x = data [ 'train' ] [ 0 ] train_y = data [ 'train' ] [ 1 ] vocab = data [ 'train' ] [ 2 ] dev_x = data [ 'dev' ] [ 0 ] dev_y = data [ 'dev' ] [ 1 ] test_x = data [ 'test' ] [ 0 ] test_y = data [ 'test' ] [ 1 ] model_DC = DC ( train_x . size ( 1 ) , len ( vocab ) ) . to ( device ) optimizer = torch . optim . Adam ( model_DC . parameters ( ) , lr = learning_rate ) total_batches = int ( len ( train_x ) / batch_size ) + 1 total_batches_dev = int ( len ( dev_x ) / batch_size ) + 1 total_batches_test = int ( len ( test_x ) / batch_size ) + 1 min_loss = np . inf patience = 0 max_patience = 5 for epoch in range ( epochs ) : losses = [ ] for i in range ( total_batches ) : if i < total_batches - 1 : features = train_x [ i * batch_size : ( i + 1 ) * batch_size ] . to ( dtype = torch . float , device = device ) labels = train_y [ i * batch_size : ( i + 1 ) * batch_size ] . to ( dtype = torch . long , device = device ) else : features = train_x [ i * batch_size : ] . to ( dtype = torch . float , device = device ) labels = train_y [ i * batch_size : ] . to ( dtype = torch . long , device = device ) preds = model_DC ( features ) loss_out = loss ( preds , labels ) optimizer . zero_grad ( ) loss_out . backward ( ) optimizer . step ( ) losses . append ( loss_out . item ( ) ) loss_train = np . mean ( losses ) print ( "\n---------------------------------------------------------" ) print ( "epoch: " + str ( epoch + 1 ) ) print ( "training set loss: " + str ( loss_train ) ) with torch . no_grad ( ) : losses = [ ] for i in range ( total_batches_dev ) : if i < total_batches_dev - 1 : features = dev_x [ i * batch_size : ( i + 1 ) * batch_size ] . to ( dtype = torch . float , device = device ) labels = dev_y [ i * batch_size : ( i + 1 ) * batch_size ] . to ( dtype = torch . long , device = device ) else : features = dev_x [ i * batch_size : ] . to ( dtype = torch . float , device = device ) labels = dev_y [ i * batch_size : ] . to ( dtype = torch . long , device = device ) preds = model_DC ( features ) loss_out = loss ( preds , labels ) losses . append ( loss_out . item ( ) ) loss_dev = np . mean ( losses ) if loss_dev > min_loss : patience += 1 else : min_loss = loss_dev patience = 0 print ( "validation set loss: " + str ( loss_dev ) + ", patience: " + str ( patience ) ) print ( "---------------------------------------------------------" ) if patience == max_patience : print ( "\n---------------------------------------------------------" ) print ( "Early Stopping at epoch:" , str ( epoch + 1 ) ) break evaluate_pos ( test_x , test_y , model_DC , total_batches_test , batch_size )
short_docstring: Performs the training of the POS-tagging task
  Prints the accuracy that is obtained from the test set
short_docstring_tokens: Performs the training of the POS - tagging task Prints the accuracy that is obtained from the test set
comment: '''
      Performs the training of the POS-tagging task
      Prints the accuracy that is obtained from the test set
      '''$SEP$#Early Stopping