tensorflow基础入门3-常见操作

  • 常见操作 : 加、减、乘、除
import tensorflow as tf

# 创建a,b两个张量
a = tf.constant([[1,2,3,4],[5,6,7,8]])
b = tf.constant([[10,20,30,40],[50,60,70,80]])

# tf.add()
tf_add = tf.add(a,b)
print('加法:',tf_add)

# tf.subtract()
tf_sub = tf.subtract(a,b)
print('减法:',tf_sub)

# tf.multiply()
tf_mul = tf.multiply(a,b)
print('乘法:',tf_mul)

# tf.divide()
tf_div = tf.divide(b,a)
print('除法:',tf_div)



>>>  加法: tf.Tensor(
                     [[11 22 33 44]
                      [55 66 77 88]], shape=(2, 4), dtype=int32)

>>>  减法: tf.Tensor(
                     [[ -9 -18 -27 -36]
                      [-45 -54 -63 -72]], shape=(2, 4), dtype=int32)

>>>  乘法: tf.Tensor(
                     [[ 10  40  90 160]
                      [250 360 490 640]], shape=(2, 4), dtype=int32)

>>>  除法: tf.Tensor(
                     [[10. 10. 10. 10.]
                      [10. 10. 10. 10.]], shape=(2, 4), dtype=float64) 
  • 注意:a,b维度应该保持一致,否则代码会报错
import tensorflow as tf

a = tf.constant([[1,2,3,4],[5,6,7,8]])
b = tf.constant([[10,20,],[50,60,70,80]])

tf_add = tf.add(a,b)
print('加法:',tf_add)


  • 如下所示,a,b维度不一致,代码运行错误
Traceback (most recent call last):
  File "D:\pycharm\pythonProject2\tensor\a.py", line 5, in <module>
    b = tf.constant([[10,20,],[50,60,70,80]])
  File "D:\anaconda\envs\tf2x\lib\site-packages\tensorflow\python\framework\constant_op.py", line 261, in constant
    return _constant_impl(value, dtype, shape, name, verify_shape=False,
  File "D:\anaconda\envs\tf2x\lib\site-packages\tensorflow\python\framework\constant_op.py", line 270, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "D:\anaconda\envs\tf2x\lib\site-packages\tensorflow\python\framework\constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Can't convert non-rectangular Python sequence to Tensor.
  • 常见操作 : 求绝对值、取负、开方等
import tensorflow as tf

a = tf.constant([1,-2,-3],dtype=tf.float32)
b = a = tf.constant([4,-7,-6],dtype=tf.float32)

# 求绝对值
tf_abs = tf.abs(a)
print('绝对值:',tf_abs)

# 取负
tf_neg = tf.negative(a)
print('取负:',tf_neg)

# 返回符号:a>0,返回1;a<0,返回-1;a==0,返回0
tf_sign = tf.sign(a)
print('返回:',tf_sign)

# 计算平方
tf_squ = tf.square(a)
print('平方:',tf_squ)

# 开根号
tf_sqrt = tf.sqrt(a)
print('开根号:',tf_sqrt)

# 幂次方,即x的y次方
tf_pow = tf.pow(a,b)
print('幂次方:',tf_pow)

# 最大值
tf_max = tf.maximum(a,b)
print('最大值:',tf_max)
>>>  绝对值: tf.Tensor([4. 7. 6.], shape=(3,), dtype=float32)

>>>  取负: tf.Tensor([-4.  7.  6.], shape=(3,), dtype=float32)

>>>  返回: tf.Tensor([ 1. -1. -1.], shape=(3,), dtype=float32)

>>>  平方: tf.Tensor([16. 49. 36.], shape=(3,), dtype=float32)

>>>  开根号: tf.Tensor([ 2. nan nan], shape=(3,), dtype=float32)

>>>  幂次方: tf.Tensor([ 2.5600000e+02 -1.2142656e-06  2.1433470e-05], shape=(3,), dtype=float32)

>>>  最大值: tf.Tensor([ 4. -7. -6.], shape=(3,), dtype=float32)
  • 注意:创建的张量 a 默认类型是 int32
import tensorflow as tf

a = tf.constant([1,-2,-3])
print(a.dtype)
<dtype: 'int32'>
  • 存在的问题:使用tf.sqrt()代码报错
import tensorflow as tf

a = tf.constant([1,-2,-3])
print(a.dtype)

# 开根号
tf_sqrt = tf.sqrt(a)
print('开根号:',tf_sqrt)
>>>   File "D:\pycharm\pythonProject2\tensor\a.py", line 8, in <module>
    tf_sqrt = tf.sqrt(a)
  • 解决方法:改变 a 的 dtype
import tensorflow as tf

a = tf.constant([1,-2,-3])
print(a.dtype)
a = tf.constant([1,-2,-3],dtype=tf.float32)
print(a.dtype)

# 开根号
tf_sqrt = tf.sqrt(a)
print('开根号:',tf_sqrt)
>>>  <dtype: 'int32'>
>>>  <dtype: 'float32'>
>>>  开根号: tf.Tensor([ 1. nan nan], shape=(3,), dtype=float32)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值