input = torch.randn(4, 4)
torch.mean(a, 0) 等同于 input.mean(0)
方法参考:torch.mean(input, dim, keepdim=False, *, dtype=None, out=None) → Tensor
Parameters:
dim retained or not.作用:在指定的dim上,对每一行(列、纵)取均值。
Returns the mean value of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them.
比如如下数组(C,H,W)为[2,3,4],在C维度上,求每纵(Channel)的均值。
import torch
x = torch.arange(24).view(2, 3, 4).float()
y = x.mean(0)
print("x.shape:", x.shape)
print("x:")
print(x)
print("y.shape:", y.shape)
print("y:")
print(y)
输出结果:
比如如下数组(C,H,W)为[2,3,4],在H维度上,求每列(Height)的均值。
代码:
import torch
x = torch.arange(24).view(2, 3, 4).float()
y = x.mean(1)
print("x.shape:", x.shape)
print("x:")
print(x)
print("y.shape:", y.shape)
print("y:")
print(y)
输出:
比如如下数组(C,H,W)为[2,3,4],在W维度上,求每行(Width)的均值。
代码:
import torch
x = torch.arange(24).view(2, 3, 4).float()
y = x.mean(2)
print("x.shape:", x.shape)
print("x:")
print(x)
print("y.shape:", y.shape)
print("y:")
print(y)
输出:

代码例子:
import torch
x = torch.arange(24).view(2, 3, 4).float()
y = x.mean([0,1])
print("x.shape:", x.shape)
print("x:")
print(x)
print("y.shape:", y.shape)
print("y:")
print(y)
结果输出:可以理解成先做dim=1,然后再做dim=0;
y = x.mean(-3) 等同于 y = x.mean(0)
y = x.mean(-2) 等同于 y = x.mean(1)
y = x.mean(-1) 等同于 y = x.mean(2)

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).
从英语字面上:输出的tensor和输入的tensor,size一样,但是指定的dim值为1。
看例子:y = x.mean(0, keepdim = True)
import torch
x = torch.arange(24).view(2, 3, 4).float()
y = x.mean(0,keepdim = True)
print("x.shape:", x.shape)
print("x:")
print(x)
print("y.shape:", y.shape)
print("y:")
print(y)
结果输出:如图所示,第0维度被保留,但置为1。

方法参考:torch.sum(input, dim, keepdim=False, *, dtype=None) → Tensor
Parameters:
dim retained or not.作用:在指定的dim上,对每一行(列、纵)求和。
Returns the sum of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them.
代码例子:
import torch
x = torch.arange(2* 3 * 4).view(2, 3, 4)
y = x.sum((1,0))
print("x.shape: ",x.shape)
print("x:")
print(x)
print("y.shape: ",y.shape)
print("y:")
print(y)
结果输出:
代码例子:y = x.sum((0,1)),其中参数必须为元组(0,1),否则报错。[0,0],序列也行。
import torch
x = torch.arange(2* 3 * 4).view(2, 3, 4)
y = x.sum((0,1))
print("x.shape: ",x.shape)
print("x:")
print(x)
print("y.shape: ",y.shape)
print("y:")
print(y)
x.shape: torch.Size([2, 3, 4])
x:
tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
y.shape: torch.Size([4])
y:
tensor([60, 66, 72, 78])
结果输出:

代码例子:tensor.mean(2).sum(0)
import torch
import numpy as np
import torchvision.transforms as transforms
# 这里以上述创建的单数据为例子
data = np.array([
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]],
[[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]],
[[3, 3, 3], [3, 3, 3], [3, 3, 3], [3, 3, 3], [3, 3, 3]],
[[4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4]],
[[5, 5, 5], [5, 5, 5], [5, 5, 5], [5, 5, 5], [5, 5, 5]]
], dtype='uint8')
# 将数据转为C,W,H,并归一化到[0,1]
data = transforms.ToTensor()(data)
# 需要对数据进行扩维,增加batch维度
data = torch.unsqueeze(data, 0) # tensor([1, 3, 5, 5])
nb_samples = 0.
# 创建3维的空列表
channel_mean = torch.zeros(3) # tensor([0., 0., 0.])
channel_std = torch.zeros(3) # tensor([0., 0., 0.])
print(data.shape) # torch.Size([1, 3, 5, 5])
N, C, H, W = data.shape[:4] # shape是一个列表, N:1 C:3 H:5 W:5
data = data.view(N, C, -1) # 将w,h维度的数据展平,为batch,channel,data,然后对三个维度上的数分别求和和标准差
print(data.shape) # tensor([1, 3, 25])
# 展平后,w,h属于第二维度,对他们求平均,sum(0)为将同一纬度的数据累加
channel_mean += data.mean(2).sum(0)
# 展平后,w,h属于第二维度,对他们求标准差,sum(0)为将同一纬度的数据累加
channel_std += data.std(2).sum(0)
# 获取所有batch的数据,这里为1
nb_samples += N
# 获取同一batch的均值和标准差
channel_mean /= nb_samples
channel_std /= nb_samples
print(channel_mean, channel_std)
# tensor([0.0118, 0.0118, 0.0118]) tensor([0.0057, 0.0057, 0.0057])
我是Ruby和RubyonRails世界的新手。我已经阅读了一些指南,但我在使用以下语法时遇到了一些麻烦。我认为在Ruby中使用:condition语法来定义具有某种访问器的类属性,例如:classSampleattr_accessor:conditionend隐式声明“条件”属性的getter和setter。当我查看一些Rails示例代码时,我发现以下示例我并不完全理解。例如:@post=Post.find(params[:id])为什么它使用这种语法访问id属性,而不是:@post=Post.find(params[id])或者,例如:@posts=Post.find(:all):
例如,:符号-我正在尝试弄清楚:的含义,以及它与@的区别也没有任何符号。如果有真正有用的指南! 最佳答案 它是一个符号,是一种Ruby语言结构。符号类似于字符串,但thisblogpost解释细节。@表示类的实例变量:它基本上是一个在类实例的所有方法之间共享的变量。它与:无关。 关于ruby-:meaninrailsbeforeavariablename?是什么,我们在StackOverflow上找到一个类似的问题: https://stackoverflow
大家早上好我在float学方面遇到了一些问题,完全迷失在“.to_f”、“*100”和“.0”中!我希望有人能帮助我解决我的具体问题,并准确解释他们的解决方案为何有效,以便我下次理解这一点。我的程序需要做两件事:对一组小数求和,确定它们的和是否正好为1.0确定1.0与数字总和之间的差值-将变量的值设置为使总和等于1.0的精确差值。例如:[0.28,0.55,0.17]->总和应为1.0,但我一直得到1.xxxxxx。我正在以下列方式实现总和:sum=array.inject(0.0){|sum,x|sum+(x*100)}/100我需要此功能的原因是我正在读取一组来自excel的小数。
我需要计算我的Rails3应用中两个字段的乘积之和(即相当于Excel的sumproduct函数)。Rails中是否有一种方法可以帮助解决这个问题?如果没有,那么使用自定义sql的Rails代码是什么?例如,酒店有很多房间。房间具有sqft(平方英尺)、数量(该尺寸)和hotel_id的属性。我想计算给定酒店中所有房间的总平方英尺。在SQL中,对于Hotel.id=8,我相信以下语句会起作用:selectsum(rooms.sqft*rooms.quantity)asSumSqftfromroomsinnerjoinhotelsonrooms.hotel_id=hotels.idwhe
先创建一个tensor>>>importtorch>>>a=torch.rand(1,4,3) >>>print(a) tensor([[[0.0132,0.7809,0.0468], [0.2689,0.6871,0.2538], [0.7656,0.5300,0.2499], [0.2500,0.4967,0.0685]]]) 分类进行reshape操作时,假如第二维代表类别,直接reshape使得数据对应结果会错>>>b=a.reshape(-1,4)>>>print(b) tensor([[0.0132,0.7809,0.0468,0.2689],
我正在尝试使用ProjectEuler中的Ruby解决数学问题。Here是我尝试的第一个:Ifwelistallthenaturalnumbersbelow10thataremultiplesof3or5,weget3,5,6and9.Thesumofthesemultiplesis23.Findthesumofallthemultiplesof3or5below1000.请帮助我改进我的代码。total=0(0...1000).eachdo|i|total+=iif(i%3==0||i%5==0)endputstotal 最佳答案
我一直在使用的一些开放源代码具有以下行作为函数声明:defparse_query(query=nil,options={},models=nil)“等于”符号对语句有什么影响?它只是使参数可选吗? 最佳答案 如果调用函数的人没有指定参数,它会设置参数的默认值。 关于ruby-on-rails-rubyrails:whatdoes"equals"symbolmeanasaparameter?,我们在StackOverflow上找到一个类似的问题: https:/
尝试将ActiveStorage用于简单的图像上传表单。它创建成功,但在提交时抛出错误:undefinedmethod`upload'fornil:NilClassDidyoumean?load这是它要我查看的block:@comment=Comment.create!params.require(:comment).permit(:content)@comment.image.attach(params[:comment][:image])redirect_tocomments_pathend这是在完整的Controller中:classCommentsController实际应该发
Anaconda+PyCharm+PyTorch(GPU)+虚拟环境声明一、安装Anaconda二、安装PyCharm三、创建虚拟环境并安装PyTorch四、关联虚拟环境五、致谢声明感谢姜小敏同学对我的支持、鼓励和鞭策!默认你的电脑上已经装有GPU,如果没有GPU,可以正常的进行各种下载安装操作,但是最终结果会有所不同。一、安装Anaconda首先,进入Anaconda官网,单击Download按钮,稍微等待即可下载安装包。下载好之后,双击打开安装包,进行一系列安装操作。建议安装路径全英文,并且一定要记住安装地址。此处不勾选第二项,因此之后需要人为配置环境变量。没啥用,不用勾选,就是跳出两个打
我注意到array.sum和array.inject(:+)产生不同的结果。这是什么原因?a=[10,1.1,6.16]a.inject(:+)#=>17.259999999999998a.sum#=>17.26 最佳答案 Array#sum的C实现委托(delegate)给Kahansummationalgorithm当它的一些输入是float时。这个算法......significantlyreducesthenumericalerrorinthetotalobtainedbyaddingasequenceoffinitepre