1.1 tensor

#学习torch的基本操作
#__future__模块,把下一个新版本的特性导入到当前版本

from __future__ import print_function
import torch

#关于构造张量
####################
'''
#构造5*3空矩阵
x = torch.empty(5,3)
print ('x=',x)

#构造5*3随机矩阵,矩阵元素大小(0,1)
q = torch.rand(5,3)
print ('q=',q)

#构造5*3的0矩阵,限定矩阵元素为长整型
w = torch.zeros(5,3,dtype=torch.long)
print ('w=',w)
print ('w.type=',w.dtype)  #输出矩阵元素类型
print ('w.shape=',w.shape) #输出矩阵尺寸


#直接输入数据构造张量
r = torch.tensor([5,2.5])
print (r)

# 基于已存在张量构建新的张量
t = r.new_ones(5,3,dtype=torch.double)
print (r,'\n',t)

t = torch.randn_like(t,dtype=torch.float)
print (t)
print (t.size())  #获得尺寸

#关于张量之间的运算
###################
#加法_1
#相加的矩阵元素类型应该一样,如都是double
t = r.new_ones(5,3,dtype=torch.double)
print (t)
y = torch.rand(5,3,dtype=torch.double) 
print (y)
print (t+y)

#加法_2
print (torch.add(t,y))

result = torch.empty(5,3,dtype=torch.double)
torch.add(t,y,out=result)
print (result)

#原位相加
x = torch.rand(5,3,dtype=torch.double)
print (y)
y.add_(x)
print (x)
print (y)

#note:任何原位的操作,都在后面加一个 '_' ,如x.copy_(y), x.t_()等

print (x[:,1])



# 调整张量尺寸
#使用torch.view

a = torch.randn(4,4)
print (

a)
s = a.view(16)
print (s)
d = a.view(-1,8)  #-1代表,尺寸调整以列数8为主,

 

print (d)
print (a.size(),s.size(),d.size())



#对只有一个元素的张量,可以使用 .item()  将其值取出来作为一个数
x = torch.randn(1)
print(x)
print(x.item())


#torch到numpy之间的转换
#torch张量与numpy数组共享同一个存储位置,改变其中一个的值,另一个也会随之变化
a = torch.ones(5,5)
print (a)
b = a.numpy()
print(b)
a.add_(1)
print(a)
print(b)
'''

#numpy到torch之间的转换
import numpy as np
a = np.ones(5)
print(a)
b = torch.from_numpy(a)
print(b)
np.add(a,1,out=a)
print(a)
print(b)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:一维向量一般用来表示Bias或者是Linear input。\[1\] torch.tensor(\[1.1\]) \[2\] tensor(\[1.1000\]) \[3\] torch.tensor(\[1.1, 2.2\]) \[4\] tensor(\[1.1000, 2.2000\]) \[5\] torch.FloatTensor(1) # 创建一维Float型tensor \[6\] tensor(\[1.4013e-45\]) \[7\] torch.FloatTensor(2) \[8\] tensor(\[3.7140e+00, 4.5916e-41\]) \[9\] data = np.ones(2) # 通过np创建向量再转换成tensor \[10\] array(\[1., 1.\]) \[11\] data \[12\] torch.from_numpy(data) \[13\] tensor(\[1., 1.\], dtype=torch.float64) \[14\] a = torch.ones(2) \[15\] a.shape \[16\] torch.Size(\[2\]) 二维Tensor。引用\[2\]:0维tensor一般用来表示损失函数的数值及Loss的数值。\[1\] import torch \[2\] torch.tensor(1.) \[3\] tensor(1.) \[4\] a = torch.tensor(1.3) \[5\] a.shape \[6\] torch.Size(\[\]) \[7\] len(a.shape) \[8\] 0 \[9\] a.size() \[10\] torch.Size(\[\]) 一维向量。引用\[3\]:一般用来表示CNN的数据,表达形式为\[b,c,w,h\] b: batch size c: channel w: width h: hight \[1\] a = torch.rand(2,3,28,28) \[2\] a.shape \[3\] torch.Size(\[2, 3, 28, 28\]) \[4\] a.numel() \[5\] 4704 \[6\] a.dim() \[7\] 4 创建Tensor import from numpy \[1\] import torch \[2\] import numpy as np \[1\] a = np.array(\[2,3,3\]) \[4\] torch.from_numpy(a) \[5\] tensor(\[2, 3, 3\], dtype=torch.int32) \[6\] a = np.ones(\[2,3\]) # 创建元素都为1的2行3列矩阵。 \[7\] torch.from_numpy(a) \[8\] tensor(\[\[1., 1., 1.\], \[1., 1., 1.\]\], dtype=torch.float64) import from list \[1\] torch.tensor(\[2.,3.2\]) \[2\] tensor(\[2.0000, 3.2000\]) \[3\] torch.FloatTensor(\[2.,3.2\]) \[4\] tensor(\[2.0000, 3.2000\]) \[5\] torch.tensor(\[\[2.,3.2\],\[2.,2.3\]\]) \[6\] tensor(\[\[2.0000, 3.2000\], \[2.0000, 2.3000\]\]) # 创建两行三列的tensor。 \[7\] torch.FloatTensor(2,3) \[8\] tensor(\[\[0., 0., 0.\], \[0., 0., 0.\]\])。 问题: pytorch tensor 转string 回答: 在PyTorch中,可以使用`str()`函数将一个PyTorch tensor转换为字符串。例如,如果有一个名为`tensor1`的tensor,可以使用`str(tensor1)`将其转换为字符串表示形式。 #### 引用[.reference_title] - *1* *2* *3* [Pytorch数据类型及Tensor的相关操作](https://blog.csdn.net/leeyns/article/details/113367690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值