日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

torch.squeeze 函数运用

發布時間:2023/12/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 torch.squeeze 函数运用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1. 定義
  • 2. 代碼

1. 定義

torch.squeeze函數的作用是對輸入的張量進行處理,如果張量維度里面有大小為1 的部分,那我們就移除,否則保留

torch.squeeze(input, dim=None, *, out=None) → Tensor
  • input : 輸入的張量
  • dim : 默認保留,可以指定維度

2. 代碼

# -*- coding: utf-8 -*- # @Project: zc # @Author: zc # @File name: test_torch_squeeze # @Create time: 2021/12/25 16:17# 導入相關數據庫 import torch# 定義一個張量,維度為 size = [2,1,2,1,2] # torch.sequeeze :表示移除張量中,所有大小為1的維度 # dim :指定此維度,如果大小為 1 ,則移除,否則保持 x = torch.zeros(2, 1, 2, 1, 2) x_squeeze_0 = x.squeeze(0) # 第 dim=0 維度是不是1,則保持;size = [2,1,2,1,2] x_squeeze_1 = x.squeeze(1) # 第 dim=1 維度是1,則移除; size = [2,2,1,2] x_squeeze_all = torch.squeeze(x) # 移除所有size=1 的維度,size = [2,2,2] print(f'x_shape={x.shape}') print(f'x_squeeze_0={x_squeeze_0.shape}') print(f'x_squeeze_1={x_squeeze_1.shape}') print(f'x_squeeze_all={x_squeeze_all.shape}')
  • 結果
x_shape=torch.Size([2, 1, 2, 1, 2]) x_squeeze_0=torch.Size([2, 1, 2, 1, 2]) x_squeeze_1=torch.Size([2, 2, 1, 2]) x_squeeze_all=torch.Size([2, 2, 2])

總結

以上是生活随笔為你收集整理的torch.squeeze 函数运用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。