Best Python code snippet using slash
pytorch_logreg_example.py
Source:pytorch_logreg_example.py  
...68        ax.set_xlabel('x');69        ax.set_ylabel('y');70        ax.set_zlabel('z')71        plt.show()72def report_test_error():73    correct = 074    total = 075    with torch.no_grad():76        for data in test_loader:77            samples, labels = data78            outputs = net(samples)79            _, predicted = torch.max(outputs, 1)80            total += labels.size(0)81            correct += (predicted == labels).sum().item()82    print('Accuracy of the network on the 10000 test images: %d %%' % (83            100 * correct / total))84if __name__ == '__main__':85    ################################################################################86    # Build or load data87    ################################################################################88    input_dim = 389    output_dim = 290    X_train, Y_train = build_gaussian_data(1000, 200)91    X_test, Y_test = build_gaussian_data(2000, 2000)92    # specify training and testing datasets93    train_dataset = DataLogReg(X_train, Y_train)94    test_dataset = DataLogReg(X_test, Y_test)95    train_dataset.plot()96    ################################################################################97    # Initialize network class98    ################################################################################99    net = LogRegNet(input_dim, output_dim)100    print(net)101    # inspect network class102    params = list(net.parameters())103    print('Num of params matrices to flag_train:', len(params))104    print(params[0].size())105    sample_input = torch.randn(1, input_dim)     # first dimension is the batch dimension when feeding to nn layers106    features_out = net(sample_input)             # expect size batch_size x 2107    print(features_out.size())108    print(features_out)109    ################################################################################110    # Choose loss111    ################################################################################112    criterion = nn.NLLLoss()113    output = net(sample_input)114    target = torch.tensor([1])      # a dummy target class in [0,1], for example115    loss = criterion(output, target)116    print(loss)117    print(loss.grad_fn)  # MSELoss118    print(loss.grad_fn.next_functions[0][0])  # Linear119    print(loss.grad_fn.next_functions[0][0].next_functions[0][0])  # ReLU120    ################################################################################121    # Optimization122    ################################################################################123    # Training loop hyperparameters124    batch_size = 20125    epochs = 10126    learning_rate = 0.001127    # Choose an optimizer or create one128    import torch.optim as optim129    optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9)130    # Setup data batching131    nwork = 0132    train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=nwork)133    test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=nwork)134    # Set device135    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")136    net.to(device)137    ########inputs, labels = data[0].to(device), data[1].to(device)  # what to send to deive? the Dataset object?138    print(device)139    for epoch in range(epochs):       # loop over the dataset multiple times140        print(epoch)141        running_loss = 0.0142        for i, data in enumerate(train_loader, 0):143            inputs, labels = data  # get the inputs; data is a list of [inputs, labels]144            # zero the parameter gradients145            optimizer.zero_grad()146            # forward + backward + optimize147            outputs = net(inputs)148            loss = criterion(outputs, labels)149            loss.backward()150            optimizer.step()151            # print statistics152            running_loss += loss.item()153            if i % 10 == 9:    # print every 9154                print('Epoch: %d, batch: %5d, loss: %.3f' %155                      (epoch, i + 1, running_loss))156                report_test_error()157                running_loss = 0.0158    print('Finished Training')159    ################################################################################160    # Save model161    ################################################################################162    import os163    model_path = DIR_MODELS + os.sep + 'net_logreg.pth'164    torch.save(net.state_dict(), model_path)165    ################################################################################166    # Load model167    ################################################################################168    dataiter = iter(test_loader)169    samples, labels = dataiter.next()170    net = LogRegNet(input_dim, output_dim)171    net.load_state_dict(torch.load(model_path))172    outputs = net(samples)173    _, predicted = torch.max(outputs, 1)174    print('Predicted: ', ' '.join('%5s' % predicted[j] for j in range(4)))175    print('True: ', ' '.join('%5s' % labels[j] for j in range(4)))...reporter_interface.py
Source:reporter_interface.py  
...26            self.report_test_success(test, result)27        elif result.is_skip():28            self.report_test_skip(test, result)29        elif result.is_error():30            self.report_test_error(test, result)31        elif result.is_interrupted():32            self.report_test_interrupted(test, result)33        else:34            assert result.is_failure()35            self.report_test_failure(test, result)36    def report_test_success(self, test, result):37        pass38    def report_test_skip(self, test, result):39        pass40    def report_test_error(self, test, result):41        pass42    def report_test_failure(self, test, result):43        pass44    def report_test_interrupted(self, test, result):45        pass46    def report_test_error_added(self, test, error):47        pass48    def report_test_failure_added(self, test, error):49        pass50    def report_test_skip_added(self, test, reason):51        pass52    def report_fancy_message(self, headline, message):53        pass54    def report_message(self, message):...test_accuracy.py
Source:test_accuracy.py  
2import numpy as np 3import sample 4import sys5import animation6def report_test_error(savefile_name, action_class, test_inds, pre_step, prune_viewpoints):7	summ = 08	for ind in test_inds:9		ind = int(ind)10		print("Checking error for test_ind ", ind)11		action = sample.extract_action('data/joint_positions', action_class, ind, prune_viewpoints)12		initial_input = sample.generate_initial_input(action)13		pred_result = sample.sample('data/', initial_input, savefile_name, pre_step)14		animation.animate_action(pred_result, action_class, ind,'Prediction of %s'%action_class)15		summ += sample.mean_squared_error(pred_result, action)16	return summ / len(test_inds)17if __name__ == '__main__':18	savefile_name = sys.argv[1]19	action_class = sys.argv[2]20	test_inds = sys.argv[3]21	pre_step = 1022	prune_viewpoints = False23	err = report_test_error(savefile_name, action_class, test_inds.split(','), pre_step, prune_viewpoints)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
