【问题标题】:Indent Error when there appears to be none似乎没有缩进错误
【发布时间】:2019-04-21 05:37:41
【问题描述】:

您我一直在尝试运行此脚本,但最后总是出现缩进错误 backprop(x,y) 函数。我真的很感激任何帮助!

import cPickle
import gzip

def load_data():

    f = gzip.open('mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = cPickle.load(f)
    f.close()
    return (training_data, validation_data, test_data)

import numpy as np

class Network(object):
    def __init__(self, layers):
        self.layers = layers
        self.biases = [np.random.randn(y,1) for y
                        in layers[1:]]
        self.weights = [np.transpose(np.random.randn(x,y))
                        for x,y
                        in zip(layers[:-1],layers[1:])]
        self.num_layers = len(layers)




    def backprop(self, x, y):
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        # feedforward
        activation = x
        activations = [x] # list to store all the activations, layer by layer
        zs = [] # list to store all the z vectors, layer by layer
        for b, w in zip(self.biases, self.weights):
            z = np.dot(w, activation)+b
            zs.append(z)
            activation = sigmoid(z)
            activations.append(activation)
        # backward pass
        delta = self.cost_derivative(activations[-1], y) * \
            sigmoid_prime(zs[-1])#set first delta 
        nabla_b[-1] = delta#set last dC/db to delta vector 
        nabla_w[-1] = np.dot(delta, activations[-2].transpose())
        #calculate nabla_b, nabla_w for the rest of the layers
        for l in xrange(2, self.num_layers):
            z = zs[-l]
            sp = sigmoid_prime(z)   
            delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
            nabla_b[-l] = delta
            nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
        #this is where python says there is an indent error!
        return (nabla_b, nabla_w)

【问题讨论】:

  • 我已经选择了您的所有代码并在编辑器中按下了{} 按钮。这会将所选文本格式化为代码。请查看问题以确保它看起来像您的原始代码。您可以随时 edit 解决或改进您的问题。
  • 尝试用spaces替换tab
  • @CrisLuengo 感谢您帮助我格式化代码;我对此有点麻烦......但我还是发布了它,哈哈。再次感谢哥们!
  • @HaBom 解决了问题!非常感谢!!

标签: python-2.7 image-processing neural-network


【解决方案1】:

通过选择Notepad++的“编辑”下拉菜单,选择空白操作,最后点击“TAB到空格”解决了这个问题;显然,这应该在选择触发错误的代码部分之后完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2023-04-02
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2019-05-14
    相关资源
    最近更新 更多