【pytorch学习】torch.rand

torch.rand

返回服从均匀分布的初始化后的tenosr,外形是其参数size。
torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)[0,1)

The shape of the tensor is defined by the variable argument size.
'''
Parameters
	size (int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
	
	out (Tensor, optional) – the output tensor.
	
	dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).
	
	layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
	
	device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
	
	requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
'''

参数说明

size: 定义tensor的shape ,这里可以是一个list 也可以是一个tuple

dtype:(可选)我不设置值 默认值就是torch.set_default_tensor_type制定的值,如果需要设置那就是torch.dtype的那几个。作用是指定返回tensor的数据类型

layout:(可选)值为 torch.layout。 torch.layout表示torch.Tensor内存布局的对象。有torch.strided(dense Tensors 默认)并为torch.sparse_coo(sparse COO Tensors)提供实验支持。
torch.strided代表密集张量,是最常用的内存布局。每个strided张量都会关联 一个torch.Storage,它保存着它的数据。这些张力提供了多维度, 存储的strided视图。Strides是一个整数型列表:k-th stride表示在张量的第k维从一个元素跳转到下一个元素所需的内存。关于这里的理解请看demo2

device:(可选)就是创建的tensor存放的device,这里就不做赘述了,大致概念了解看这里\

requires_grad: (可选)是bool 类型的值,默认值是False .因为 pytorch 后期的版本将Varibale 和Tensor进行合并了,这里的如果设置为Flase 表示再反响传播的时候不会对这个节点机型求导,如果你对tensorflow熟悉,

例子

demo1
x=torch.rand([2,5])
x.stride()
		'''
		输出分别为:
		tensor([[0.0834, 0.8865, 0.7525, 0.7402, 0.0886],
                [0.0364, 0.2632, 0.0531, 0.8937, 0.6145]])
		        
		(5, 1)  #注意看这里,要深刻理解这里为啥这个数值
		'''

y=x.t()
y.stride()
	'''
	输出分别为:
	
	tensor([[0.0834, 0.0364],
	        [0.8865, 0.2632],
	        [0.7525, 0.0531],
	        [0.7402, 0.8937],
	        [0.0886, 0.6145]])
	
	(1, 5) #注意看这里
	'''


  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值