[TensorFlow系列-8]:TensorFlow基础 - 张量的取整运算
Posted 文火冰糖的硅基工坊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[TensorFlow系列-8]:TensorFlow基础 - 张量的取整运算相关的知识,希望对你有一定的参考价值。
作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing
本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119620874
目录
1.4 Tensor的广播机制: 不同维度的tensor实例运算
第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
以上是关于[TensorFlow系列-8]:TensorFlow基础 - 张量的取整运算的主要内容,如果未能解决你的问题,请参考以下文章