本文整理汇总了Python中models.crnn.zero_grad方法的典型用法代码示例。如果您正苦于以下问题:Python crnn.zero_grad方法的具体用法?Python crnn.zero_grad怎么用?Python crnn.zero_grad使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.crnn
的用法示例。
在下文中一共展示了crnn.zero_grad方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: trainBatch
# 需要导入模块: from models import crnn [as 别名]
# 或者: from models.crnn import zero_grad [as 别名]
def trainBatch(net, criterion, optimizer):
data = train_iter.next()
cpu_images, cpu_texts = data
batch_size = cpu_images.size(0)
utils.loadData(image, cpu_images)
t, l = converter.encode(cpu_texts)
utils.loadData(text, t)
utils.loadData(length, l)
preds = crnn(image)
preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
cost = criterion(preds, text, preds_size, length) / batch_size
crnn.zero_grad()
cost.backward()
optimizer.step()
return cost
示例2: trainBatch
# 需要导入模块: from models import crnn [as 别名]
# 或者: from models.crnn import zero_grad [as 别名]
def trainBatch(net, criterion, optimizer):
data = train_iter.next()
cpu_images, cpu_texts = data
batch_size = cpu_images.size(0)
utils.loadData(image, cpu_images)
t, l = converter.encode(cpu_texts)
utils.loadData(text, t)
utils.loadData(length, l)
optimizer.zero_grad()
preds = crnn(image)
preds = preds.log_softmax(2)
preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
cost = criterion(preds, text, preds_size, length)
# crnn.zero_grad()
cost.backward()
optimizer.step()
return cost
示例3: trainBatch
# 需要导入模块: from models import crnn [as 别名]
# 或者: from models.crnn import zero_grad [as 别名]
def trainBatch(net, criterion, optimizer, train_iter):
data = train_iter.next()
cpu_images, cpu_texts = data
# print('----cpu_images-----')
# print(cpu_images.shape)
batch_size = cpu_images.size(0)
utils.loadData(image, cpu_images)
t, l = converter.encode(cpu_texts)
utils.loadData(text, t)
utils.loadData(length, l)
# print('----image-----')
# print(image.shape)
preds = crnn(image)
preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
cost = criterion(preds, text, preds_size, length) / batch_size
crnn.zero_grad()
cost.backward()
optimizer.step()
return cost
示例4: trainBatch
# 需要导入模块: from models import crnn [as 别名]
# 或者: from models.crnn import zero_grad [as 别名]
def trainBatch(net, optimizer):
data = train_iter.next()
cpu_images, cpu_texts = data
batch_size = cpu_images.size(0)
utils.loadData(image, cpu_images)
t, l = converter.encode(cpu_texts)
utils.loadData(text, t)
utils.loadData(length, l)
preds = crnn(image)
preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
H, cost = seg_ctc_ent_cost(preds, text, preds_size, length, uni_rate=opt.uni_rate)
h_cost = (1-opt.h_rate)*cost-opt.h_rate*H
cost_sum = h_cost.data.sum()
inf = float("inf")
if cost_sum == inf or cost_sum == -inf or cost_sum > 200*batch_size:
print("Warning: received an inf loss, setting loss value to 0")
return torch.zeros(H.size()), torch.zeros(cost.size()), torch.zeros(h_cost.size())
crnn.zero_grad()
h_cost.backward()
torch.nn.utils.clip_grad_norm(crnn.parameters(), opt.max_norm)
optimizer.step()
return H / batch_size, cost / batch_size, h_cost / batch_size