第一阶段-入门详细图文讲解tensorflow1.4 -(三)TensorFlow 编程基础知识

这篇博客是TensorFlow 1.4的入门教程,详细介绍了TensorFlow的核心概念,包括张量、计算图、会话、TensorBoard、占位符、变量以及基础API的使用。文章强调了TensorFlow程序的两大部分:构建计算图和运行计算图,并通过实例解释了如何在会话中执行计算图。此外,还简述了tf.train和tf.estimator这两个API在机器学习中的作用,为初学者提供了理解TensorFlow模型训练和管理的入门指导。
摘要由CSDN通过智能技术生成

开始之前先把准备工作做好。
打开Anaconda。
没有安装移步 第一阶段-入门详细图文讲解tensorflow1.4 -安装(二)Windows CPU or GPU
这里写图片描述

当然有很多python IED。看你熟悉哪个IDE。新手如果没有经验先按我的步骤走,可以避免入坑。打开spyder
这里写图片描述

——–>开始我们的tensorflow征程<——

这里写图片描述

对于小白直接开始tensorflow基础知识是有困难的。
需要:线性代数与矩阵,概率论,多重积分,级数变换,信息论等工科基础。默认需要有前馈神经网络(BP),标准卷积神经网络(CNN),标准循环神经网络(RNN)基础知识。tensorflow会让你如虎添翼。
但是不想因为这些困难让大家望而止步。这不是paper,而是blog。初级以通俗简单例子通透基本原理。中级以实战项目为例细细品味tensorflow。

get started这页文档,是面向基础的,全流程体验tensorflow。非常适合入门。
follow me.

TensorFlow provides multiple APIs. The lowest level API–TensorFlow Core– provides you with complete programming control. We recommend TensorFlow Core for machine learning researchers and others who require fine levels of control over their models. The higher level APIs are built on top of TensorFlow Core. These higher level APIs are typically easier to learn and use than TensorFlow Core. In addition, the higher level APIs make repetitive tasks easier and more consistent between different users. A high-level API like tf.estimator helps you manage data sets, estimators, training and inference.

This guide begins with a tutorial on TensorFlow Core. Later, we demonstrate how to implement the same model in tf.estimator. Knowing TensorFlow Core principles will give you a great mental model of how things are working internally when you use the more compact higher level API.

最底层的API,tensorflow Core是面向机器学习研究者的,和一些想完全控制自己模型的人。
另外一些封装比较好的高层的API是非常容易上手和使用的。像tf.estimator。

先看几个核心概念。

一,Tensor(张量)

张量是tensorflow核心的基本数据单元。
张量由一组基本值组成,这些值构成任意数量的数组。
张量的秩是它的维数。
下面是张量的一些例子:

3 # a rank 0 tensor; a scalar with shape []
[1., 2., 3.] # a rank 1 tensor; a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

rank:张量的秩。
shape:描述tensor的形状。

理解:做到给出一个shape数组,能够写出tensor的形状。技巧:去掉中括号,逗号+1就是数组的值。数组的的长度是rank。

其实张量是物理学上的量,严格意义上张量在线性不变的情况下才和,scalar ,vector,matrix ,n-dimension量等价。先知道这些对tensorflow程序来说已经够用。深入理解张量请移步《张量分析》

二,The Computational Graph(计算图)

站在计算图的角度,你可以认为tensorflow程序是有相对独立的两部分组成。
1,构建计算图。
2,运行计算图。

计算图是由一系列的tensorflow的操作组成,并且这些操作编配成计算图的节点。
我们先构建一个简单的图。

node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)

打印结果:

Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)

注意:很惊讶结果并没有输出3.0和4.0。
实际中我们运行计算图必须在Session中。

三,Session(会话)

Session:囊括tensorflow运行时候的状态,运行控制。
修改一下:

sess = tf.Session()
print(sess.run([node1, node2]))

结果:[3.0, 4.0]

我们当然可以写出更复杂的计算图。

from __future__ import print_function
node3 = tf.add(node1, node2)
print("node3:", node3)
print(
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值