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

做电影网站解析怎样做境外电商

做电影网站解析,怎样做境外电商,1688做网站需要多少钱,网站设计哪家专业目录 基本概念参数详解 示例view 和 reshape 在具体应用中的参数解释参数解释 更多示例高维张量示例非连续内存示例 总结 基本概念 view 和 reshape 都用于调整张量的形状#xff0c;它们的参数是新的形状#xff0c;每个维度的大小可以指定为具体的数值或者 -1。-1 表示这个… 目录 基本概念参数详解 示例view 和 reshape 在具体应用中的参数解释参数解释 更多示例高维张量示例非连续内存示例 总结 基本概念 view 和 reshape 都用于调整张量的形状它们的参数是新的形状每个维度的大小可以指定为具体的数值或者 -1。-1 表示这个维度的大小由张量的总元素数量自动推断。 参数详解 new_shape这是一个 tuple 或者一个 list定义了新的形状。每个元素代表对应维度的大小。-1特殊值表示该维度的大小由其他维度自动推断。 示例 假设有一个张量 tensor形状为 [batch_size, seq_len, num_labels]。 import torchtensor torch.randn(4, 3, 5) # 示例张量形状为 (4, 3, 5)要将其形状调整为 [12, 5]可以使用 view 或 reshape。 # 使用 view reshaped_tensor_view tensor.view(-1, 5) print(View tensor shape:, reshaped_tensor_view.shape) # 输出: torch.Size([12, 5])# 使用 reshape reshaped_tensor_reshape tensor.reshape(-1, 5) print(Reshape tensor shape:, reshaped_tensor_reshape.shape) # 输出: torch.Size([12, 5])view 和 reshape 在具体应用中的参数解释 在序列标记分类任务中我们通常需要将 logits 和标签调整为适合计算损失的形状。 假设 logits 的形状为 [batch_size, seq_len, num_labels]我们希望将其调整为 [batch_size * seq_len, num_labels]以便与标签 [batch_size * seq_len] 对应。 以下是使用 view 和 reshape 的示例 import torch import torch.nn as nn from transformers import BertTokenizer, BertForTokenClassification# 初始化模型和tokenizer model_name bert-base-uncased tokenizer BertTokenizer.from_pretrained(model_name) model BertForTokenClassification.from_pretrained(model_name, num_labels5) # 假设有5个分类# 假设输入文本 text I love natural language processing. inputs tokenizer(text, return_tensorspt)# 获取模型输出 outputs model(**inputs) seq_logits outputs.logits# 假设标签映射 tags_to_idx {O: 0, B-PER: 1, I-PER: 2, B-LOC: 3, I-LOC: 4} tags torch.tensor([[0, 0, 0, 0, 1, 2, 3, 4]]) # 示例标签形状为 (batch_size, seq_len)# 使用 reshape 调整形状 pred seq_logits.reshape([-1, len(tags_to_idx)]) label tags.reshape([-1]) ignore_index tags_to_idx[O]# 计算损失 criterion nn.CrossEntropyLoss(ignore_indexignore_index) loss criterion(pred, label) print(Loss with reshape:, loss.item())# 使用 view 调整形状 pred_view seq_logits.view(-1, len(tags_to_idx)) label_view tags.view(-1)# 计算损失 loss_view criterion(pred_view, label_view) print(Loss with view:, loss_view.item())参数解释 seq_logits.reshape([-1, len(tags_to_idx)]) 和 seq_logits.view(-1, len(tags_to_idx)]) -1表示这个维度的大小由其他维度自动推断。这里是将 [batch_size, seq_len, num_labels] 调整为 [batch_size * seq_len, num_labels]。len(tags_to_idx)表示 num_labels即分类的数量。 更多示例 高维张量示例 假设有一个四维张量形状为 [2, 2, 3, 4]我们希望将其调整为 [4, 3, 4] import torchtensor torch.randn(2, 2, 3, 4) print(Original shape:, tensor.shape) # 输出: torch.Size([2, 2, 3, 4])# 使用 view 调整形状 view_tensor tensor.view(4, 3, 4) print(View tensor shape:, view_tensor.shape) # 输出: torch.Size([4, 3, 4])# 使用 reshape 调整形状 reshape_tensor tensor.reshape(4, 3, 4) print(Reshape tensor shape:, reshape_tensor.shape) # 输出: torch.Size([4, 3, 4])非连续内存示例 import torchtensor torch.tensor([[1, 2, 3], [4, 5, 6]]) transpose_tensor tensor.t() # 转置张量 print(Transpose shape:, transpose_tensor.shape) # 输出: torch.Size([3, 2])# 使用 view会报错因为内存不连续 try:view_tensor transpose_tensor.view(-1) except RuntimeError as e:print(Error using view:, e)# 使用 contiguous 方法确保内存连续 contiguous_tensor transpose_tensor.contiguous() view_tensor contiguous_tensor.view(-1) print(Contiguous view tensor:, view_tensor) print(Contiguous view tensor shape:, view_tensor.shape) # 输出: torch.Size([6])# 使用 reshape reshape_tensor transpose_tensor.reshape(-1) print(Reshape tensor:, reshape_tensor) print(Reshape tensor shape:, reshape_tensor.shape) # 输出: torch.Size([6])总结 view 和 reshape 参数 参数是一个 tuple 或者 list定义新的形状。-1 表示该维度的大小由其他维度自动推断。 view 的限制要求输入张量是连续的。reshape 的灵活性可以处理非连续内存的张量。 通过这些详细的例子和解释你可以更好地理解如何使用 view 和 reshape 来调整张量的形状。
http://www.hkea.cn/news/14340431/

相关文章:

  • 选择佛山网站设计wordpress 4.4.1漏洞
  • 网站建设流程域名申请域名注册价格
  • 建设外贸型网站做网站为什么需要购买域名
  • 专做商业平台网站东莞英文建站公司
  • 青岛电子商务网站建设十个免费软件不收费
  • 网站空间的分类网站开发建设赚钱吗
  • 定制化网站开发公司英文电商网站建设
  • 网站平台是怎么做财务的建设网站需要懂什么
  • 南宁网站推广手段杭州余杭区网站建设
  • 东莞网站建设 烤活鱼建立网站链接结构的基本方式是
  • 苏州h5建站排名优化软件点击
  • xunsearch做搜索网站wordpress怎么收录
  • 做it的网站互联网技术的应用
  • 资阳市住房和城乡建设局网站离石市网站建设公司
  • 眉山招聘网站建设国家高新技术企业图片
  • 软件开发与网站开发wordpress 后门检查
  • 网站建设与维护的选择题editplus怎么创网站
  • 查询价格的网站广州网站制作培训
  • 网站改版设计思路郑州网站设计
  • 做杂志的网站有哪些内容莱芜金点子电子版最新招聘信息
  • 简单好看个人主页网站模板专业团队介绍文案
  • 网站建设图片怎么切大连三川建设集团
  • 在那个网站找模具做图片在线转外链
  • 网站建设厌倦西平县住房和城乡建设局网站
  • 跑步机 东莞网站建设网站联系方式设计
  • 螺旋钢管网站建设网站建设 标书
  • 零起飞网站建设工作室seo推广怎么样
  • 苏州优秀网站设计做网站页面视频教学
  • 摄影师作品网站一个网站做无限关键词
  • 网站后台 清理缓存WordPress置顶不生效