栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【深度学习 走进tensorflow2.0】TensorFlow 2.0 一些常用低阶API编程

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【深度学习 走进tensorflow2.0】TensorFlow 2.0 一些常用低阶API编程

在tensorflow 2.0中定义了很多低阶API,在日常编程中除了需要用到高阶API tf.keras 之外,也需要用到一些低阶API,下面一起来学习吧。

1、tf.constant 提供了常量的申明功能。

# -*- coding: utf-8 -*-

import tensorflow as tf

a=tf.constant(10)
print(a.numpy())

运行结果:

10
Process finished with exit code 0

2、tf.Variable 提供了变量的申明功能。

# -*- coding: utf-8 -*-

import tensorflow as tf

a=tf.constant(10)
print(a.numpy())


b=tf.Variable(7)
print(b.numpy())

运行结果:

10
7
Process finished with exit code 0

3、tf.reshape提供了多阶Tensor的形状变换功能。

import tensorflow as tf
a=tf.Variable([[0,1,2],[3,4,5]])
print(a)

b=tf.reshape(a,[3,2])
print(b)

4、tf.math.reduce_mean 提供对tensor 求平均值的功能。

import tensorflow as tf
a=tf.constant([1,2,3,4,5,6,7])
b=tf.math.reduce_mean(a)
print(b.numpy())

运行结果:

4
Process finished with exit code 0

5、tf.random.normal 提供随机生成一个tensor,其值符合正态分布。

import tensorflow as tf
a=tf.random.normal(shape=[2,3],mean=2)
print(a.numpy())

运行结果:

[[0.43493736 2.034966   1.1315627 ]
 [3.548563   2.4270773  1.1707357 ]]

Process finished with exit code 0

6、tf.random.uniform 提供随机生成一个tensor,其值符合均匀分布。

import tensorflow as tf
a=tf.random.uniform(shape=[2,3],minval=1,maxval=10,seed=8,dtype=tf.int32)
print(a.numpy())

运行结果:

[[4 9 9]
 [6 4 9]]
Process finished with exit code 0

7、tf.transpose 提供了矩阵的转置功能。

import tensorflow as tf
a=tf.constant([[1,2,3],[4,5,6]])
b=tf.transpose(a)
print(b.numpy())

运行结果:

[[1 4]
 [2 5]
 [3 6]]

Process finished with exit code 0

8.tf.concat 提供多个tensor 拼接功能。

import tensorflow as tf
a=tf.constant([[1,2,3],[4,5,6]])
b=tf.constant([[7,8,9],[10,11,12]])
c=tf.concat([a,b],axis=0)
print(c.numpy())

运行结果:

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
Process finished with exit code 0

9.tf.bitcast 提供数据类型转换功能。

import tensorflow as tf
a=tf.constant(43.344)
print(a.dtype)
b=tf.bitcast(a,type=tf.int32)
print(b.dtype)

运行结果:



Process finished with exit code 0
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/461654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号