[TensorFlow系列-8]:TensorFlow基础 - 张量的取整运算

本文详细介绍了TensorFlow中针对张量的取整运算,包括向下取整`floor()`、向上取整`ceil()`、四舍五入`round()`以及取余数`mod()`等操作。通过实例展示了每个函数的使用方法和结果,帮助读者理解如何在TensorFlow中进行数值处理。
摘要由CSDN通过智能技术生成

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

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


目录

 第1 章 Tensor运算概述

1.1 概述

1.3  “in place“运算 

1.4 Tensor的广播机制: 不同维度的tensor实例运算

1.5 环境准备

1.6 取整运算概述

第2章 向下取整:floor()

第3章 向上取整:ceil()

第4章 四舍五入的函数:around() 

第5章 裁剪取整数部分:trunc() -- 不支持

第6章 取小数部分:frac() -- 不支持

第7章 取余数运算:mod()


 第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 取整运算概述

第2章 向下取整:floor()

floor() 返回小于或者等于指定表达式的最大整数,即向取整,即小于该数的、与该数最接近的整数。

#实例

a = tf.constant([0.24, 1.0, 2.1,  3, 4.55])  
print  ('原数组:')
print (a)
print ('\n')

print ('运算后:')
print (tf.floor(a))
print (tf.math.floor(a))
#输出结果为:

原数组:
tf.Tensor([0.24 1.   2.1  3.   4.55], shape=(5,), dtype=float32)


运算后:
tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)
tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)

第3章 向上取整:ceil()

ceil() 返回大于或者等于指定表达式的最小整数,即向上取整。

#实例

a = tf.constant([0.24, 1.0, 2.1,  3, 4.55])  
print  ('原数组:')
print (a)
print ('\n')

print ('运算后:')
# print (tf.ceil(a)) # 不支持
print (tf.math.ceil(a)) 
#输出结果为:

原数组:
tf.Tensor([0.24 1.   2.1  3.   4.55], shape=(5,), dtype=float32)


运算后:
tf.Tensor([1. 1. 3. 3. 5.], shape=(5,), dtype=float32)

第4章 四舍五入的函数:around() 

around() 函数返回指定数字的四舍五入值。

#实例

a = tf.constant([0.24, 1.0, 2.1,  3, 4.55])  
print  ('原数组:')
print (a)
print ('\n')

print ('运算后:')
print (tf.round(a)) 
print (tf.math.round(a)) 
#输出结果为:

原数组:
tf.Tensor([0.24 1.   2.1  3.   4.55], shape=(5,), dtype=float32)


运算后:
tf.Tensor([0. 1. 2. 3. 5.], shape=(5,), dtype=float32)
tf.Tensor([0. 1. 2. 3. 5.], shape=(5,), dtype=float32)

第5章 裁剪取整数部分:trunc() -- 不支持

trunc() 函数返回指定数字的整数部分

a = tf.constant([0.24, 1.0, 2.1,  3, 4.55])  
print  ('原数组:')
print (a)
print ('\n')

print ('运算后:')
# print (tf.trunc(a))  #不支持
# print (tf.math.trunck(a))  #不支持
#输出结果:

原数组:
tf.Tensor([0.24 1.   2.1  3.   4.55], shape=(5,), dtype=float32)


运算后:

第6章 取小数部分:frac() -- 不支持

frac() 函数返回指定数字的分数

a = tf.constant([0.24, 1.0, 2.1,  3, 4.55])  
print  ('原数组:')
print (a)
print ('\n')

print ('运算后:')
# print (tf.math.frac(a))  #不支持
输出结果:

原数组:
tf.Tensor([0.24 1.   2.1  3.   4.55], shape=(5,), dtype=float32)


运算后:

第7章 取余数运算:mod()

numpy.mod() 计算输入数组中相应元素的相除后的余数

函数 numpy.remainder() 也产生相同的结果。

实例

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(a.mode(b))  # 不支持
print(tf.math.mod(a,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([0 0 1 2 0], shape=(5,), dtype=int32)
tf.Tensor([0 0 1 2 0], shape=(5,), dtype=int32)
原数据a
tf.Tensor([10 10 10 10 10], shape=(5,), dtype=int32)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

文火冰糖的硅基工坊

你的鼓励是我前进的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值