1.2常用函数

本节目标:掌握在建立和操作神经网络过程中常用的函数

# 常用函数

import tensorflow as tf
import numpy as np # 强制Tensor的数据类型转换
x1 = tf.constant([1,2,3],dtype = tf.float64)
print(x1)
x2 = tf.cast(x1,tf.int32)
print(x2)
# 计算张量中最小的元素
print(tf.reduce_min(x2))
# 计算张量中最大的元素
print(tf.reduce_max(x2))
输出结果:
tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
# 理解axis,在一个二位张量或者数组中,可以通过调整axis等于0或者1控制执行维度
# axis=0代表跨行(经度,down),而axis=1跨列(纬度,across)
# 不指定axis,则所有元素参与计算
x = tf.constant([[1,2,3],
[4,5,6]])
print(x)
print(tf.reduce_mean(x)) # 求平均[2,5],平均为3
print(tf.reduce_sum(x,axis = 1)) # 求总和按行操作
输出结果:
tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor([ 6 15], shape=(2,), dtype=int32)
# tf.Variable()将变量标记为可训练,被标记的变量会在反向传播中被记录梯度信息
w = tf.Variable(tf.random.normal([2,2],mean= 0,stddev =1))
print(w)
输出结果:
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[ 0.47072804, -0.7259878 ],
[-1.6562318 , 0.15564619]], dtype=float32)>
# 常用的运算函数
# 加法
a = tf.constant([1,2,3],dtype = tf.float32)
b = tf.constant([4,5,6],dtype = tf.float32)
print(tf.add(a,b))
# 减法
print(tf.subtract(a,b))
# 乘法
print(tf.multiply(a,b))
# 除法
print(tf.divide(b,a))
# 平方
print(tf.square(a))
# 次方
print(tf.pow(a,3))
# 开放
print(tf.sqrt(a))
# 矩阵乘法
c = tf.ones([3,2])
d = tf.fill([2,3],6.)
print(tf.matmul(c,d))
输出结果:
tf.Tensor([5. 7. 9.], shape=(3,), dtype=float32)
tf.Tensor([-3. -3. -3.], shape=(3,), dtype=float32)
tf.Tensor([ 4. 10. 18.], shape=(3,), dtype=float32)
tf.Tensor([4. 2.5 2. ], shape=(3,), dtype=float32)
tf.Tensor([1. 4. 9.], shape=(3,), dtype=float32)
tf.Tensor([ 1. 8. 27.], shape=(3,), dtype=float32)
tf.Tensor([1. 1.4142135 1.7320508], shape=(3,), dtype=float32)
tf.Tensor(
[[12. 12. 12.]
[12. 12. 12.]
[12. 12. 12.]], shape=(3, 3), dtype=float32)  
# 切分传入张量的第一维度,生成输入特征/标签配对,构成数据集
features = tf.constant([12,23,10,17])
labels = tf.constant([0,1,1,0])
# 对特征和标签进行一一配对
dataset = tf.data.Dataset.from_tensor_slices((features,labels))
print(dataset)
for element in dataset:
print(element)
输出结果:
<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>
(<tf.Tensor: id=286, shape=(), dtype=int32, numpy=12>, <tf.Tensor: id=287, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=288, shape=(), dtype=int32, numpy=23>, <tf.Tensor: id=289, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=290, shape=(), dtype=int32, numpy=10>, <tf.Tensor: id=291, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=292, shape=(), dtype=int32, numpy=17>, <tf.Tensor: id=293, shape=(), dtype=int32, numpy=0>)
# 求导数运算
with tf.GradientTape() as tape:
w= tf.Variable(tf.constant(3.0))
loss = tf.pow(w,2)
# 对w2求w的倒数
grad = tape.gradient(loss,w)
print(grad)
输出结果:
tf.Tensor(6.0, shape=(), dtype=float32)
# 求导数运算
with tf.GradientTape() as tape:
w= tf.Variable(tf.constant(3.0))
loss = tf.pow(w,2)
# 对w2求w的倒数
grad = tape.gradient(loss,w)
print(grad)
输出结果:
0 one
1 two
2 three
# 独热编码:将张量中的每个元素按照规律独立编码,编码中0为否1为是
labels = tf.constant([0,1,2,3])
classes = 4
output = tf.one_hot(labels,depth = classes)
print(output)
输出结果:
tf.Tensor(
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]], shape=(4, 4), dtype=float32)
# 用softmax函数使得输出符合概率分布,将输出用e为底y为指数,求出每个输出的概率
# 对概率进行归一化操作
y = tf.constant([1.01,2.01,-0.66])
y_pro = tf.nn.softmax(y)
print("After softmax,y_pro is:",y_pro)
输出结果:
After softmax,y_pro is: tf.Tensor([0.25598174 0.69583046 0.0481878 ], shape=(3,), dtype=float32)
# 用assign_sub函数对参数进行自更新,赋值操作(更新参数为可训练)
w = tf.Variable(4)
# 对w进行自减一操作w = w -1
w.assign_sub(1)
print(w)
输出结果:
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>

# 返回张量沿指定维度最大值的索引
test = np.array([[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]])
print(test)
print(tf.argmax(test,axis = 0))
print(tf.argmax(test,axis = 1)) # 判断两个数是否相等,bool类型
correct = tf.equal(1,1)
print(correct)
输出结果:
[[ 1  2  3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
tf.Tensor([3 3 3], shape=(3,), dtype=int64)
tf.Tensor([2 2 2 2], shape=(4,), dtype=int64)
tf.Tensor(True, shape=(), dtype=bool) 本节对各个函数运用,对神经网络搭建和操作十分重要,请大家务必掌握。
												

tensorflow2.0学习笔记第一章第二节的更多相关文章

  1. tensorflow2.0学习笔记第一章第一节

    一.简单的神经网络实现过程 1.1张量的生成 # 创建一个张量 #tf.constant(张量内容,dtpye=数据类型(可选)) import tensorflow as tf import num ...

  2. tensorflow2.0学习笔记第一章第四节

    1.4神经网络实现鸢尾花分类 import tensorflow as tf from sklearn import datasets import pandas as pd import numpy ...

  3. tensorflow2.0学习笔记第一章第五节

    1.5简单神经网络实现过程全览

  4. tensorflow2.0学习笔记第一章第三节

    1.3鸢尾花数据读入 # 从sklearn包datasets读入数据 from sklearn import datasets from pandas import DataFrame import ...

  5. PRML学习笔记第一章

    [转] PRML笔记 - 1.1介绍 模式识别的目标 自动从数据中发现潜在规律,以利用这些规律做后续操作,如数据分类等. 模型选择和参数调节 类似的一族规律通常可以以一种模型的形式为表达,选择合适模型 ...

  6. Java 学习笔记 第一章:Java语言开发环境搭建

    第一章:Java语言开发环境搭建 第二章:常量.变量和数据类型 第三章:数据类型转换.运算符和方法入门 1.Java虚拟机——JVM JVM(Java Virtual Machine ):Java虚拟 ...

  7. C语言学习笔记第一章——开篇

    本文章B站有对应视频 (本文图片.部分文字引用c primer plus) 什么是C语言 顾名思义,c语言是一门语言,但是和我们所讲的话不同,它是一门编程语言,是为了让机器可以听懂人的意思所以编写的一 ...

  8. Java学习笔记 第一章 入门<转>

    第一章 JAVA入门 一.基础常识 1.软件开发 什么是软件? 软件:一系列按照特定顺序组织的计算机数据和指令的集合 系统软件:DOS,Windows,Linux 应用软件:扫雷.QQ.迅雷 什么是开 ...

  9. c#高级编程第七版 学习笔记 第一章 .NET体系结构

    第一章      .NET体系结构 本章内容: 编译和运行面向.NET的代码 Microsoft中间语言(Microsoft Intermediate Language,MSIL或简称IL)的优点 值 ...

随机推荐

  1. python之logging基础入门

    博客学习至:https://www.cnblogs.com/Nicholas0707/p/9021672.html#_label0 https://www.cnblogs.com/dream66/p/ ...

  2. 如何搭建一个WEB服务器项目(五)—— Controller返回JSON字符串

    从服务器获取所需数据(JSON格式) 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出错误,分享宝贵经验 ...

  3. 转帖 支撑4.5亿活跃用户的WhatsApp架构概览

    http://www.csdn.net/article/2014-02-27/2818559-an-overview-at-whatsapp's-19b-architecture/2 写的很好,确实牛 ...

  4. 存储系列之 LUN 和 LVM

    一.LUN 1.LUN的由来 上一篇文章已经介绍了RAID技术的原理,那么RAID的实现呢?有两种方式,RAID软件和RAID硬件.但是因软件RAID占用主机CPU和主机内存,而且RAID功能不易实现 ...

  5. 王艳 201771010127《面向对象程序设计(java)》第六周学习总结

    实验六 继承定义与使用 一:理论部分: 第五章:继承类. 1.继承:已有类来构建新类的一种机制.档定义了一个新类继承另一个类时,这个新类就继承了这个类的方法和域,同时在新类中添加新的方法和域以适应新的 ...

  6. 判断割是否唯一zoj2587

    Unique Attack Time Limit: 5 Seconds      Memory Limit: 32768 KB N supercomputers in the United State ...

  7. SpringBoot自定义装配的多种实现方法

    Spring手动装配实现 对于需要加载的类文件,使用@Configuration/@Component/@Service/@Repository修饰 @Configuration public cla ...

  8. 哥德巴赫猜想 Java实现

    1.接口实现 package goldbach; /** * 输入一个大于6的偶数,请输出这个偶数能被分解为哪两个质数的和.如:10=3+7 12=5+7 * 此为按接口实现类完成 * * @auth ...

  9. C++软件开发面试题总结

    面试题有难有易,不能因为容易,我们就轻视,更不能因为难,我们就放弃.我们面对高薪就业的态度永远不变,那就是坚持.坚持.再坚持.出现问题,找原因:遇到困难,想办法.我们一直坚信只有在坚持中才能看到希望, ...

  10. HTTP——无状态协议理解

    无状态服务器是指一种把每个请求作为与之前任何请求都无关的独立的事务的服务器. HTTP是一个属于应用层的面向对象的协议 ------未完待续