本文整理匯總了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