當前位置: 首頁>>代碼示例>>Python>>正文


Python crnn.zero_grad方法代碼示例

本文整理匯總了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 
開發者ID:zzzDavid,項目名稱:ICDAR-2019-SROIE,代碼行數:18,代碼來源:train.py

示例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 
開發者ID:rahzaazhar,項目名稱:PAN-PSEnet,代碼行數:20,代碼來源:train.py

示例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 
開發者ID:hwwu,項目名稱:ctpn-crnn,代碼行數:21,代碼來源:crnn_main.py

示例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 
開發者ID:liuhu-bigeye,項目名稱:enctc.crnn,代碼行數:26,代碼來源:crnn_main_seg.py


注:本文中的models.crnn.zero_grad方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。