RuntimeError:只能计算浮点类型的平均值。取而代之的是字节。对于平均值 += images_data.mean(2).sum(0)
Posted
技术标签:
【中文标题】RuntimeError:只能计算浮点类型的平均值。取而代之的是字节。对于平均值 += images_data.mean(2).sum(0)【英文标题】:RuntimeError: Can only calculate the mean of floating types. Got Byte instead. for mean += images_data.mean(2).sum(0) 【发布时间】:2021-01-29 03:40:51 【问题描述】:我有以下代码:
# Device configuration
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
seed = 42
np.random.seed(seed)
torch.manual_seed(seed)
# split the dataset into validation and test sets
len_valid_set = int(0.1*len(dataset))
len_train_set = len(dataset) - len_valid_set
print("The length of Train set is ".format(len_train_set))
print("The length of Test set is ".format(len_valid_set))
train_dataset , valid_dataset, = torch.utils.data.random_split(dataset , [len_train_set, len_valid_set])
# shuffle and batch the datasets
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=8, shuffle=True, num_workers=4)
test_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=8, shuffle=True, num_workers=4)
print("LOADERS",
len(dataloader),
len(train_loader),
len(test_loader))
Train set的长度为720
测试集长度为80
装载机 267 90 10
mean = 0.0
std = 0.0
nb_samples = 0.0
for data in train_loader:
images, landmarks = data["image"], data["landmarks"]
batch_samples = images.size(0)
images_data = images.view(batch_samples, images.size(1), -1)
mean += images_data.mean(2).sum(0)
std += images_data.std(2).sum(0)
nb_samples += batch_samples
mean /= nb_samples
std /= nb_samples
我得到这个错误:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-23-9e47ddfeff5e> in <module>
7
8 images_data = images.view(batch_samples, images.size(1), -1)
----> 9 mean += images_data.mean(2).sum(0)
10 std += images_data.std(2).sum(0)
11 nb_samples += batch_samples
RuntimeError: Can only calculate the mean of floating types. Got Byte instead.
固定代码取自 https://***.com/a/64349380/2414957 它适用于 dataloader 但不适用于 train_loader
还有,这些是结果
print(type(images_data))
print(images_data)
我们有:
<class 'torch.Tensor'>
tensor([[[74, 74, 74, ..., 63, 63, 63],
[73, 73, 73, ..., 61, 61, 61],
[75, 75, 75, ..., 61, 61, 61],
...,
[74, 74, 74, ..., 38, 38, 38],
[75, 75, 75, ..., 39, 39, 39],
[72, 72, 72, ..., 38, 38, 38]],
[[75, 75, 75, ..., 65, 65, 65],
[75, 75, 75, ..., 62, 62, 62],
[75, 75, 75, ..., 63, 63, 63],
...,
[71, 71, 71, ..., 39, 39, 39],
[74, 74, 74, ..., 38, 38, 38],
[73, 73, 73, ..., 37, 37, 37]],
[[72, 72, 72, ..., 62, 62, 62],
[74, 74, 74, ..., 63, 63, 63],
[75, 75, 75, ..., 61, 61, 61],
...,
[74, 74, 74, ..., 38, 38, 38],
[74, 74, 74, ..., 39, 39, 39],
[73, 73, 73, ..., 37, 37, 37]],
...,
[[75, 75, 75, ..., 63, 63, 63],
[73, 73, 73, ..., 63, 63, 63],
[74, 74, 74, ..., 62, 62, 62],
...,
[74, 74, 74, ..., 38, 38, 38],
[73, 73, 73, ..., 39, 39, 39],
[73, 73, 73, ..., 37, 37, 37]],
[[73, 73, 73, ..., 62, 62, 62],
[75, 75, 75, ..., 62, 62, 62],
[74, 74, 74, ..., 63, 63, 63],
...,
[73, 73, 73, ..., 39, 39, 39],
[74, 74, 74, ..., 38, 38, 38],
[74, 74, 74, ..., 38, 38, 38]],
[[74, 74, 74, ..., 62, 62, 62],
[74, 74, 74, ..., 63, 63, 63],
[74, 74, 74, ..., 62, 62, 62],
...,
[74, 74, 74, ..., 38, 38, 38],
[73, 73, 73, ..., 38, 38, 38],
[72, 72, 72, ..., 36, 36, 36]]], dtype=torch.uint8)
当我尝试时
images_data = images_data.float()
mean += images_data.mean(2).sum(0)
我没有像我预期的那样得到 3 个均值和 3 个标准值的张量,但得到了一个非常大的张量(每个 torch.Size([600]))
【问题讨论】:
【参考方案1】:正如错误所说,您的 images_data
是一个 ByteTensor,即具有 dtype uint8
。 Torch 拒绝计算整数的平均值。您可以使用以下命令将数据转换为float
:
(images_data * 1.0).mean(2)
或者
torch.Tensor.float(images_data).mean(2)
【讨论】:
它的工作原理与我使用的 float 类似,并为我提供了一个 600 个值的张量,而不是 3 个元素的张量以上是关于RuntimeError:只能计算浮点类型的平均值。取而代之的是字节。对于平均值 += images_data.mean(2).sum(0)的主要内容,如果未能解决你的问题,请参考以下文章
如何使用Scala计算Spark中数据框中列的开始索引和结束索引之间的行的平均值?