如何在PyTorch中计算张量元素的对数(附例子)

1,450 阅读4分钟

"在这个PyTorch教程中,我们将看到如何在一个给定的张量上执行对数函数。 PyTorch是一个开源的框架,可以用Python编程语言。

张量是一个多维数组,用于存储数据。所以为了使用张量,我们必须导入Torch模块。 要创建张量,使用的方法是tensor()"

语法

torch.tensor(data)

其中data是一个多维数组。

log()

PyTorch中的log()是用来返回张量对象中所有元素的自然对数的。它只需要一个参数。

语法

torch.log(tensor_object)

参数

tensor_object是输入的张量。

例子1

在这个例子中,我们将创建一个有3行5列的张量,并对其应用log()。

#import torch module

import torch

 

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5)

 

#display

print(data)

 

print()

 

#get logarithmic values

print("logarithmic values:")

print(torch.log(data))

输出

tensor([[-1.0134, -0.0345, 0.0841, 0.7704, 0.3895],

[ 0.5293, -0.9141, 0.4486, -1.1050, -0.1396],

[-2.7476, -1.6378, -0.3021, 0.0936, 1.9816]])

logarithmic values:

tensor([[ nan, nan, -2.4762, -0.2608, -0.9429],

[-0.6361, nan, -0.8017, nan, nan],

[ nan, nan, nan, -2.3682, 0.6839]])

我们可以看到,张量中所有元素的自然对数值都被返回。

例2

用5*5矩阵创建张量并返回自然对数值。

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

#display

print(data)

 

print()

 

#get logarithmic values

print("logarithmic values:")

print(torch.log(data))

输出

tensor([[-0.2143, 0.4640, -0.7694, 0.2063, 0.1471],

[-0.9600, 0.3431, 0.0933, -0.7847, -0.6198],

[ 1.9533, 0.7456, -0.8035, -0.2091, -2.1858],

[-0.3841, 0.4142, -1.6795, -1.3310, 1.5622],

[ 0.3093, 0.6724, 0.5488, -1.3811, 1.6062]])

logarithmic values:

tensor([[ nan, -0.7679, nan, -1.5782, -1.9169],

[ nan, -1.0698, -2.3719, nan, nan],

[ 0.6695, -0.2936, nan, nan, nan],

[ nan, -0.8815, nan, nan, 0.4461],

[-1.1735, -0.3969, -0.6001, nan, 0.4739]])

我们可以看到,张量中所有元素的自然对数值被返回。

log10()

PyTorch中的log10()用于返回张量对象中的所有元素的以10为底的对数。它只需要一个参数。

语法

torch.log10(tensor_object)

参数

tensor_object是输入的张量。

例子1

在这个例子中,我们将创建一个有3行和5列的3维张量,并对其应用log10()。

#import torch module

import torch

 

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5)

 

#display

print(data)

 

print()

 

#get logarithmic values to the base 10

print("logarithmic values to the base 10:")

print(torch.log10(data))

输出

tensor([[ 0.1137, 1.8604, 0.1547, 0.1092, 0.0385],

[-1.2716, 1.8937, -0.4259, 0.4512, 0.5377],

[-1.3074, 2.2634, 1.0972, -0.3502, 0.4971]])

logarithmic values to the base 10:

tensor([[-0.9441, 0.2696, -0.8105, -0.9617, -1.4140],

[ nan, 0.2773, nan, -0.3456, -0.2695],

[ nan, 0.3548, 0.0403, nan, -0.3035]])

例2

用5*5的矩阵创建张量,并返回以10为底的对数值。

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

 

#display

print(data)

 

print()

 

#get logarithmic values to the base 10

print("logarithmic values to the base 10:")

print(torch.log10(data))

输出

tensor([[-0.2903, -0.1354, -0.7794, -0.5695, -0.7214],

[ 0.5197, 0.5463, 1.4539, 0.0285, -0.7019],

[-0.0714, -1.2804, 0.0606, 1.1813, 0.9769],

[ 0.2130, 1.1354, 0.2970, -0.2755, -0.0466],

[ 2.8192, -0.9078, 0.5023, 1.1128, 0.3141]])

logarithmic values to the base 10:

tensor([[ nan, nan, nan, nan, nan],

[-0.2842, -0.2626, 0.1625, -1.5455, nan],

[ nan, nan, -1.2177, 0.0724, -0.0101],

[-0.6717, 0.0551, -0.5273, nan, nan],

[ 0.4501, nan, -0.2990, 0.0464, -0.5029]])

我们可以看到,张量中所有元素的以10为底的对数值被返回。

log2()

PyTorch中的log2()用于返回张量对象中所有元素的以2为底的对数。它只需要一个参数。

语法

torch.log2(tensor_object)

参数

tensor_object是输入的张量。

例子1

在这个例子中,我们将创建一个有3行和5列的3维张量,并对其应用log2()。

#import torch module

import torch

 

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5)

 

#display

print(data)

 

print()

 

#get logarithmic values to the base 2

print("logarithmic values to the base 2:")

print(torch.log2(data))

输出

tensor([[-0.0242, 0.6124, -1.2847, -0.2737, 1.2455],

[-0.5786, -0.1747, 0.6064, -0.5265, 0.3504],

[-0.3898, 0.5609, -0.0565, 0.5324, 0.0105]])

logarithmic values to the base 2:

tensor([[ nan, -0.7075, nan, nan, 0.3168],

[ nan, nan, -0.7216, nan, -1.5128],

[ nan, -0.8342, nan, -0.9095, -6.5752]])

我们可以看到,张量中所有元素的对数值都是以2为基数返回的。

例2

用5*5的矩阵创建张量,并返回以2为底的对数值。

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

 

#display

print(data)

 

print()

 

#get logarithmic values to the base 2

print("logarithmic values to the base 2:")

print(torch.log2(data))

输出

tensor([[ 3.0918, 0.2328, 0.6354, -0.6991, 2.1373],

[-1.2590, -1.5860, -0.1142, -0.1805, -1.9556],

[ 1.2391, 1.0197, 0.1663, 0.9892, -1.4073],

[ 0.0174, 0.8185, 0.3453, -0.7556, 1.0040],

[-1.0775, 0.4131, -0.7916, -0.9372, 0.1482]])

logarithmic values to the base 2:

tensor([[ 1.6285e+00, -2.1029e+00, -6.5418e-01, nan, 1.0958e+00],

[ nan, nan, nan, nan, nan],

[ 3.0926e-01, 2.8108e-02, -2.5882e+00, -1.5649e-02, nan],

[-5.8447e+00, -2.8896e-01, -1.5339e+00, nan, 5.7767e-03],

[ nan, -1.2754e+00, nan, nan, -2.7546e+00]])

我们可以看到,张量中所有元素的对数值都是以2为基数返回的。

与CPU一起工作

如果你想在CPU上运行一个对数函数,那么我们必须用cpu()函数创建一个张量。这将在CPU机器上运行。

当我们创建一个张量时,此时,我们可以使用cpu()函数。

语法

torch.tensor(data).cpu()

例子

在cpu上用5*5的矩阵创建张量,并返回自然对数值、以2为底的对数值和以10为底的对数值。

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5).cpu()

 

#display

print(data)

 

print()

#get natural log values

print("natural log values: ")

print(torch.log(data))

print()

#get logarithmic values to the base 2

print("logarithmic values to the base 2:")

print(torch.log2(data))

print()

#get logarithmic values to the base 10

print("logarithmic values to the base 10:")

print(torch.log10(data))

输出

tensor([[-0.2807, 0.0260, 0.3326, -0.1958, 2.7080],

[ 1.3534, -0.2371, 0.0085, 0.1877, 1.4870],

[ 1.2967, 0.4262, -0.6323, 0.4446, 3.0513],

[ 0.4478, -0.0436, -0.4577, 1.3098, 0.7293],

[-0.4575, -1.4020, -0.9323, -0.4406, 0.5844]])

natural log values:

tensor([[ nan, -3.6494, -1.1009, nan, 0.9962],

[ 0.3026, nan, -4.7711, -1.6731, 0.3968],

[ 0.2598, -0.8529, nan, -0.8107, 1.1156],

[-0.8034, nan, nan, 0.2699, -0.3157],

[ nan, nan, nan, nan, -0.5371]])

logarithmic values to the base 2:

tensor([[ nan, -5.2650, -1.5882, nan, 1.4372],

[ 0.4366, nan, -6.8833, -2.4138, 0.5724],

[ 0.3748, -1.2304, nan, -1.1696, 1.6094],

[-1.1591, nan, nan, 0.3893, -0.4554],

[ nan, nan, nan, nan, -0.7749]])

logarithmic values to the base 10:

tensor([[ nan, -1.5849, -0.4781, nan, 0.4327],

[ 0.1314, nan, -2.0721, -0.7266, 0.1723],

[ 0.1128, -0.3704, nan, -0.3521, 0.4845],

[-0.3489, nan, nan, 0.1172, -0.1371],

[ nan, nan, nan, nan, -0.2333]])

我们可以看到,张量中所有元素的以2为底的对数值、以10为底的对数值和自然对数值都被返回。

总结

torch.log()是一个简单的对数函数,用于返回张量对象中所有元素的自然对数。log10()用于返回张量对象中所有元素的以10为底的对数,而log2()用于返回张量对象中所有元素的以2为底的对数。我们在使用cpu时也讨论了如何应用这些函数。