[Type conversion from matrix to image 3]: mutual conversion of single-dimensional data, List, Numpy.ndarray, torch.Tensor, etc.

1. Convert a single data to other data formats

1.1 Single data -> list

Perform list operations directly on the data, or directly access an existing list

grammar:

list_name = [single_data] 或者 list_name.append(single_data)

1.2 Single data -> numpy.ndarray

You can use the initialization syntax directly

grammar:

numpyarr_name = np.array(single_data)

For example:

import numpy as np
single_data = 100
numpyarr_test = np.array(single_data)
print(numpyarr_test)
print(numpyarr_test.dtype)
print(numpyarr_test.shape)

image-20210319093653825

1.3 Single data ->torch.tensor

You can use the initialization syntax directly

Method one syntax: (but this method creates a dimension of 0)

tensor_name = torch.tensor(single_data)

Method two syntax: create a new tensor by way of a list

tensor_name = torch.Tensor([single_data])

Method three syntax: create a new empty tensor and assign it in (the new dimension of this method is 1)

tensor_name = torch.empty(1)
tensor_name[0] = single_data  

For example:

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)

image-20210319100639662

Second, the list is converted to other data formats

2.1 list -> single data

Direct use of coercive type conversion in python

2.2 list -> numpy.ndarray

Note: The types in the list must be the same when converting

grammar:

numpyarr_name = np.array(list_name)

For example:

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)

image-20210318224551965

2.3 list -> torch.tensor

Note: The types of all the elements in the list (including the elements in the multi-dimensional list) must be the same when converting

grammar:

tensor_name = torch.Tensor(list_name) 

For example:

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)

image-20210318225625664

3. Convert numpy.ndarray to other data formats

3.1 numpy.ndarry -> single data

Direct use of coercive type conversion in python

For example:

import numpy as np
single_data = float(np.array([100]))
print(single_data)
print(type(single_data))

image-20210319100848124

3.2 numpy.ndarry -> list

grammar:

numpyarr_name.tolist()

For example:

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]))

image-20210318224805672

3.3 numpy.ndarry -> torch.tensor

Note 1: Can only be converted to CPU tensor

Note 2: The shape will not change

grammar:

tensor_name = torch.from_numpy(numpyarr_name)

For example:

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())

image-20210318221447318

Four, torch.tensor is converted to other data formats

4.1 torch.tensor -> single data

Direct use of coercive type conversion in python

For example:

import torch
tensor_test = torch.ones(1)
single_data = float(tensor_test)
print(single_data)
print(type(single_data))

image-20210319102351865

4.2 torch.tensor -> list

grammar:

list_name =  tensor_name.tolist()

For example:

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))

image-20210319102813437

4.3 torch.tensor -> numpy.ndarry

Note: Only CPU tensors can perform such conversion

Note: the shape will not change

grammar:

numpyarr_name = tensor_name.numpy()
# 直接使用不进行赋值的话不会改变类型

For example:

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)

image-20210318221515828

LAST, references

Pytorch: tensor type construction and mutual conversion_JNing-CSDNblog

NumPy-Data Type · TutorialsPoint NumPy Tutorial

Pytorch basic data types_torrent source -CSDN blog_pytorch data types

[numpy] Conversion between ndarray and list _doufuxixi's blog -CSDN blog _ndarray to list

Python3 list, np.array, torch.tensor mutual conversion_black and white Dove Sani's blog-CSDN blog

Python-conversion between array and list_Haiyang_Duan-CSDNblog

Numpy initialization of python data analysis (1)

Conversion of tensor and other data structures in pytorch (int, list, array)

[PyTorch study notes] 3: A variety of methods to create Tensor_LauZyHou's notes-CSDNblog_Create tensor

Guess you like

Origin blog.csdn.net/qq_41554005/article/details/115001010