Tensorflow是Google開發的開源機器學習庫。它的應用之一是開發深度神經網絡。
模塊tensorflow.math
為許多基本的數學運算提供支持。函數tf.log()
[別名tf.math.log
]支持Tensorflow中的自然對數函數。它期望以複數形式輸入為或浮點數。輸入類型為張量,如果輸入包含多個元素,則將計算元素對數,。
用法:tf.log(x, name=None) or tf.math.log(x, name=None)
參數:
x:類型為bfloat16,half,float32,float64,complex64或complex128的張量。
name(可選):操作的名稱。
返回類型:與x具有相同大小和類型的張量。
代碼1:
# Importing the Tensorflow library
import tensorflow as tf
# A constant vector of size 5
a = tf.constant([-0.5, -0.1, 0, 0.1, 0.5], dtype = tf.float32)
# Applying the log function and
# storing the result in 'b'
b = tf.log(a, name ='log')
# Initiating a Tensorflow session
with tf.Session() as sess:
print('Input type:', a)
print('Input:', sess.run(a))
print('Return type:', b)
print('Output:', sess.run(b))
輸出:
Input type:Tensor("Const:0", shape=(5, ), dtype=float32) Input:[-0.5 -0.1 0. 0.1 0.5] Return type:Tensor("log:0", shape=(5, ), dtype=float32) Output:[ nan nan -inf -2.3025851 -0.6931472]
表示不存在負值的自然對數,並且表示隨著輸入接近零,它接近負無窮大。
代碼2:可視化
# Importing the Tensorflow library
import tensorflow as tf
# Importing the NumPy library
import numpy as np
# Importing the matplotlib.pylot function
import matplotlib.pyplot as plt
# A vector of size 20 with values from 0 to 1 and 1 to 10
a = np.append(np.linspace(0, 1, 10), np.linspace(1, 10, 10))
# Applying the logarithmic function and
# storing the result in 'b'
b = tf.log(a, name ='log')
# Initiating a Tensorflow session
with tf.Session() as sess:
print('Input:', a)
print('Output:', sess.run(b))
plt.plot(a, sess.run(b), color = 'red', marker = "o")
plt.title("tensorflow.abs")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid()
plt.show()
輸出:
Input:[ 0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556 0.66666667 0.77777778 0.88888889 1. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. ] Output:[ -inf -2.19722458 -1.5040774 -1.09861229 -0.81093022 -0.58778666 -0.40546511 -0.25131443 -0.11778304 0. 0. 0.69314718 1.09861229 1.38629436 1.60943791 1.79175947 1.94591015 2.07944154 2.19722458 2.30258509]
相關用法
- Python Tensorflow cos()用法及代碼示例
- Python Tensorflow sin()用法及代碼示例
- Python Tensorflow abs()用法及代碼示例
- Python Tensorflow tan()用法及代碼示例
- Python Tensorflow exp()用法及代碼示例
- Python Tensorflow atan()用法及代碼示例
- Python Tensorflow acos()用法及代碼示例
- Python Tensorflow asin()用法及代碼示例
- Python Tensorflow sinh()用法及代碼示例
- Python Tensorflow reciprocal()用法及代碼示例
- Python Tensorflow cosh()用法及代碼示例
- Python Tensorflow logical_not()用法及代碼示例
- Python Tensorflow asinh()用法及代碼示例
- Python Tensorflow acosh()用法及代碼示例
注:本文由純淨天空篩選整理自sanskar27jain大神的英文原創作品 Python | Tensorflow log() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。