import tensorflow as tf
# 查看tensorflow的版本
print("TensorFlow Version:{}".format(tf.__version__))
输出结果:
TensorFlow Version:2.3.0
【TensorFlow2.0 低阶API基础编程】
【tf.constant提供了常量的声明功能,示例代码如下】
a = tf.constant(9)
print(a,a.numpy())
输出结果:
tf.Tensor(9, shape=(), dtype=int32) 9
【tf.Variable提供了变量的声明功能,示例代码如下】
# 声明一个Python变量
a1 = 9
# 声明一个0阶Tensor变量
a2 = tf.Variable(9)
# 声明一个1阶Tensor变量,即数组
a3 = tf.Variable([0,1,2])
print(a1,a2,a3,a3.numpy())
输出结果:
9
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=9> <tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([0, 1, 2])>
[0, 1, 2]
【tf.reshape提供了多阶Tensor的形状变换功能,示例代码如下】
b = tf.Variable([[0,1,2],[3,4,5]])
print(b.shape)
# 对b的形状进行变换,变换为(3,2)
b1 = tf.reshape(b,[3,2])
print(b1.shape,b1.numpy())
输出结果:
(2, 3)
(3, 2)
[[0 1]
[2 3]
[4 5]]
【tf.math.reduce_mean提供了对Tensor求平均值的功能,输出数据类型会根据输入类型来确定】
# 使用该API可以配置的参数如下:
# input_tensor:配置输入的Tensor
# axis:配置按行或列求平均,默认按行
# keepdims:配置输出结果是否保持二维矩阵特性
# name:配置操作的名称
c = tf.constant([1,2.,3,4,5,6,7.])
print(c.dtype)
print(tf.math.reduce_mean(c))
d = tf.constant([[1,2,1],[9,1,5]])
print(d.dtype)
print(tf.math.reduce_mean(d))
输出结果:
<dtype: 'float32'>
tf.Tensor(4.0, shape=(), dtype=float32)
<dtype: 'int32'>
tf.Tensor(3, shape=(), dtype=int32)
# int型,均值取整
【tf.ramdom.normal可以随机生成一个Tensor,其值符合正态分布】
# 使用该API有如下参数需要配置:
# shape:配置生成的Tensor的维度
# mean:配置正态分布的均值
# stddev:配置正态分布的标准差
# seed:配置正态分布的随机数种子
# dtype:配置生成的Tensor数据类型
e = tf.random.normal(shape=[2,3],mean=2)
print(e.numpy())
输出结果:
[[0.92077017 1.3909743 3.6273527 ]
[0.24692464 2.4902384 0.6180022 ]]
【tf.random.uniform可以随机生成一个Tensor,其值符合均匀分布】
# 使用该API有如下参数需要配置
# shape:配置生成的Tensor的维度
# minval:配置生成数值的最小值
# maxval:配置生成数值的最大值
# seed:配置正态分布的随机数种子
# dtype:配置生成的Tensor数据类型
f = tf.random.uniform(shape=[2,3],minval=1,maxval=9,seed=8,dtype=tf.int32)
print(f.numpy())
输出结果:
[[5 3 7]
[6 5 7]]
【tf.transpose提供了矩阵的转置功能】
# 使用该API时配置的参数如下:
# a:输入需要转置的矩阵
# perm:配置转置后矩阵的形状
# conjugate:当输入的矩阵是复数时,需要配置为True
# name:配置本次操作的名称
x = tf.constant([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
g = tf.transpose(x,perm=[0,2,1])
print(g.numpy())
输出结果:
[[[ 1 4]
[ 2 5]
[ 3 6]]
[[ 7 10]
[ 8 11]
[ 9 12]]]
【tf.math.argmax提供了一个返回一数组内最大值对应索引的功能】
# 使用该API时有如下参数可以配置
# input:配置输入的数组
# axis:配置计算的维度
# output_type:配置输出的格式
# name:配置操作的名称
y = tf.constant([1,2,3,4,5])
h = tf.math.argmax(y)
print(h.numpy())
输出结果:
4
【tf.expand_dims的作用是在输入的Tensor中增加一个维度,比如t是一个维度为[2]的Tensor,那么tf.expand_dims(t,0)的维度就会变成[1,2]】
# 使用这个API时需要配置如下参数
# input:配置输入的Tensor
# axis:配置需要添加维度的下标,比如[2,1]需要在2和1之间添加,则配置值为1
# name:配置输出Tensor的名称
# 初始化一个维度为(3,1)的Tensor
i = tf.constant([[1],[2],[3]])
print(i.shape)
# 为i增加一个维度,使其维度变为(1,3,1),数字表示增加维度的位置
ii = tf.expand_dims(i,0)
print(ii.shape)
print(ii)
输出结果:
(3, 1)
(1, 3, 1)
tf.Tensor(
[[[1]
[2]
[3]]], shape=(1, 3, 1), dtype=int32)
【tf.concat的作用是将多个Tensor在同一个维度上进行连接】
# 使用该API时需要进行如下参数配置
# values:配置Tensor的列表或者是一个单独的Tensor
# axis:配置按行或按列连接,axis=0表示按行连接,axis=1表示按列连接
# name:配置运算操作的名称
j1 = tf.constant([[2,3,4],[4,5,6],[2,3,4]])
j2 = tf.constant([[1,2,2],[6,7,9],[2,3,2]])
j = tf.concat([j1,j2],axis=0)
print(j.numpy())
输出结果:
[[2 3 4]
[4 5 6]
[2 3 4]
[1 2 2]
[6 7 9]
[2 3 2]]
【tf.bitcast提供了数据类型转换功能】
# 使用该API时需要进行如下参数配置
# input:配置需要进行类型转换的Tensor,Tensor的类型可以为bfloat16,half,float32,float64,int64,int32,uint8,uint16,uint32,uint64,int8,int16,complex64,complex128,qint8,quint8,qint16,quint16,qint32。
# type:配置转换后的数据类型,可以选择的类型包括tf.bfloat16,tf.half,tf.float32,tf.float64,tf.int64,tf.int32,tf.uint8,tf.uint16,tf.uint32,tf.uint64,tf.int8,tf.int16,tf.complex64,tf.complex128,tf.qint8,tf.quint8,tf.qint16,tf.quint16,tf.qint32。
# name:配置运算操作的名称
k = tf.constant(32.0)
kk = tf.bitcast(a,type=tf.int32)
print(k.dtype)
print(kk.dtype)
输出结果:
<dtype: 'float32'>
<dtype: 'int32'>
TensorFlow学习笔记之一些低阶API
最新推荐文章于 2024-01-04 14:32:29 发布