![](/img/trans.png)
[英]Meaning of "IndentationError: expected an indented block" exception?
[英]IndentationError: expected an indented block by using exception
我的代碼是:我試圖通過使用異常來探索錯誤,我發現了以下
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
function 時向我展示了錯誤來自本節。
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 語句沒有正確縮進
代碼:(修復了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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.