官术网_书友最值得收藏!

Converting between tensors and NumPy arrays

Converting a NumPy array is as simple as performing an operation on it with a torch tensor. The following code should make this clear:

We can see the result of the type torch tensor. In many cases, we can use NumPy arrays interchangeably with tensors and always be sure the result is a tensor. However, there are times when we need to explicitly create a tensor from an array. This is done with the torch.from_numpy function:

To convert from a tensor to a NumPy array, simply call the torch.numpy() function:

Notice that we use Python's built-in type() function, as in type(object), rather than the tensor.type() we used previously. The NumPy arrays do not have a type attribute. Another important thing to understand is that NumPy arrays and PyTorch tensors share the same memory space. For example, see what happens when we change a variables value as demonstrated by the following code:

Note also that when we print a tensor, it returns a tuple consisting of the tensor itself and also its dtype, or data type attribute. It's important here because there are certain dtype arrays that cannot be turned into tensors. For example, consider the following code:

 This will generate an error message telling us that only supported dtype are able to be converted into tensors. Clearly, int8 is not one of these supported types. We can fix this by converting our int8 array to an int64 array before passing it to torch.from_numpy. We do this with the numpy.astype function, as the following code demonstrates:

It is also important to understand how numpy dtype arrays convert to torch dtype. In the previous example, numpy int32 converts to IntTensor. The following table lists the torch dtype and their numpy equivalents:

 

The default dtype for tensors is FloatTensor; however, we can specify a particular data type by using the tensor's dtype attribute. For an example, see the following code:

主站蜘蛛池模板: 忻城县| 巴林左旗| 绥滨县| 屯留县| 奉节县| 枣强县| 分宜县| 湖南省| 岫岩| 涟水县| 仁寿县| 青海省| 肃宁县| 邵阳市| 思南县| 论坛| 滦南县| 冕宁县| 云浮市| 山东省| 晋江市| 富阳市| 无极县| 承德县| 棋牌| 湘潭市| 乾安县| 大同市| 永登县| 衡南县| 柏乡县| 兴城市| 武宣县| 称多县| 乌拉特中旗| 襄汾县| 新余市| 湄潭县| 大洼县| 孝昌县| 新巴尔虎左旗|