Coursera TensorFlow 基礎課程-week1

A New Programming Paradigm

參考:Ubuntu 16 安裝TensorFlow及Jupyter notebook 安裝TensorFlow。

本篇博客翻譯來自 Introduction to TensorFlow for Artificial Intelligence, Machine Learning, and Deep Learning

僅供學習、交流等非盈利性質使用!!!

1. 神經網絡的“Hello World”

給定如下的一組數字,如何擬合?

X = -1, 0 ,1 ,2 , 3, 4
Y = -3, -1,1 ,3 , 5, 7

如果用神經網絡的方式該如何解決?

神經網絡的解決方案是這樣子的:

① 抽選一個擬合函數來對所有數據進行擬合;

② 使用損失函數來判讀這次擬合的好壞;

③ 同時使用優化器來修改擬合函數,再次擬合;

④ 重複上面的步驟到指定次數,或收斂。

上面步驟的具體代碼過程如下:

  1. 定義模型
model = keras.Sequential([keras.layers.Dense(units=1,input_shape=[1])])
  • keras.layers.Dense :定義一層神經網絡;
  • units: 這層神經網絡中神經元的個數,如上面代碼中,只有一個神經元;
  • input_shape:說明輸入的數據的維度;
  • keras.Sequential : 定義一個線性模型;
  1. 定義損失函數
model.compile(optimizer='sgd', loss='mean_squared_error')

損失函數就是指的是這次擬合的效果好壞,比如應用這次擬合後,預測值和真實值一點也不差,那麼就說明擬合比較好。

  1. 設置輸入、輸出
xs = np.array([-1, 0 ,1 ,2 , 3, 4] , dtype = float)
ys = np.array([-3, -1,1 ,3 , 5, 7] , dtype = float)
  1. 擬合參數
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 了。

  1. 預測新數據:
print(model.predict([10.0]))

[[18.97531]]

從預測結果來看結果非常接近19(如果用2x-1 = y 來擬合的話,結果就是19)。

所以結果不是19的理由是:

  • 訓練數據非常少,僅有6組數據;
  • 10的預測結果有很大可能是19,但是神經網絡並不能夠確定,所以這裏給出了一個概率值。

code download: link1link2

拓展: From rules to data

說的就是:之前的給定一個規則和輸入數據,然後就可以推測出新的數據的結果。而現在是給定輸入輸出數據,然後讓神經網絡模型訓練,訓練後,給新的輸入即可得到輸出,這就是和之前的不同。

2. Quiz:

  1. 第 1 個問題
    The diagram for traditional programming had Rules and Data In, but what came out?
  • Binary
  • Machine Learning
  • Bugs
  • Answers
  1. 第 2 個問題
    The diagram for Machine Learning had Answers and Data In, but what came out?
  • Rules
  • Models
  • Binary
  • Bugs
  1. 第 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
  1. 第 4 個問題
    What is a Dense?
  • A layer of disconnected neurons
  • Mass over Volume
  • A single neuron
  • A layer of connected neurons
  1. 第 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
  1. 第 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
  1. 第 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
  1. 第 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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章