#classify number of tuple import torch import numpy as np import matplotlib.pyplot as plt from torch.autograd import Variable import torch.optim as optim ECHOS=10000 #Train_dataset train_x_1,train_x_2,train_x_3=torch.rand(100,100),torch.rand(100,100),torch.rand(100,100) train_x_1,train_x_2,train_x_3=torch.clamp(train_x_1,0,0.3),torch.clamp(train_x_2,0,0.4),torch.clamp(train_x_3,0,0.6) train_x=torch.cat((train_x_1,train_x_2,train_x_3)) #Test_dataset test_1,test_2,test_3=torch.rand(1,100),torch.rand(1,100),torch.rand(1,100) test_1,test_2,test_3=torch.clamp(test_1,0,0.305),torch.clamp(test_2,0.3,0.407),torch.clamp(test_3,0,0.6) test=torch.cat((test_1,test_2,test_3)) #Label label_1,label_2,label_3=torch.zeros(100).type(torch.LongTensor),torch.ones(100).type(torch.LongTensor),torch.ones(100).type(torch.LongTensor)+1 label=torch.cat((label_1,label_2,label_3)) print(label.size()) #Build Nerual NEtwork model=torch.nn.Sequential( torch.nn.Linear(100,30), torch.nn.Linear(30,3), torch.nn.Softmax() ) #optimizer..... optimizer=optim.SGD(model.parameters(),lr=0.05) criterion=torch.nn.CrossEntropyLoss() train_x=Variable(train_x) label=Variable(label) for echo in range(ECHOS): predict_value=model(train_x) loss=criterion(predict_value,label) optimizer.zero_grad() loss.backward() optimizer.step() print('Loss:{}'.format(loss)) model.eval() test=Variable(test) test_result=model(test) print(test_result)
PyTorch数字分类问题2
最新推荐文章于 2024-07-20 10:49:06 发布