技术标签: 机器学习、深度学习(ML/DL)  






TensorFlow Tutorial (Tensorflow教程)

Welcome to this week's programming assignment. Until now, you've always used numpy to build neural networks. Now we will step you through a deep learning framework that will allow you to build neural networks more easily. Machine learning frameworks like TensorFlow, PaddlePaddle, Torch, Caffe, Keras, and many others can speed up your machine learning development significantly. All of these frameworks also have a lot of documentation, which you should feel free to read. In this assignment, you will learn to do the following in TensorFlow:

  • Initialize variables
  • Start your own session
  • Train algorithms
  • Implement a Neural Network

Programing frameworks can not only shorten your coding time, but sometimes also perform optimizations that speed up your code.



1 - Exploring the Tensorflow Library (导入Tensorflor库)

To start, you will import the library:


Writing and running programs in TensorFlow has the following steps:

  1. Create Tensors (variables) that are not yet executed/evaluated.
  2. Write operations between those Tensors.
  3. Initialize your Tensors.
  4. Create a Session.
  5. Run the Session. This will run the operations you'd written above.



1.1 - Linear function(线性函数)

Lets start this programming exercise by computing the following equation: =+Y=WX+b, where W and X are random matrices and b is a random vector.

Exercise: Compute +WX+b where ,W,X, and b are drawn from a random normal distribution. W is of shape (4, 3), X is (3,1) and b is (4,1). As an example, here is how you would define a constant X that has shape (3,1):

X = tf.constant(np.random.randn(3,1), name = "X")

You might find the following functions helpful:

  • tf.matmul(..., ...) to do a matrix multiplication
  • tf.add(..., ...) to do an addition
  • np.random.randn(...) to initialize randomly


1.2 - Computing the sigmoid(计算sigmoid函数值)

Great! You just implemented a linear function. Tensorflow offers a variety of commonly used neural network functions like tf.sigmoid and tf.softmax. For this exercise lets compute the sigmoid function of an input.

You will do this exercise using a placeholder variable x. When running the session, you should use the feed dictionary to pass in the input z. In this exercise, you will have to (i) create a placeholder x, (ii) define the operations needed to compute the sigmoid using tf.sigmoid, and then (iii) run the session.


To summarize, you how know how to:
1. Create placeholders
2. Specify the computation graph corresponding to operations you want to compute
3. Create the session
4. Run the session, using a feed dictionary if necessary to specify placeholder variables' values.


1.3 - Computing the Cost(计算成本函数值)

You can also use a built-in function to compute the cost of your neural network. So instead of needing to write code to compute this as a function of [2]()a[2](i) and ()y(i) for i=1...m:



1.4 - Using One Hot encodings(使用one-hot(独热)编码)

Many times in deep learning you will have a y vector with numbers ranging from 0 to C-1, where C is the number of classes. If C is for example 4, then you might have the following y vector which you will need to convert as follows:


1.5 - Initialize with zeros and ones(初始化)

Now you will learn how to initialize a vector of zeros and ones. The function you will be calling is tf.ones(). To initialize with zeros you could use tf.zeros() instead. These functions take in a shape and return an array of dimension shape full of zeros and ones respectively.


2 - Building your first neural network in tensorflow (使用Tensorflow构建第一个神经网络)

In this part of the assignment you will build a neural network using tensorflow. Remember that there are two parts to implement a tensorflow model:

  • Create the computation graph
  • Run the graph

Let's delve into the problem you'd like to solve!

2.0 - Problem statement: SIGNS Dataset (问题描述)

One afternoon, with some friends we decided to teach our computers to decipher sign language. We spent a few hours taking pictures in front of a white wall and came up with the following dataset. It's now your job to build an algorithm that would facilitate communications from a speech-impaired person to someone who doesn't understand sign language.

  • Training set: 1080 pictures (64 by 64 pixels) of signs representing numbers from 0 to 5 (180 pictures per number).
  • Test set: 120 pictures (64 by 64 pixels) of signs representing numbers from 0 to 5 (20 pictures per number).


2.1 - Create placeholders(创建占位符)

Your first task is to create placeholders for X and Y. This will allow you to later pass your training data in when you run your session.


2.2 - Initializing the parameters(初始化参数)

Your second task is to initialize the parameters in tensorflow.


2.3 - Forward propagation in tensorflow(Tensorflow中的前向传播)

You will now implement the forward propagation module in tensorflow. The function will take in a dictionary of parameters and it will complete the forward pass. The functions you will be using are:


2.4 Compute cost(计算成本函数值)

As seen before, it is very easy to compute the cost using:

tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = ..., labels = ...))


2.5 - Backward propagation & parameter updates(后向传播和参数更新)

This is where you become grateful to programming frameworks. All the backpropagation and the parameters update is taken care of in 1 line of code. It is very easy to incorporate this line in the model.

After you compute the cost function. You will create an "optimizer" object. You have to call this object along with the cost when running the tf.session. When called, it will perform an optimization on the given cost with the chosen method and learning rate.


2.6 - Building the model(构建模型)

Now, you will bring it all together!


2.7 - Test with your own image (optional / ungraded exercise)(使用自己的照片进行测试)

Congratulations on finishing this assignment. You can now take a picture of your hand and see the output of your model. To do that:

1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub.
2. Add your image to this Jupyter Notebook's directory, in the "images" folder
3. Write your image's name in the following code
4. Run the code and check if the algorithm is right!


What you should remember:
- Tensorflow is a programming framework used in deep learning
- The two main object classes in tensorflow are Tensors and Operators.
- When you code in tensorflow you have to take the following steps:

  • Create a graph containing Tensors (Variables, Placeholders ...) and Operations (tf.matmul, tf.add, ...)
  • Create a session
  • Initialize the session
  • Run the session to execute the graph

- You can execute the graph multiple times as you've seen in model()
- The backpropagation and optimization is automatically done when running the session on the "optimizer" object. 



版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。



pycharm中使用阿里镜像源方法很多小伙伴因为导入包的时候因为下载速度过慢而头疼,这是因为pycharm下载包的默认源是国外网站(https://pypi.python.org/simple),这时候我们可以用清华镜像或者其他镜像源1、首先进入设置界面(File-Settings)2、进入安装包的界面,点击右边的+号3、点击Manage Repoditories,这个就是设置仓库4、添加清华镜像源,点击OK5、配置成功...

PyCharm vs Spyder:两个Python IDE的快速比较_cumei1658的博客-程序员秘密

If you have followed my blog you may have noticed that a lot of focus have been put on how to learn programming (particularly in Python). I have also written about Integrated Development Environments ...






《信息技术校本课程纲要》由会员分享,可在线阅读,更多相关《信息技术校本课程纲要(4页珍藏版)》请在人人文库网上搜索。1、信息技术校本课程纲要课程名称:如何构建家庭局域网课程类型:科学素养类教学材料:自编授课课时: 30 课时主讲教师:张刚授课对象:初一、初二学生一、 课程目标:(一)、知识与技能:1、通过本课程的学习, 培养学生在信息技术方面的动手能力, 拓 展学生的知识面,从而提高学生的综合信息...


软件学院天梯赛参赛队员第一次训练 L2-1 红豆生南国 (25 分)(完全二叉树,树的遍历,重建二叉树,DFS)_forget……的博客-程序员秘密

题目:有诗云: 相思 (王维 唐)红豆生南国, 春来发几枝。愿君多采撷, 此物最相思。那么,我们来采红豆吧!假设红豆树是这个样子的:这种红豆树的特点是:每个结点都有一个正整数编号,标在结点内部。结点的编号各不相同。 最上方一层结点是“红豆”(图中红圈所示的5个结点),这一层被称之为红豆层。 树的根结点、左子结点、右子结点、左子树、右子树等的定义与“数据结构”中的“二叉树”相同,但它毕竟是“自然界中的树”,树根在最下方,如图中的结点5 图中这棵红豆树...

华中科技大学计算机组成原理慕课第四章 存储系统(一) 单元测验(习题+答案+详细解析)_Code_流苏的博客-程序员秘密_sram中的存储单元

华中科技大学计算机组成原理慕课第四章 存储系统(一) 单元测验(习题+答案+详细解析)

Pycharm IDE安装pandas库失败解决方法_ChiFanLAM的博客-程序员秘密_安装pandas库失败

Pycharm IDE安装pandas库失败解决方法作为一名python新手,我们利用Pychram IDE进行编程时总是会遇到一些配置上的难题。比如,在配置第三方库文件时,我们会遇到漫长的等待安装时间后,却发现安装失败!!!具体的安装失败错误提示为:Error occur when installing " "找遍了各种方法,发现下面的解决方案真的屡试不爽:进入Pycharm的terminal:输入:python -m pip install -upgrade pip或者在P

linux 挂载存储命令,Linux挂载存储及命令_知乎汽车的博客-程序员秘密

常用命令一、网卡部分1、确认IP地址命令ifconfigifconfig -aifconfig ethX2、关闭命令ifdown ethXservice network stop3、开启命令ifup ethXservice network start4、重启命令service network restart/etc/init.d/network restart5、修改IP地址命令vi /etc/s...


华中理工大学1999硕士入学计算机组成原理真题一.填空(每空1分,共20分)1.计算机中数值数据表示长采用的格式有 和 两种。2.已知十进制数,则相应的二进制数X= ,[X]补= 。3.若X=-0.X1X2……Xn,则[X]原= ,[-X]补= 。4.主机与外部设备之间以软件方式控制信息交换的方式有 ...

python try catch finally_Java try catch finally异常处理(Exception)_金雪锋的博客-程序员秘密

1、Java Exceptions执行Java代码时,可能会发生不同的错误异常:程序员编写的编码错误,由于输入错误引起的错误或其他不可预见的情况。发生错误时,Java通常会停止并生成错误消息。 技术术语是:Java将引发异常(引发错误)。2、Java try catchtry语句允许定义要执行的错误代码块。如果在try块中发生错误,则catch语句允许定义要执行的代码块。try和catch关键字...