tf.square()、tf.pow()、tf.math.pow()函数介绍和示例

tf.square()、tf.pow()、tf.math.pow()函数介绍和示例

1. tf.square()

释义:对各元素求平方

示例

import tensorflow as tf

X = tf.constant([1, 2, 3, 4], dtype=tf.float32, name=None)
Y = tf.square(X)         # 平方操作

with tf.Session() as sess:
    print(sess.run(Y))
[ 1.  4.  9. 16.]
2. tf.pow(x, y, name=None)

释义:幂值计算操作,即 x^y

示例

import tensorflow as tf

x = tf.constant(2, dtype=tf.float32)
y = tf.constant(3, dtype=tf.float32)
z = tf.pow(x, y)                              # 标量^标量操作

X1 = tf.constant(3, dtype=tf.float32)
Y1 = tf.constant([1, 2, 3], dtype=tf.float32)        
Z1 = tf.pow(X1, Y1)                           # 标量^向量操作(矩阵同理)

X2 = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)
Y2 = tf.constant([[2,2,2],[3,3,3]], dtype=tf.float32)
Z2 = tf.pow(X2, Y2)                           # 矩阵^矩阵操作(向量同理),对应位置的值进行幂值计算

with tf.Session() as sess:
    print(sess.run(z))
    print('='*30)
    
    print(sess.run(Z1))
    print('='*30)
    
    print(sess.run(Z2))
8.0
==============================
[ 3.  9. 27.]
==============================
[[  1.   4.   9.]
 [ 64. 125. 216.]]
3. tf.math.pow(x, y, name=None)

释义:同 tf.pow(x, y, name=None)

示例

import tensorflow as tf

x = tf.constant(2, dtype=tf.float32)
y = tf.constant(3, dtype=tf.float32)
z = tf.math.pow(x, y)                              # 标量^标量操作

X1 = tf.constant(3, dtype=tf.float32)
Y1 = tf.constant([1, 2, 3], dtype=tf.float32)        
Z1 = tf.math.pow(X1, Y1)                           # 标量^向量操作(矩阵同理)

X2 = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)
Y2 = tf.constant([[2,2,2],[3,3,3]], dtype=tf.float32)
Z2 = tf.math.pow(X2, Y2)                           # 矩阵^矩阵操作(向量同理),对应位置的值进行幂值计算

with tf.Session() as sess:
    print(sess.run(z))
    print('='*30)
    
    print(sess.run(Z1))
    print('='*30)
    
    print(sess.run(Z2))
8.0
==============================
[ 3.  9. 27.]
==============================
[[  1.   4.   9.]
 [ 64. 125. 216.]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值