tensorflow之算术运算符:tf.add,tf.subtract,tf.multiply,tf.scalar_mul,tf.div

1.tf.add

tf.add或tf.math.add:加法操作

tf.add(

    x,

    y,

    name=None

)

参数说明:

x:一个张量。必须是下列类型之一:bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string.

y:一个张量。类型必须同x。

name:操作的名字,可选参数

返回值:x+y

一个张量,类型同x

a=tf.constant(2)

b=tf.constant(5)

addOp=tf.add(a,b)

sess=tf.Session()

init=tf.global_variables_initializer()

sess.run(init)

print(sess.run(addOp))

print(addOp)

输出:

7

Tensor("Add_4:0", shape=(), dtype=int32)

2.tf.subtract

减法操作

tf.subtract(

    x,

    y,

    name=None

)

参数说明:

X:一个张量。必须是下列类型之一:bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128

y:一个张量。类型同x.

name:张量操作的名字,可选参数。

返回值:x-y

一个张量,类型同x

Subtract支持广播操作

subtractOp=tf.subtract(a,b)

print(sess.run(subtractOp))

print(subtractOp)

输出:

-3

Tensor("Sub:0", shape=(), dtype=int32)

3.tf.multiply

乘法操作

tf.multiply(

    x,

    y,

    name=None

)

参数说明:

X:一个张量。必须是下列类型之一:bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128

y:一个张量。类型同x.

name:张量操作的名字,可选参数。

返回值:x*y

一个张量,类型同x

multiply=tf.multiply(a,b,name='mul')

print(sess.run(multiply))

print(multiply)

输出:

10

Tensor("mul:0", shape=(), dtype=int32)

4.tf.scalar_mul

标量和张量相乘

tf.scalar_mul(

    scalar,

    x

)

参数说明:

scalar:标量

x:一个张量

返回值:sclar*x

scalar_mul_op=tf.scalar_mul(4,a)

print(sess.run(scalar_mul_op))

print(scalar_mul_op)

输出:

8

Tensor("mul_4:0", shape=(), dtype=int32)

5.tf.div

除法操作,如果其中一个是浮点数,则结果是浮点类型,否则是整数类型

tf.div(

    x,

    y,

    name=None

)

 

参数说明:

x:实数数值类型,分子

y:实数类型,分母

name:操作名字。

返回值:

x/y

divOp=tf.div(1.0,2)

print(sess.run(divOp))

print(divOp)

输出:

0.5

Tensor("div:0", shape=(), dtype=float32)

 

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值