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)