报错记录:dataset与dataloader有关

一、记录报错,在写dataset与dataloader时遇到的问题

卡了我好久。。。知道了答案豁然开朗,就第一次写下了博客去记录(同时谢谢善良的网友)

ValueError: num_samples should be a positive integer value, but got num_samples=0
这个错误是由于传递给一个函数的num_samples参数的值为0而引起的。

百度查到的几个解决办法分别为:
1、修改shuffle参数。
当dataloader设置了batch_sampler和sampler时,不需要shuffle来进行随机,所以将shuffle设置为FALSE即可。
ps:dataloader的参数:

  • dataset:加载的数据集。
  • batch_size:每个batch包含的数据数量 shuffle:是否打乱数据位置
  • sampler:自定义从数据集中采样的策略,如果指定了,则shuffle必须为False
  • batch_sampler:和sampler一样,与shuffle互斥
  • num_workers:使用线程的数量,当为0时数据直接加载到主程序,默认为0
  • drop_last:dataset中的数据个数可能不是batch_size的整数倍,为True会将多出来不足一个的batch的数据丢弃。

2、len返回的为0,DataLoader读出来后,就报了 got num_samples=0,将这个修改后就正常运行了。

我的错误和第二个原因一样。
因为我在写dataset类的init初始化时,将

self.patch=()
self.mos=[]

然后在getitem中再给self.patch和self.mos赋值,这样读到的len就是0.
总之,在dataset类的初始化中不可以设置为空,一定要给数据长度,否则报错!!!

神奇的是,在我找不到错的时候,碰巧在主程序里写了一句print(dataset[0])后才测试

    for data in train_loader:
        imgs,targets = data
        print(imgs.shape)
        print(targets.shape)

有这句print就可以进行,没这句print就报错。原因是执行了print后调用train_loader=DataLoader()函数,此时self.patch和self.mos就有了值,所以才不会报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 大意是:值误:num_samples应该是一个正整数,但是输入的值不是正整数。 这个误通常出现在使用机器学习模型时,num_samples是指训练数据集中的样本数量,应该是一个正整数。如果输入的值不是正整数,就会出现这个误。解决方法是检查输入的值是否正确,并确保它是一个正整数。 ### 回答2: valueerror: num_samples should是一个Python中的误提示,意为“样本数应该为……”。这个误通常出现在使用机器学习相关的库时,表示样本数的值不符合要求。 在机器学习中,样本数是指用于训练分类器或回归器的数据样本的数量。不同的机器学习算法对样本数的要求不同,但通常都要求样本数足够多、具有代表性和均衡性。 例如,在使用Scikit-Learn库进行机器学习实验时,如果将数据集(比如说一个.csv文件)导入后,却出现了valueerror: num_samples should这样的误提示,那么通常是因为样本数不符合要求。 这个误提示中的num_samples应该是一个整数,表示数据集中包含的样本数。这个值应该大于0,且不应该超过数据集本身的大小。如果样本数小于算法所需,则可能导致过拟合和欠拟合。如果样本数过多,则可能会增加计算时间和内存占用量,同时也可能导致算法的不可解或不稳定。 修复这个误可以通过增加数据集的样本量、删除无效或重复的样本、使用噪声过滤技术对数据进行清洗等方法。此外,也可以通过调整机器学习算法的超参数来适应不同的数据集规模和特征分布。 ### 回答3: ValueError: num_samples should be a positive integer value. This error typically occurs when you are working with Python libraries that require you to specify the number of samples in your data set, such as when you are training a machine learning model or performing statistical analysis. The most common cause of this error is a mistake in specifying the number of samples in your data set. You may have accidentally entered a negative number, a non-integer value, or left the value blank altogether. Alternatively, the error may be due to a bug in the Python library you are working with. To resolve this error, you will need to double-check your code and ensure that you have correctly specified the number of samples in your data set. You can also try updating the library you are using or checking the documentation for any known issues related to this error. If you are still having difficulty resolving this error, you may want to seek help from a more experienced Python developer or consult online forums and communities where others have encountered similar issues. With the right approach and a bit of perseverance, you can overcome this error and continue working with your Python code.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值