【问题标题】:TypeError ..... got multiple values for argument when trying to load_mnist(...) functionTypeError .....尝试加载_mnist(...)函数时获得了多个参数值
【发布时间】:2020-02-10 01:00:55
【问题描述】:

请协助解决如下所示的错误

def load_mnist(path, kind='train'):
    """Load MNIST data from `path`"""
    labels_path = os.path.join(path, 
                               '%s-labels-idx1-ubyte' % kind)
    images_path = os.path.join(path, 
                               '%s-images-idx3-ubyte' % kind)

    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II', 
                                 lbpath.read(8))
        labels = np.fromfile(lbpath, 
                             dtype=np.uint8)

    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack(">IIII", 
                                               imgpath.read(16))
        images = np.fromfile(imgpath, 
                             dtype=np.uint8).reshape(len(labels), 784)
        images = ((images / 255.) - .5) * 2

    return images, labels

然后:

X_train, y_train = load_mnist('.\\Chapter 12\\MNIST', 'labels-idx1-ubyte', kind='train')

错误信息是:

TypeError: load_mnist() 为参数 'kind' 获得了多个值

目录内容为:

t10k-images.idx3-ubyte

t10k-labels.idx1-ubyte

train-images.idx3-ubyte

train-labels.idx1-ubyte

【问题讨论】:

  • 您的 load_mnist 接受两个参数,但您将三个参数传递给它。你期望它做什么?据我所知,您不想将'labels-idx1-ubyte' 传递给函数...

标签: python function typeerror


【解决方案1】:

错误是 load_mnist 接受两个参数,但您确实给 load_mnist 三个参数(就像 @uneven_mark 所说)

让我更正你的代码

X_train, Y_train = load_mnist('.\\Chapter 12\\MNIST', kind='train')

load_mnist函数没有错误,错误在变量赋值中

【讨论】:

  • 我在使用推荐代码时遇到了一个新问题,''' File "", line 1, in X_train, y_train = load_mnist(' .\\Chapter 12\\MNIST', kind='train') File "", line 12, in load_mnist with open(labels_path, 'rb') as lbpath: FileNotFoundError: [Errno 2]没有这样的文件或目录:'.\\Chapter 12\\MNIST\\train-labels-idx1-ubyte''''
  • 提出一个新问题
猜你喜欢
  • 2019-07-05
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
  • 2019-04-11
  • 2021-12-18
  • 2018-07-28
  • 2022-08-22
相关资源
最近更新 更多