1.単一のデータを他のデータ形式に変換します
1.1単一データ->リスト
データに対して直接リスト操作を実行するか、既存のリストに直接アクセスします
文法:
list_name = [single_data] 或者 list_name.append(single_data)
1.2単一データ-> numpy.ndarray
初期化構文を直接使用できます
文法:
numpyarr_name = np.array(single_data)
例えば:
import numpy as np
single_data = 100
numpyarr_test = np.array(single_data)
print(numpyarr_test)
print(numpyarr_test.dtype)
print(numpyarr_test.shape)
1.3単一データ-> torch.tensor
初期化構文を直接使用できます
メソッド1の構文:(ただし、このメソッドは0の次元を作成します)
tensor_name = torch.tensor(single_data)
方法2の構文:リストを使用して新しいテンソルを作成する
tensor_name = torch.Tensor([single_data])
メソッド3の構文:新しい空のテンソルを作成して割り当てます(このメソッドの新しい次元は1です)
tensor_name = torch.empty(1)
tensor_name[0] = single_data
例えば:
import torch
single_data = 100
tensor_test = torch.tensor(single_data)
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
single_data = 100
tensor_test = torch.Tensor([single_data])
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
single_data = 100
tensor_test = torch.empty(1)
tensor_test[0] = single_data
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
次に、リストは他のデータ形式に変換されます
2.1リスト->単一データ
Pythonでの強制型変換の直接使用
2.2リスト-> numpy.ndarray
注:変換するときは、リスト内のタイプが同じである必要があります
文法:
numpyarr_name = np.array(list_name)
例えば:
import numpy as np
list_test = [1,2,3,4]
print(type(list_test))
numpyarr_test = np.array(list_test)
print(type(numpyarr_test))
print(numpyarr_test.dtype)
print(numpyarr_test.shape)
2.3リスト-> torch.tensor
注:リスト内のすべての要素(多次元リスト内の要素を含む)のタイプは、変換中に同じである必要があります
文法:
tensor_name = torch.Tensor(list_name)
例えば:
import torch
list_test = [[1,2,3],[4,5,6]]
print(type(list_test))
tensor_test = torch.Tensor(list_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
3.numpy.ndarrayを他のデータ形式に変換します
3.1numpy.ndarry->単一のデータ
Pythonでの強制型変換の直接使用
例えば:
import numpy as np
single_data = float(np.array([100]))
print(single_data)
print(type(single_data))
3.2numpy.ndarry->リスト
文法:
numpyarr_name.tolist()
例えば:
import numpy as np
numpyarr_test = np.ones((1,2))
list_test = numpyarr_test.tolist()
print(type(list_test))
print(len(list_test))
print(len(list_test[0]))
3.3 numpy.ndarry-> torch.tensor
注1:CPUテンソルにのみ変換できます
注2:形状は変わりません
文法:
tensor_name = torch.from_numpy(numpyarr_name)
例えば:
import torch
import numpy as np
numpyarr_test = np.ones((1,2))
print(type(numpyarr_test))
print(numpyarr_test.dtype)
tensor_test = torch.from_numpy(numpyarr_test)
print(type(tensor_test))
print(tensor_test.type())
4つ目は、torch.tensorが他のデータ形式に変換されることです。
4.1torch.tensor->単一データ
Pythonでの強制型変換の直接使用
例えば:
import torch
tensor_test = torch.ones(1)
single_data = float(tensor_test)
print(single_data)
print(type(single_data))
4.2torch.tensor->リスト
文法:
list_name = tensor_name.tolist()
例えば:
import torch
tensor_test = torch.ones((1,3))
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
list_test = tensor_test.tolist()
print(list_test)
print(type(list_test))
4.3 torch.tensor-> numpy.ndarry
注:このような変換を実行できるのはCPUテンソルのみです
注:形状は変わりません
文法:
numpyarr_name = tensor_name.numpy()
# 直接使用不进行赋值的话不会改变类型
例えば:
import torch
import numpy as np
tensor_test = torch.Tensor(1,2)
print(type(tensor_test))
print(tensor_test.type())
numpyarr_test = tensor_test.numpy()
print(type(numpyarr_test))
print(numpyarr_test.dtype)
最後に、参照
Pytorch:テンソル型の構築と相互変換_JNing-CSDNblog
NumPy-データ型・TutorialsPointNumPyチュートリアル
Pytorchの基本データtypes_torrentsource -CSDNblog_pytorchデータ型
[numpy] ndarrayとlist_doufuxixiのブログ-CSDNblog_ndarrayからlistへの変換
Python3リスト、np.array、torch.tensor相互変換_黒と白のDoveSaniのブログ-CSDNブログ
Python-配列とlist_Haiyang_Duan間の変換-CSDNblog
pytorchのテンソルおよびその他のデータ構造の変換(int、list、array)
[PyTorch研究ノート] 3:Tensor_LauZyHouのノートを作成するためのさまざまな方法-CSDNblog_Createテンソル