[TensorFlow系列-7]:TensorFlow基础 - 张量的算术运算

 作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119620066


目录

 第1章 Tensor运算概述

1.1 概述

1.3  “in place“运算 

1.4 Tensor的广播机制

1.5 环境准备

1.6 算术运算概述:加、减、系数乘、系数除

第2章 加法运算:add

第3章 减法运算:subtract

第4章 乘法运算:multiply

第5章除法运算:divide

第6章 倒数运算 /

第7章 幂运算:power()

第8章 广播机制




 第1章 Tensor运算概述

1.1 概述

TensorFlow提供了大量的张量运算,基本上可以对标Numpy多维数组的运算,以支持对张量的各种复杂的运算。

这些操作运算中大多是对数组中每个元素执行相同的函数运算,并获得每个元素函数运算的结果序列,这些序列生成一个新的同维度的数组。

https://tensorflow.google.cn/tutorials/quickstart/beginner?hl=zh-cn

 1.2 运算分类

(1)算术运算:加、减、系数乘、系数除

(2)函数运算:sin,cos

(3)取整运算:上取整、下取整

(4)统计运算:最大值、最小值、均值

(5)线性代数运算

1.3  “in place“运算 

NA

1.4 Tensor的广播机制

实现不同维度tensor的扩展运算

1.5 环境准备

#环境准备
import numpy as np
import tensorflow as tf
print("hello world")
print("tensorflow version:", tf.__version__)

1.6 算术运算概述:加、减、系数乘、系数除

Tensor算术函数包含简单的加减乘除运算add()subtract()multiply() 和 divide()

算术运算后得到的结果依然是是相同维度的数组。

第2章 加法运算:add

#代码实例
a = tf.range(9)
print ('原数据a:')
print (a)
print ('\n')

b = tf.range(9)
print ('原数据b:')
print (b)
print ('\n')

print ('运算后数据:')
print (a+b)
print (tf.add(a,b))
#print (a.add(b))   #不支持
print ('\n')

print (a)
print ('\n')
输出结果为:

原数据a:
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)


原数据b:
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)


运算后数据:
tf.Tensor([ 0  2  4  6  8 10 12 14 16], shape=(9,), dtype=int32)
tf.Tensor([ 0  2  4  6  8 10 12 14 16], shape=(9,), dtype=int32)


tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)

第3章 减法运算:subtract

#代码实例

a = tf.range(1,10)
print ('原数据a:')
print (a)
print ('\n')

b = tf.range(0,9)
print ('原数据b:')
print (b)
print ('\n')

print ('运算后数据:')
print (a-b)
print (tf.subtract(a,b))
#print (a.subtract(b)) 不支持
print ('\n')

print (a)
print ('\n')
#输出结果

原数据a:
tf.Tensor([1 2 3 4 5 6 7 8 9], shape=(9,), dtype=int32)


原数据b:
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)


运算后数据:
tf.Tensor([1 1 1 1 1 1 1 1 1], shape=(9,), dtype=int32)
tf.Tensor([1 1 1 1 1 1 1 1 1], shape=(9,), dtype=int32)


tf.Tensor([1 2 3 4 5 6 7 8 9], shape=(9,), dtype=int32)

第4章 乘法运算:multiply

#代码实例

a = tf.constant([2,4,6,8,10,12])
print ('原数据a:')
print (a)
print ('\n')

b = tf.constant([2,4,6,8,10,12])
print ('原数据b:')
print (b)
print ('\n')

print ('运算后数据:')
print (a*b)
print (tf.multiply(a,b))
# print (a.multiply(b)) #不支持
print ('\n')

print (a)
print ('\n')
#输出结果

原数据a:
tf.Tensor([ 2  4  6  8 10 12], shape=(6,), dtype=int32)


原数据b:
tf.Tensor([ 2  4  6  8 10 12], shape=(6,), dtype=int32)


运算后数据:
tf.Tensor([  4  16  36  64 100 144], shape=(6,), dtype=int32)
tf.Tensor([  4  16  36  64 100 144], shape=(6,), dtype=int32)


tf.Tensor([ 2  4  6  8 10 12], shape=(6,), dtype=int32)

第5章除法运算:divide

#代码实例

a = tf.constant([2,4,6,8,10,12])
print ('原数据a:')
print (a)
print ('\n')

b = tf.constant([2,4,6,8,10,12])
print ('原数据b:')
print (b)
print ('\n')

print ('运算后数据:')
print (a/b)
print (tf.divide(a,b))
# print (a.divide(b)) # 不支持
print ('\n')

print (a)
print ('\n')
#输出结果

原数据a:
tf.Tensor([ 2  4  6  8 10 12], shape=(6,), dtype=int32)


原数据b:
tf.Tensor([ 2  4  6  8 10 12], shape=(6,), dtype=int32)


运算后数据:
tf.Tensor([1. 1. 1. 1. 1. 1.], shape=(6,), dtype=float64)
tf.Tensor([1. 1. 1. 1. 1. 1.], shape=(6,), dtype=float64)


tf.Tensor([ 2  4  6  8 10 12], shape=(6,), dtype=int32)

第6章 倒数运算 /

reciprocal() 函数返回参数逐元素的倒数。如 1/4 倒数为 4/1

#实例

a = tf.constant([2,4,5,8,10,20])
print("原数据a")
print(a)

#导数运算
print("运算数据")
print(1/a)
# print(tf.reciprocal(a)) #不支持
# print(a.reciprocal()) # 不支持

print("原数据")
print (a)
#输出结果为:

原数据a
tf.Tensor([ 2  4  5  8 10 20], shape=(6,), dtype=int32)
运算数据
tf.Tensor([0.5   0.25  0.2   0.125 0.1   0.05 ], shape=(6,), dtype=float64)
原数据
tf.Tensor([ 2  4  5  8 10 20], shape=(6,), dtype=int32)

第7章 幂运算:power()

power() 函数将第一个输入数组中的元素作为底数,计算它与第二个输入数组中相应元素的

#实例

a = tf.constant([10,10,10,10,10])
b = tf.constant([1,2,3,4,5])
print("原数据a")
print(a)
print("原数据b")
print(b)

#导数运算
print("运算后数据")
#print(a^b)
print(tf.pow(a,b))
# print(a.pow(b))  #不支持

print("原数据a")
print (a)
#输出结果为:

原数据a
tf.Tensor([10 10 10 10 10], shape=(5,), dtype=int32)
原数据b
tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)
运算后数据
tf.Tensor([    10    100   1000  10000 100000], shape=(5,), dtype=int32)
原数据a
tf.Tensor([10 10 10 10 10], shape=(5,), dtype=int32)

第8章 广播机制

当两个张量的维度不同,对他们进行运算时,需要对维度小的张量进行扩展,扩展成高纬度的张量,这个扩展的过程采用的是广播机制,即对低维度数据进行广播式(拷贝)扩展。

a = tf.range(9)
print ('原数据:')
print (a)

b = tf.reshape(a, [3,3])
print ('\n第一个数组:')
print (b)

print ('\n第二个数组:')
c = np.array([10,10,10])  
print (c)

print ('\n两个数组相加:')
print (np.add(b,c))
输出结果:

原数据:
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)

第一个数组:
tf.Tensor(
[[0 1 2]
 [3 4 5]
 [6 7 8]], shape=(3, 3), dtype=int32)

第二个数组:
[10 10 10]

两个数组相加:
[[10 11 12]
 [13 14 15]
 [16 17 18]]


作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119620066

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文火冰糖的硅基工坊

你的鼓励是我前进的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值