Python 缩进错误:使用异常时应为缩进块

Python 缩进错误:使用异常时应为缩进块,python,pandas,indentation,Python,Pandas,Indentation,我的代码是:我试图通过使用异常来探索错误,我发现了以下内容 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)

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

    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))
试试看:
而((训练时间<最大时间)和需要学习):
列车批次=列车iter.next()
x、 t=混凝土示例(系列批次)
#打印(t)
y=型号(x)
损失=F.均方误差(y,t)
model.cleargrads()
loss.backward()
optimizer.update()
如果列车是新纪元:
打印(“epoch”,train_iter.epoch,“loss=,loss.data,end=”)
损失附加(火车时代)
丢失附加(丢失数据)
除ValueError为e外:
引发异常('无效的json:{}'。格式(e))
try语句后的while语句没有正确缩进

try:
而((训练时间<最大时间)和需要学习):
列车批次=列车iter.next()
x、 t=混凝土示例(系列批次)
#打印(t)
y=型号(x)
损失=F.均方误差(y,t)
model.cleargrads()
loss.backward()
optimizer.update()
如果列车是新纪元:
打印(“epoch”,train_iter.epoch,“loss=,loss.data,end=”)
损失附加(火车时代)
丢失附加(丢失数据)
除ValueError为e外:
引发异常('无效的json:{}'。格式(e))
try语句后的while语句没有正确缩进代码:(修复了try:…的缩进,除了:…块)

试试看:
而((训练时间<最大时间)和需要学习):
列车批次=列车iter.next()
x、 t=混凝土示例(系列批次)
#打印(t)
y=型号(x)
损失=F.均方误差(y,t)
model.cleargrads()
loss.backward()
optimizer.update()
如果列车是新纪元:
打印(“epoch”,train_iter.epoch,“loss=,loss.data,end=”)
损失附加(火车时代)
丢失附加(丢失数据)
异常除外,如e:#替换为异常以处理其他错误
引发异常('无效的json:{}'。格式(e))
尽管如此:
test\u batch=test\u iter.next()
x_测试,t_测试=浓缩样本(测试批次)
y_检验=模型(x_检验)
损失检验=F.均方误差(y检验,t检验)
如果测试是新的时代:
测试时间=0
测试仪器当前位置=0
测试结果是否为新纪元=错误
测试试验机。按下位置=无
打破
打印(“test_loss=”,loss_test.data)
loss_Y_test.append(loss_test.data)
研究损失=损失测试数据
如果研究丢失<研究保留:
需要学习=错误
打印(“损失小于阈值”)
代码:(修复了
try:…的缩进,除了:…
块)

试试看:
而((训练时间<最大时间)和需要学习):
列车批次=列车iter.next()
x、 t=混凝土示例(系列批次)
#打印(t)
y=型号(x)
损失=F.均方误差(y,t)
model.cleargrads()
loss.backward()
optimizer.update()
如果列车是新纪元:
打印(“epoch”,train_iter.epoch,“loss=,loss.data,end=”)
损失附加(火车时代)
丢失附加(丢失数据)
异常除外,如e:#替换为异常以处理其他错误
引发异常('无效的json:{}'。格式(e))
尽管如此:
test\u batch=test\u iter.next()
x_测试,t_测试=浓缩样本(测试批次)
y_检验=模型(x_检验)
损失检验=F.均方误差(y检验,t检验)
如果测试是新的时代:
测试时间=0
测试仪器当前位置=0
测试结果是否为新纪元=错误
测试试验机。按下位置=无
打破
打印(“test_loss=”,loss_test.data)
loss_Y_test.append(loss_test.data)
研究损失=损失测试数据
如果研究丢失<研究保留:
需要学习=错误
打印(“损失小于阈值”)

You
try:
block缩进错误。@shaikmoeed如何更正?是否要处理except block中的错误?@shaikmoeed是的,没错,tqYou
try:
block缩进错误。@shaikmoeed如何更正?是否要处理except block中的错误?@shaikmoeed是的,没错,tqnow我正面临此错误KeyError:10534在处理上述异常期间,发生了另一个异常:@aboomair添加代码的详细信息并完成对问题的回溯。您可以看到,已完成的代码在上面,错误位于
try
函数的同一区域a,我得到了此
文件“”,第17行为True时:^IndentationError:意外缩进
现在我在处理上述异常时遇到此错误KeyError:10534,发生了另一个异常:@aboomair添加代码的详细信息并完成对问题的回溯。您可以看到,已完成的代码在上面,错误位于
try
函数的同一区域,我得到了此
文件“”,第17行,而True:^indicationError:意外缩进
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")
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)
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((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")