deep learning with pytorch——5

数字类型
1.张量构造函数(即张量、零和一等函数)的dtype参数指定将包含在张量中的数字数据类型。数据类型指定张量可以保存的可能值(整数与浮点数)和每个值的字节数。下面是dtype参数的可能值列表,张量中可以存储的数据类型:
deep learning with pytorch——5每个torch.float、torch.double等都有一个对应的具体类torch.FloatTensor、torch.doublensor等。torch.int8的类是torch.CharTensor,torch.uint8的类是torch.ByteTensor。torch.Tensor是torch.FloatTensor的别名。默认数据类型是32位浮点
2.要分配正确数值类型的张量,可以指定适当的dtype作为构造函数的参数,如下所示:构造张量时可以通过dtype参数来指定张量中元素的数据类型
deep learning with pytorch——5
3.通过访问相应的属性,可以了解张量的dtype
deep learning with pytorch——5
4.可以使用相应的转换方法将张量创建函数的输出转换为正确的类型
deep learning with pytorch——5
或者
deep learning with pytorch——5
始终可以使用类型方法将一种类型的张量转换为另一种类型的张量:

deep learning with pytorch——5
产生一个在0到1之间的10×2的张量,然后转化为short类型