学习利用torch进行逻辑回归
一、导入模块
import torch
import torch.nn.functional as F
二、参数值设置
n_item=1000
n_feature=2
learning_rate=0.001
epochs=100
三、建立变量
torch.manual_seed(123)
data_x=torch.randn(size=(n_item,n_feature)).float()
data_y=torch.where(torch.subtract(data_x[:,0]*0.5,data_x[:,1]*1.5) > 0, 1., 0.).float()
四、建立逻辑回归类
class LogisticRegressionManually(object):
def __init__(self):
self.w=torch.randn(size=(n_feature,1), requires_grad=True)
self.b=torch.zeros(size=(1,1), requires_grad=True)
def forward(self,x):
y_hat=F.sigmoid(torch.matmul(self.w.transpose(0,1),x) +self.b)
return y_hat
@staticmethod
def loss_func(y_hat,y):
return -(torch.log(y_hat)*y + (1-y)*torch.log(1 - y_hat))
def train(self):
for epoch in range(epochs):
for step in range(n_item):
y_hat = self.forward(data_x[step])
y = data_y[step]
loss = self.loss_func(y_hat, y)
loss.backward()
with torch.no_grad():
self.w.data -= learning_rate * self.w.grad.data
self.b.data -= learning_rate*self.b.grad.data
self.w.grad.data.zero_()
self.b.grad.data.zero_()
print('EPOCH:%03d,Loss:%.3f' % (epoch, loss.item()))
五、运行
建立实例
if __name__=="__main__":
lrm= LogisticRegressionManually()
lrm.train()
六、结果
EPOCH:000,Loss:0.562
EPOCH:001,Loss:0.548
EPOCH:002,Loss:0.536
EPOCH:003,Loss:0.525
EPOCH:004,Loss:0.516
EPOCH:005,Loss:0.508
EPOCH:006,Loss:0.500
EPOCH:007,Loss:0.493
EPOCH:008,Loss:0.487
EPOCH:009,Loss:0.482
EPOCH:010,Loss:0.476
EPOCH:011,Loss:0.471
EPOCH:012,Loss:0.467
EPOCH:013,Loss:0.462
EPOCH:014,Loss:0.458
EPOCH:015,Loss:0.454
EPOCH:016,Loss:0.450
EPOCH:017,Loss:0.446
EPOCH:018,Loss:0.443
EPOCH:019,Loss:0.440
EPOCH:020,Loss:0.436
EPOCH:021,Loss:0.433
EPOCH:022,Loss:0.430
EPOCH:023,Loss:0.427
EPOCH:024,Loss:0.424
EPOCH:025,Loss:0.422
EPOCH:026,Loss:0.419
EPOCH:027,Loss:0.416
EPOCH:028,Loss:0.414
EPOCH:029,Loss:0.411
EPOCH:030,Loss:0.409
EPOCH:031,Loss:0.407
EPOCH:032,Loss:0.404
EPOCH:033,Loss:0.402
EPOCH:034,Loss:0.400
EPOCH:035,Loss:0.398
EPOCH:036,Loss:0.396
EPOCH:037,Loss:0.393
EPOCH:038,Loss:0.391
EPOCH:039,Loss:0.389
EPOCH:040,Loss:0.388
EPOCH:041,Loss:0.386
EPOCH:042,Loss:0.384
EPOCH:043,Loss:0.382
EPOCH:044,Loss:0.380
EPOCH:045,Loss:0.378
EPOCH:046,Loss:0.377
EPOCH:047,Loss:0.375
EPOCH:048,Loss:0.373
EPOCH:049,Loss:0.372
EPOCH:050,Loss:0.370
EPOCH:051,Loss:0.368
EPOCH:052,Loss:0.367
EPOCH:053,Loss:0.365
EPOCH:054,Loss:0.364
EPOCH:055,Loss:0.362
EPOCH:056,Loss:0.361
EPOCH:057,Loss:0.359
EPOCH:058,Loss:0.358
EPOCH:059,Loss:0.356
EPOCH:060,Loss:0.355
EPOCH:061,Loss:0.353
EPOCH:062,Loss:0.352
EPOCH:063,Loss:0.351
EPOCH:064,Loss:0.349
EPOCH:065,Loss:0.348
EPOCH:066,Loss:0.347
EPOCH:067,Loss:0.345
EPOCH:068,Loss:0.344
EPOCH:069,Loss:0.343
EPOCH:070,Loss:0.342
EPOCH:071,Loss:0.340
EPOCH:072,Loss:0.339
EPOCH:073,Loss:0.338
EPOCH:074,Loss:0.337
EPOCH:075,Loss:0.336
EPOCH:076,Loss:0.335
EPOCH:077,Loss:0.333
EPOCH:078,Loss:0.332
EPOCH:079,Loss:0.331
EPOCH:080,Loss:0.330
EPOCH:081,Loss:0.329
EPOCH:082,Loss:0.328
EPOCH:083,Loss:0.327
EPOCH:084,Loss:0.326
EPOCH:085,Loss:0.325
EPOCH:086,Loss:0.324
EPOCH:087,Loss:0.323
EPOCH:088,Loss:0.322
EPOCH:089,Loss:0.321
EPOCH:090,Loss:0.320
EPOCH:091,Loss:0.319
EPOCH:092,Loss:0.318
EPOCH:093,Loss:0.317
EPOCH:094,Loss:0.316
EPOCH:095,Loss:0.315
EPOCH:096,Loss:0.314
EPOCH:097,Loss:0.313
EPOCH:098,Loss:0.312
EPOCH:099,Loss:0.311
Process finished with exit code 0