pytorch之reshape()


1、說明:

  reshape()和numpy.reshape()函數的作用是,重塑的數組的shape。

2、注意:(參考鏈接1:Python中reshape函數參數-1的意思?

  python默認是按行取元素。

  參數-1,表示模糊reshape的意思。

  比如:reshape(-1,3),固定3列 多少行不知道。

3、實驗代碼:

  

1 import numpy as np
2 import torch
3 a = [[1,2,3],[4,5,6]] #定義一個數組
4 b = torch.tensor(a)    #初始化一個tensor
5 print("原始數組為:\n{0}\n\n".format(b))
6 print("reshape(-1),(就是把任何shape的tensor拉平):\n{0}\n\n".format(b.reshape(-1)))#string.format(var)是python里的格式化輸出函數
7 print("reshape(-1,2),(reshape成三行兩列):\n{0}\n\n".format(b.reshape(-1,2)))

 

4、實驗結果(jupyter notebook)

原始數組為:
tensor([[1, 2, 3],
        [4, 5, 6]])


reshape(-1),(就是把任何shape的tensor拉平):
tensor([1, 2, 3, 4, 5, 6])


reshape(-1,2),(reshape成三行兩列):
tensor([[1, 2],
        [3, 4],
        [5, 6]])

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM