【问题标题】:I am trying to generate a dataset but getting value error "ValueError: 'a' cannot be empty unless no samples are taken "我正在尝试生成数据集,但出现值错误“ValueError:'a' 不能为空,除非没有采样”
【发布时间】:2019-07-11 01:53:06
【问题描述】:

我正在使用包含 3000 多张图像的数据集进行迁移学习。 这是代码的一部分:

import glob
import numpy as np
import os
import shutil

np.random.seed(42)
files = glob.glob('train/*')

cat_files = [fn for fn in files if 'cat' in fn]
dog_files = [fn for fn in files if 'dog' in fn]
len(cat_files), len(dog_files)

cat_train = np.random.choice(cat_files, size=1500, replace=False)

【问题讨论】:

    标签: python-3.x deep-learning transfer-learning


    【解决方案1】:

    是你的文件位置问题。去查看文件位置,应该是这样的 C:...>train>(the image).

    【讨论】:

      【解决方案2】:

      如果没有来自train/ 的一些示例数据,很难准确判断发生了什么,但是从np.random.choice() 的源代码中通过谷歌搜索您的错误消息出现了这一点:

          def choice(self, a, size=None, replace=True, p=None):
      
          ...
      
          Raises
          -------
          ValueError
              If a is an int and less than zero, if a or p are not 1-dimensional,
              if a is an array-like of size 0, if p is not a vector of
              probabilities, if a and p have different lengths, or if
              replace=False and the sample size is greater than the population
              size
      
          ...
      
          # Format and Verify input
          a = np.array(a, copy=False)
          if a.ndim == 0:
              try:
                  # __index__ must return an integer by python rules.
                  pop_size = operator.index(a.item())
              except TypeError:
                  raise ValueError("'a' must be 1-dimensional or an integer")
              if pop_size <= 0 and np.prod(size) != 0:
                  raise ValueError("'a' must be greater than 0 unless no samples are taken")
      

      看来cat_files 可能为空,或者类型不正确。在将其传递给np.random.choice() 之前,您是否验证了其内容?

      【讨论】:

      • 感谢您的回复。数据集 (cat_files) 不为空。
      • 我可以确认这个答案。我在同一代码行 samp = np.random.choice(n_docs, samp_size)n_docs 出现了相同的错误,结果为 0,因为 n_docs = len(docs),文档是一个空系列“形状(0,)”。它是空的,因为我已经大大减少了我的数据集,以至于在我选择的几千行中,任何文档中都没有“抽象”,尽管 preprocess(MY_SERIES, samp_size=samp_size) 期望 non-empty 系列为 MY_SERIES,也就是说,该系列中至少有一个“摘要”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      相关资源
      最近更新 更多