当前位置: 首页 > wzjs >正文

哪些网站设计的好网站建立有哪些功能

哪些网站设计的好,网站建立有哪些功能,如何建设好一个公司网站,展示类网站开发费用一、张量 张量其实就是数组,不过是在深度学习中是这样的叫法 1.张量的创建 (1)基本创建方式 torch.tensor():根据指定数据创建张量 import torch import numpy as np """创建张量标量""" dat…

一、张量

  • 张量其实就是数组,不过是在深度学习中是这样的叫法

1.张量的创建

(1)基本创建方式

  • torch.tensor():根据指定数据创建张量
import torch 
import numpy as np
"""创建张量标量"""
data = torch.tensor(10)
print(data) # 输出结果tensor(10)"""numpy数组"""
data = np.random.randn(2, 3)
data = torch.tensor(data)
print(data) # 输出结果tensor([[0.1345,0.1149,0.2435],#                 [0.8026,-0.6744,-1.0918]],dtype                                             = torch.float64)"""列表,下面代码使用默认元素类型"""
data = [[10.,20.,30.],[40.,50.,60.]]
data = torch.tensor(data)
print(data) # 输出结果tensor([[10.,20.,30.],#                [40.,50.,60.])
  • torch.Tensor():根据指定形状创建张量,也可以用来创建指定数据的张量
"""创建2行3列的张量"""
data = torch.Tensor(2, 3)
"""注意:如果传递列表,则创建包含指定元素的张量"""
data = torch.Tensor([10])data = torch.Tensor([10, 20]) # tensor([10., 20.])
  • torch.IntTensor()、torch.FloatTensor()、torch.DoubleTensor():创建指定类型的张量
"""创建2行3列"""
torch.IntTensor(2, 3)"""列表"""
torch.IntTensor([2,3]) # 输出 tensor([2.,3.])"""如果类型不符合,则会强制转换"""
torch.IntTensor([2.43,3.42]) # 输出 tensor([2.,3.])

(2)创建线性和随机张量

  • torch.arangetorch.linspace创建线性张量
data = torch.arange(0,10,2) # 输出结果 tensor([0,2,4,6,8])
data = torch.linspace(0,9,10) 
  • torch.random.init_seedtorch.random.manual_seed随机种子设置
  • torch.randn创建随机张量
data = torch.randn(2,3) # 创建2行3列张量"""查看随机数种子"""
print('随机数种子':torch.random.initial_seed())"""随机数种子设置"""
torch.random.manual_seed(100)
data = torch.randn(2,3)
print(data)
print('随机数种子:',torch.random.initial_seed())

(3)创建0-1张量

  • torch.onestorch.ones_like创建全1张量
  • torch.zerostorch.zeros_like创建全0张量
  • torch.fulltorch.full_like创建全为指定值张量
data = torch.randn(2,3)
torch.zeros(4,5) # 生成4行5列全为0的二维数组
torch.zeros_like(data) #  生成2行3列全为0的二维数组torch.ones(4,5)
torch.ones_like(data)torch.full([4,5],10) # 生成4行5列 全为10的二维数组
torch.full_like(data,20)

2.张量的类型转换

(1)张量元素的类型转换

  • data.type(torch.DoubleTensor):ShortTensor,IntTensor, LongTensor, FloatTensor
  • data.double():short, int, long, float
data = torch.randn(4, 5)
print(data)
print(data.dtype) # torch.float32
print(data.type(torch.IntTensor).dtype) # torch.int32
print(data.int().dtype) # torch.int32

(2)张量转换成Numpy数组

  • 使用Tensor.numpy函数可以将张量转换为ndarray数组,但是共享内存,可是使用copy函数避免共享
# 共享空间
import torch
import numpy as nptorch.random.manual_seed(2)
data_tensor = torch.randint(0, 10, [2, 3])
print(type(data_tensor))data_numpy = data_tensor.numpy()
print(type(data_numpy))data_numpy[0][0] = 100
print(data_numpy)
print(data_tensor)# 使用copy函数避免空间共享
import torch
import numpy as nptorch.manual_seed(2)
data_tensor = torch.randint(0, 10, [2, 3])
print(type(data_tensor))data_numpy = data_tensor.numpy().copy()
print(type(data_numpy))data_numpy[0][0] = 100
print(data_tensor)
print(data_numpy)

(3)Numpy数组转换为张量

  • 使用from_numpy可以将naddray数组转换为Tensor,默认共享内存,同样可以使用copy函数避免内存共享
  • 使用torch.tensor可以将ndarray数组转换为Tensor,默认不共享内存
# 内存共享
import torch
import numpy as npdata_numpy = np.array([1, 2, 3])
data_tensor = torch.from_numpy(data_numpy)
data_tensor[0] = 10
print(data_numpy)
print(data_tensor)
[10  2  3]
tensor([10,  2,  3], dtype=torch.int32)
# 避免内存共享
import torch
import numpy as npdata_numpy = np.array([1, 2, 3])
data_tensor = torch.from_numpy(data_numpy.copy())
data_tensor[0] = 10
print(data_numpy)
print(data_tensor)
[1 2 3]
tensor([10,  2,  3], dtype=torch.int32)
# 避免内存共享
import torch
import numpy as npdata_numpy = np.array([1, 2, 3])
data_tensor = torch.Tensor(data_numpy)
data_tensor[0] = 10
print(data_numpy)
print(data_tensor)
[1 2 3]
tensor([10.,  2.,  3.])

(4)标量张量和数字转换

  • 对于只有一个元素的张量,使用item()函数将该值从张量中提取出来
    • 注意:在后面的反向传播中,必须要加上item(),否则模型会报错
import torchdata = torch.tensor(30)
print(data) # tensor(30)
print(data.item()) # 30data1 = torch.tensor([30])
print(data1) # tensor([30])
print(data1.item()) # 30 (得到的不是[30],最终只能得到数值)

3.张量数值计算

(1)张量的基本运算

加减乘除取负号

  • add, sub, mul, div, neg

  • add_, sub_, mul_, div_, neg_(其中带下划线的版本会修改原数据)

import torchtorch.random.manual_seed(22)
data = torch.randint(0, 10, [2, 3])
print(data)print(data.add(10))
print(data) # 原数据并没有发生改变
tensor([[9, 6, 6],[4, 2, 2]])
tensor([[19, 16, 16],[14, 12, 12]])
tensor([[9, 6, 6],[4, 2, 2]])
import torchtorch.random.manual_seed(22)
data = torch.randint(0, 10, [2, 3])
print(data)print(data.add_(10))
print(data) # 原数据会发生改变
tensor([[9, 6, 6],[4, 2, 2]])
tensor([[19, 16, 16],[14, 12, 12]])
tensor([[19, 16, 16],[14, 12, 12]])

(2)点乘运算

  • 定义:是两个同维矩阵对应位置的元素相乘,使用mul和运算符*实现
import torchtorch.random.manual_seed(22)
data1 = torch.randint(0, 10, [2, 3])
print(data1)torch.random.manual_seed(23)
data2 = torch.randint(0, 10, [2, 3])
print(data2)# 点乘
print(torch.mul(data1, data2))print(data1 * data2)
输出结果:
tensor([[9, 6, 6],[4, 2, 2]])
tensor([[1, 6, 6],[7, 0, 2]])
tensor([[ 9, 36, 36],[28,  0,  4]])
tensor([[ 9, 36, 36],

(3)矩阵乘法

  • 矩阵乘法运算要求第一个矩阵shape:(n, m),第二个矩阵shape:(m ,p),两个矩阵点积运算shape为(n, p)
    • 运算符@用于进行两个矩阵的乘积运算
    • torch.matmul对进行乘积运算的两矩阵形状没有限定,对数输入的shape不同的张量,对应的最后几个维度必须符合矩阵运算规则
import torchtorch.random.manual_seed(22)
data1 = torch.randint(0, 10, [2, 4])
print(data1)torch.random.manual_seed(23)
data2 = torch.randint(0, 10, [4, 5])
print(data2)# 矩阵乘法
print(data1 @ data2)print(torch.matmul(data1, data2))
输出结果:
tensor([[9, 6, 6, 4],[2, 2, 2, 1]])
tensor([[1, 6, 6, 7, 0],[2, 7, 1, 4, 7],[5, 6, 3, 7, 8],[7, 5, 2, 9, 8]])
tensor([[ 79, 152,  86, 165, 122],[ 23,  43,  22,  45,  38]])
tensor([[ 79, 152,  86, 165, 122],[ 23,  43,  22,  45,  38]])

4.常见运算函数

  • 均值
  • 平方根
  • 指数计算
  • 对数计算
import torchdata = torch.randint(0, 10, [2, 3],dtype = torch.float64)
print(data)"""计算均值(注意:tensor必须为Float或者Double类型)"""
print(data.mean())
print(data.mean(dim = 0)) # 按列计算均值
print(data.mean(dim = 1)) # 按行进行计算"""计算总和"""
print(data.sum())
print(data.sum(dim = 0))
print(data.sum(dim = 1))"""计算平方"""
print(torch.pow(data, 2))"""计算平方根"""
print(data.sqrt())"""指数计算"""
print(data.exp())"""对数计算"""
print(data.log())
print(data.log2())
print(data.log10())

文章转载自:

http://mERwo3ml.jwxmn.cn
http://gu9Z5YZD.jwxmn.cn
http://0oHNxPcO.jwxmn.cn
http://tnjIYatu.jwxmn.cn
http://cU5VFsnZ.jwxmn.cn
http://J4xIjkpD.jwxmn.cn
http://4fd0zY22.jwxmn.cn
http://XLaIfJHb.jwxmn.cn
http://URCdlJXA.jwxmn.cn
http://FHxmn7ON.jwxmn.cn
http://IUn4cq1z.jwxmn.cn
http://RAopVnyZ.jwxmn.cn
http://ZNTBVK11.jwxmn.cn
http://pCMGjQ0G.jwxmn.cn
http://cCX9X1TE.jwxmn.cn
http://bSq0pZJy.jwxmn.cn
http://y1Cs7l6D.jwxmn.cn
http://5lTtljk0.jwxmn.cn
http://pil9dqZq.jwxmn.cn
http://SJQTs1nP.jwxmn.cn
http://cmKorwn6.jwxmn.cn
http://s056CZWg.jwxmn.cn
http://7HNLjUc5.jwxmn.cn
http://dRxAanD3.jwxmn.cn
http://k9Wh21vf.jwxmn.cn
http://COM0TWfU.jwxmn.cn
http://BbTBlgtm.jwxmn.cn
http://s1D97vTN.jwxmn.cn
http://LzYHax4f.jwxmn.cn
http://87SFUsSP.jwxmn.cn
http://www.dtcms.com/wzjs/670631.html

相关文章:

  • 做金融的看哪些网站学网站建设要多久
  • 专业东莞网站建设报价郑州注册公司网站
  • 深圳做网站价格WordPress怎么加入用户关注
  • 东莞市专注网站建设网站SEO做点提升流量象客
  • 铝合金做网站成都口碑最好的家装公司
  • 网站建设尺寸像素是多少钱亿恩 网站备案
  • asp.net网站制作教程搭建网站全过程
  • 电子商务网站建设与管理的背景模板型网站
  • 域名注册完成后如何做网站百度竞价的优势和劣势
  • 做公司网站源代码怎么写创建网站视频
  • 白云鄂博矿区网站建设电商设计详情页
  • 网站什么语言好网站友情链接要加什么用
  • 网站建设与管理模拟试卷王烨辉简历
  • 做网站需要什么执照茶叶手机网站建设
  • 首页有动效的网站长沙网站优化对策
  • 西安手机网站案例建材 网站 模板
  • 如何让网站免费怎么用手机做网站编辑
  • 网页设计公司哪个好苏州seo关键字优化
  • 甜品网站首页设计深圳网页设计学校
  • 模板建站服务器衙门口网站建设
  • 十堰网站建设费用连锁销售公司网站的建设方案
  • 夫唯seo怎么样网站优化的方式
  • 盐城网站开发招代理广州腾虎网络科技有限公司
  • 关于要求建设网站的请示网站建设用什么开源程序好
  • 网站安全评估报告优化大师最新版本
  • 做网站哪里学西安网站快速优化
  • 餐饮门户网站源码WordPress怎么文章分类
  • 给别人做网站 网站违法了大型网站开发合同
  • 免费做国际贸易的网站科技感网站设计
  • 网站建设企业建站方案建设部造价工程师网站