IndentationError:通过使用异常期望缩进的块

问题描述 投票:1回答:2

我的代码是:我试图通过使用异常来探索错误,并且发现了以下内容

    try:
while((train_iter.epoch < max_epoch) and needStudy):
    train_batch = train_iter.next()
    x, t = concat_examples(train_batch)
    #print(t)    
    y = model(x)    
    loss = F.mean_squared_error(y, t)
    model.cleargrads()
    loss.backward()
    optimizer.update()
    if train_iter.is_new_epoch:
        print("epoch", train_iter.epoch, "loss=", loss.data, end=" ")
        loss_X.append(train_iter.epoch)
        loss_Y.append(loss.data)
except ValueError as e:
        raise Exception('Invalid json: {}'.format(e))

而且我面临的错误是:

File "<ipython-input-393-be26bc8a85ab>", line 2
    while((train_iter.epoch < max_epoch) and needStudy):
        ^
IndentationError: expected an indented block

任何想法?

以下是完成的代码:

try:
    while((train_iter.epoch < max_epoch) and needStudy):
        train_batch = train_iter.next()
        x, t = concat_examples(train_batch)
        #print(t)    
        y = model(x)    
        loss = F.mean_squared_error(y, t)
        model.cleargrads()
        loss.backward()
        optimizer.update()
        if train_iter.is_new_epoch:
            print("epoch", train_iter.epoch, "loss=", loss.data, end=" ")
            loss_X.append(train_iter.epoch)
            loss_Y.append(loss.data)
except ValueError as e:
        raise Exception('Invalid json: {}'.format(e))        
        while True:
            test_batch = test_iter.next()
            x_test, t_test = concat_examples(test_batch)
            y_test = model(x_test)
            loss_test = F.mean_squared_error(y_test, t_test)
            if test_iter.is_new_epoch:
                test_iter.epoch = 0
                test_iter.current_position = 0
                test_iter.is_new_epoch = False
                test_iter._pushed_position = None
                break
        print("test_loss=", loss_test.data)
        loss_Y_test.append(loss_test.data)
        study_loss = loss_test.data
        if study_loss < studyThreshold:
            needStudy = False
            print("loss is less than threshold value")

和显示我的错误是当我使用try函数向我显示错误时来自此部分。

KeyError: 10534

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-397-31208bd43353> in <module>
      1 try:
      2     while((train_iter.epoch < max_epoch) and needStudy):
----> 3         train_batch = train_iter.next()
      4         x, t = concat_examples(train_batch)
      5         #print(t)
python pandas indentation
2个回答
0
投票

代码:(已修复try:... except:...块的缩进)

try:
    while((train_iter.epoch < max_epoch) and needStudy):
        train_batch = train_iter.next()
        x, t = concat_examples(train_batch)
        #print(t)    
        y = model(x)    
        loss = F.mean_squared_error(y, t)
        model.cleargrads()
        loss.backward()
        optimizer.update()
        if train_iter.is_new_epoch:
            print("epoch", train_iter.epoch, "loss=", loss.data, end=" ")
            loss_X.append(train_iter.epoch)
            loss_Y.append(loss.data)
except Exception as e: # replaced with Exception to handle other errors
    raise Exception('Invalid json: {}'.format(e))
    while True:
        test_batch = test_iter.next()
        x_test, t_test = concat_examples(test_batch)
        y_test = model(x_test)
        loss_test = F.mean_squared_error(y_test, t_test)
        if test_iter.is_new_epoch:
            test_iter.epoch = 0
            test_iter.current_position = 0
            test_iter.is_new_epoch = False
            test_iter._pushed_position = None
            break
    print("test_loss=", loss_test.data)
    loss_Y_test.append(loss_test.data)
    study_loss = loss_test.data
    if study_loss < studyThreshold:
        needStudy = False
        print("loss is less than threshold value")

0
投票
try:
    while((train_iter.epoch < max_epoch) and needStudy):
        train_batch = train_iter.next()
        x, t = concat_examples(train_batch)
        #print(t)    
        y = model(x)    
        loss = F.mean_squared_error(y, t)
        model.cleargrads()
        loss.backward()
        optimizer.update()
        if train_iter.is_new_epoch:
            print("epoch", train_iter.epoch, "loss=", loss.data, end=" ")
            loss_X.append(train_iter.epoch)
            loss_Y.append(loss.data)
except ValueError as e:
    raise Exception('Invalid json: {}'.format(e))

try语句后的while语句未正确缩进

© www.soinside.com 2019 - 2024. All rights reserved.