PyTorch数字分类问题2

#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)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值