Coursera TensorFlow 基础课程-week1
Posted fansy1990
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Coursera TensorFlow 基础课程-week1相关的知识,希望对你有一定的参考价值。
A New Programming Paradigm
参考:Ubuntu 16 安装TensorFlow及Jupyter notebook 安装TensorFlow。
本篇博客翻译来自 Introduction to TensorFlow for Artificial Intelligence, Machine Learning, and Deep Learning
仅供学习、交流等非盈利性质使用!!!
其他相关文章
Coursera TensorFlow 基础课程-week4
Coursera TensorFlow 基础课程-week3
Coursera TensorFlow 基础课程-week2
Coursera TensorFlow 基础课程-week1
文章目录
1. 神经网络的“Hello World”
给定如下的一组数字,如何拟合?
X = -1, 0 ,1 ,2 , 3, 4
Y = -3, -1,1 ,3 , 5, 7
如果用神经网络的方式该如何解决?
神经网络的解决方案是这样子的:
① 抽选一个拟合函数来对所有数据进行拟合;
② 使用损失函数来判读这次拟合的好坏;
③ 同时使用优化器来修改拟合函数,再次拟合;
④ 重复上面的步骤到指定次数,或收敛。
上面步骤的具体代码过程如下:
- 定义模型
model = keras.Sequential([keras.layers.Dense(units=1,input_shape=[1])])
- keras.layers.Dense :定义一层神经网络;
- units: 这层神经网络中神经元的个数,如上面代码中,只有一个神经元;
- input_shape:说明输入的数据的维度;
- keras.Sequential : 定义一个线性模型;
- 定义损失函数
model.compile(optimizer='sgd', loss='mean_squared_error')
损失函数就是指的是这次拟合的效果好坏,比如应用这次拟合后,预测值和真实值一点也不差,那么就说明拟合比较好。
- 设置输入、输出
xs = np.array([-1, 0 ,1 ,2 , 3, 4] , dtype = float)
ys = np.array([-3, -1,1 ,3 , 5, 7] , dtype = float)
- 拟合参数
model.fit(xs, ys, epochs = 500)
调用模型的fit函数即可来拟合模型的参数,而epochs则指的是拟合多少次。设置500次后,进行拟合,可以得到下面的结果:
Epoch 1/500
6/6 [==============================] - 1s 101ms/sample - loss: 60.8370
Epoch 2/500
6/6 [==============================] - 0s 501us/sample - loss: 48.2978
...
Epoch 499/500
6/6 [==============================] - 0s 451us/sample - loss: 7.3124e-05
Epoch 500/500
6/6 [==============================] - 0s 518us/sample - loss: 7.1621e-05
可以看到,最后拟合误差已经非常接近 0 了。
- 预测新数据:
print(model.predict([10.0]))
[[18.97531]]
从预测结果来看结果非常接近19(如果用2x-1 = y 来拟合的话,结果就是19)。
所以结果不是19的理由是:
- 训练数据非常少,仅有6组数据;
- 10的预测结果有很大可能是19,但是神经网络并不能够确定,所以这里给出了一个概率值。
拓展: From rules to data
说的就是:之前的给定一个规则和输入数据,然后就可以推测出新的数据的结果。而现在是给定输入输出数据,然后让神经网络模型训练,训练后,给新的输入即可得到输出,这就是和之前的不同。
2. Quiz:
- 第 1 个问题
The diagram for traditional programming had Rules and Data In, but what came out?
- Binary
- Machine Learning
- Bugs
- Answers
- 第 2 个问题
The diagram for Machine Learning had Answers and Data In, but what came out?
- Rules
- Models
- Binary
- Bugs
- 第 3 个问题
When I tell a computer what the data represents (i.e. this data is for walking, this data is for running), what is that process called?
- Labelling the Data
- Categorizing the Data
- Programming the Data
- Learning the Data
- 第 4 个问题
What is a Dense?
- A layer of disconnected neurons
- Mass over Volume
- A single neuron
- A layer of connected neurons
- 第 5 个问题
What does a Loss function do?
- Figures out if you win or lose
- Generates a guess
- Decides to stop training a neural network
- Measures how good the current ‘guess’ is
- 第 6 个问题
What does the optimizer do?
- Generates a new and improved guess
- Measures how good the current guess is
- Decides to stop training a neural network
- Figures out how to efficiently compile your code
- 第 7 个问题
What is Convergence?
- The process of getting very close to the correct answer
- A programming API for AI
- A dramatic increase in loss
- The bad guys in the next ‘Star Wars’ movie
- 第 8 个问题
What does model.fit do?
- It makes a model fit available memory
- It optimizes an existing model
- It trains the neural network to fit one set of values to another
- It determines if your activity is good for your body
My guess:
1. D
2. A
3. D
4. D
5. D
6. A
7. A
8. C
3. Excise
房屋价格预测:一般来说,一个房子需要花费50k+50k,通常50K是底价,然后每加1间房,增加50K,所以1间房一般花费100k,2间房花费150k,以此类推。
那么如果7间房,耗费多少呢?接近400K么?
Hints:100K可以只使用1来代替。
import tensorflow as tf
import numpy as np
from tensorflow import keras
model = # Your Code Here#
model.compile(# Your Code Here#)
xs = # Your Code Here#
ys = # Your Code Here#
model.fit(# Your Code here#)
print(model.predict([7.0]))
The Answers: excise answer.
以上是关于Coursera TensorFlow 基础课程-week1的主要内容,如果未能解决你的问题,请参考以下文章
Coursera TensorFlow 基础课程-week3
Coursera TensorFlow 基础课程-week3
Coursera TensorFlow 基础课程-week3
Coursera TensorFlow 基础课程-week2