python说句心里话e_Python Tensorflow exp()用法及代码示例

Tensorflow是Google开发的开源机器学习库。它的应用之一是开发深度神经网络。

模块tensorflow.math为许多基本的数学运算提供支持。功能tf.exp()[别名tf.math.exp]为Tensorflow中的 index 函数提供支持。它期望以复数形式输入为

或浮点数。输入类型为张量,如果输入包含多个元素,则将计算按元素的 index 值,

用法:tf.exp(x, name=None) or tf.math.exp(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 exp function and

# storing the result in 'b'

b = tf.exp(a, name ='exp')

# 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("exp:0", shape=(5, ), dtype=float32)

Output:[0.60653067 0.9048374 1. 1.105171 1.6487212 ]

代码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 21 with values from -10 to 10

a = np.linspace(-10, 10, 21)

# Applying the exponential function and

# storing the result in 'b'

b = tf.exp(a, name ='exp')

# 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.show()

输出:

Input:[-10. -9. -8. -7. -6. -5. -4. -3. -2. -1. 0. 1. 2. 3.

4. 5. 6. 7. 8. 9. 10.]

Output:[4.53999298e-05 1.23409804e-04 3.35462628e-04 9.11881966e-04

2.47875218e-03 6.73794700e-03 1.83156389e-02 4.97870684e-02

1.35335283e-01 3.67879441e-01 1.00000000e+00 2.71828183e+00

7.38905610e+00 2.00855369e+01 5.45981500e+01 1.48413159e+02

4.03428793e+02 1.09663316e+03 2.98095799e+03 8.10308393e+03

2.20264658e+04]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值