【问题标题】:python indentation error (tensorflow)python缩进错误(张量流)
【发布时间】:2018-05-23 01:50:34
【问题描述】:

我所有的代码都是在 Ubuntu 中用 sublime text 编写的

我目前正在学习机器学习,并且一直在关注视频... 我的代码与 Youtuber 的代码相同,但我一直得到一个

    File "deep-net.py", line 31
    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    IndentationError: unindent does not match any outer indentation level

此错误消息在第 31 行末尾有一个小插入符号指向最后的结束“}”。
这是我的一段上下文代码:

def neural_network_model(data):

    # (input_data + weights) + biases

    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])),'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}        #this is line 31
    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),'biases':tf.Variable(tf.random_normal([n_classes]))}

    #blah blah more code
    return output

最初,我没有将所有代码放在一行,因为我不喜欢爬出屏幕的代码。这是我原来的缩进:

def neural_network_model(data):

    # (input_data + weights) + biases

    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes]))}

我得到了同样的错误,除了错误在第 35 行:

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),

插入符号指向结尾的“,”

我决定将每个变量定义放在一行中,而不是将其分成两行,看看缩进错误是否会消失,但它没有。

任何帮助将不胜感激。我是一名学生,大部分时间都在编写 Java 代码,所以我现在还不习惯处理缩进问题。
谢谢

【问题讨论】:

    标签: python tensorflow indentation


    【解决方案1】:

    确保您仅使用空格或制表符进行缩进。 Sublime text has a "convert tabs to spaces" 对此有用的功能。

    【讨论】:

    • 不幸的是,我只能在非官方文档中找到它,但我也使用设置 "draw_white_space": "all", 来显示空白字符(空格显示为点,制表符显示为实线),这有助于知道何时发生这种情况:docs.sublimetext.info/en/latest/reference/…
    猜你喜欢
    • 2018-06-21
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2017-02-11
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多