【问题标题】:IndentationError: Expected an indented block - Python machine learning cat/dogIndentationError: Expected an indented block - Python机器学习猫/狗
【发布时间】:2018-12-03 13:35:02
【问题描述】:

这是我的代码:

test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = 'dog'
else:
prediction = 'cat' `

我有这样的错误:

File "<ipython-input-31-35ebf5fa8bf7>", line 7
prediction = 'dog'
         ^
IndentationError: expected an indented block

谁能帮帮我?

【问题讨论】:

    标签: python machine-learning conv-neural-network


    【解决方案1】:

    您的代码没有正确缩进:

    test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
    test_image = image.img_to_array(test_image)
     test_image = np.expand_dims(test_image, axis = 0)
    result = classifier.predict(test_image)
    training_set.class_indices
    if result[0][0] == 1:
        prediction = 'dog'
    else:
        prediction = 'cat' 
    

    【讨论】:

      【解决方案2】:

      在 Python 中,缩进很重要。 因此,您需要缩进块(例如 if 中的内容):

      if result[0][0] == 1:
        prediction = 'dog'
      #...
      

      【讨论】:

        【解决方案3】:

        python 中的块处理缩进。参考:
        "https://www.python.org/dev/peps/pep-0008/#indentation"

        Your code should be like:  
        if result[0][0] == 1:  
        <4 spaces>prediction = 'dog'  
        else:  
        <4 spaces>prediction = 'cat'  
        

        【讨论】:

          【解决方案4】:

          Python 循环结构(如 if、for 等)需要在 : 之后进行适当的缩进。 在 IDE 中键入 : 后应按 Enter 键,以自动跳转到应添加循环中的下一行代码的位置。如果你没有正确缩进它,python 让它要么在 if 循环之外,要么抛出一个缩进错误。您的代码应如下所示:

          test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg',target_size=(64,64))
          test_image = image.img_to_array(test_image)
          test_image = np.expand_dims(test_image, axis = 0)
          result = classifier.predict(test_image)
          training_set.class_indices
          if result[0][0] == 1:
              prediction = 'dog'
          else:
              prediction = 'cat' 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-02-18
            • 2021-09-15
            • 1970-01-01
            • 1970-01-01
            • 2020-02-20
            • 2017-03-03
            • 2019-05-14
            相关资源
            最近更新 更多