Kaggle Titanic

Kaggle Titanic

Kaggle The sinking of the RMS Titanic is one of the most infamous shipwrecks in history. On April 15, 1912, during her maiden voyage, the Titanic sank after colliding with an iceberg, killing 1502 out of 2224 passengers and crew. This sensational tragedy shocked the international community and led to better safety regulations for ships. One of the reasons that the shipwreck led to such loss of life was that there were not enough lifeboats for the passengers and crew. Although there was some element of luck involved in surviving the sinking, some groups of people were more likely to survive than others, such as women, children, and the upper-class. ...

2017-06-09 · 2 min read · 231 words · KbWen · EN
Kaggle Digit Recognizer

Kaggle Digit Recognizer

這是進入 Kaggle 的第一個試題:Kaggle digit recognizer。 這是一個用 CSV 儲存的 MNIST 問題,因此選用 CNN 來解決。資料格式如下: If we omit the “pixel” prefix, the pixels make up the image like this: 000 001 002 003 ... 026 027 028 029 030 031 ... 054 055 056 057 058 059 ... 082 083 | | | | ... | | 728 729 730 731 ... 754 755 756 757 758 759 ... 782 783 The test data set, (test.csv), is the same as the training set, except that it does not contain the “label” column. Your submission file should be in the following format: ...

2017-06-05 · 1 min read · 154 words · KbWen · ZH
TENSORFLOW 練習4: word2vec

TENSORFLOW 練習4: word2vec

把字詞轉成 word embedding 要在字詞中找到他們之間的某種關聯,而不只是分散無意義的符號代表。 做這個問題的核心概念是: 「假設兩個不同句子中的詞上下文相同,則代表兩個詞的語意相近。」 今天要來使用 skip-gram 模型,一個類似二元分類的方式 (判斷像或是不像)。一開始也同之前的問題,先做數據處理。 計算出現數量:[(most count word1, n1), (second word2, n2)] 文字轉成向量: 例如:The actual code for this tutorial is very short 生成的 skip-gram pairs 示意: ([the, code], actual), ([actual, for], code), … (actual, the), (actual, code), (code, actual), … 在這之間都會給他編號,轉化為 (10, 20), (10, 30), (30, 10), (30, 40) ... 的形式。 用到 nce_loss,目前我還不是非常熟練,概念上是讓目標詞的機率越高越好,並讓其餘 K 個負面樣本 (negative samples) 的機率降低。 經典案例: king - queen = man - woman ==> king - queen + woman = man ...

2017-05-12 · 1 min read · 103 words · KbWen · ZH
Tensorflow 練習3: 'FizzBuzz'

Tensorflow 練習3: 'FizzBuzz'

Joel Grus — FizzBuzz in TensorFlow 從網路上看到的幽默問題,算是一個很有趣的使用,適合在做完 Classification 後練習。 輸入資料處理和原版程式碼一樣,因為還蠻直觀的: 1 – 000000001 – [0 0 0 0 0 0 0 0 1] 2 – 000000010 – [0 0 0 0 0 0 0 1 0] ……… 輸出則是用 [1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1] 來代表四個分類。 輸入輸出都是一個矩陣的形式。利用兩層 hidden layer 分別是 512 和 256,激勵函數選擇 relu,剩下的就交給 tensorflow 分類。 結果 一開始一直分不出來,都會卡在把每個資料都判定成同一類 (0.533)。後來減低每次訓練丟進去的量就 OK 了 (忘記一開始做分類時也只丟一點點進去)。 ...

2017-05-08 · 1 min read · 97 words · KbWen · ZH
Tensorflow 練習2: CNN

Tensorflow 練習2: CNN

利用 CNN 來預測數字 (MNIST) 輸入圖形是一個 2828 灰階 0~9 的數字。輸出是一個 110 的矩陣,代表預測 0~9 的機率分布。 流程如下: 輸入 – convolution – pooling – convolution – pooling – hidden layer – output 在代碼中用到 [None, xx, xx] 和 [-1, XX, xx],代表我們忽略輸入的大小(batch size),它會跟隨著輸入自動改變。 max pooling 表示我們選擇的是那個 kernel size 裡的最大值。結構中也加入了 dropout 來避免 overfitting。 結果 上圖是沒有 dropout,下圖是有 dropout。就這個例子而言差別不大,但還是看得出來上面的訓練會比測試好。 準確率落在 97%~99% 之間 (1000 次訓練)。 (目前使用 GradientDescent,更換優化器應該會更好)。 My GitHub 系列文章 TensorFlow 練習1:Polynomial Regression TensorFlow 練習3:FizzBuzz TensorFlow 練習4:word2vec

2017-05-07 · 1 min read · 67 words · KbWen · ZH
PYTHON 機器學習基石 LS-PLA

PYTHON 機器學習基石 LS-PLA

Perceptron Learning Algorithm (PLA) 根據林軒田教授的機器學習基石課程,實作一下這個基礎的機器學習演算法。 我們探討的是監督式學習 (Supervised learning) 大架構下的二元分類 (YES/NO) 問題。 Perceptron ⇔ linear (Binary) Classifiers 我們有一組訓練資料 D,包含數據 Xn 和對應的 Yn (在這裡就是 1, -1);Hypothesis set H 代表全部可能的解 (無限多條線),經過演算法 A,從 H 找到一個可能的 g 與我們的目標函數 f 相近。 這個演算法的主要兩大步驟:找到錯誤的點,進行向量修正。 詳細課程可以參考教授的講解!!其中 naive cycle 是常用的作法。 這方法只適用於 linear separable PLA。 除此以外,當資料中有雜訊也無法使用這個方式,目前在線性問題上較好的解是用 Pocket PLA。 Linear separable PLA 首先整理一下資料。把原始格式如 ['x0\ty0\tz0\nx1\ty1\tz1\nx2\ty2\tz2\n....'] 轉換為 array([[(x0, y0), z0], [(x1, y1), z1], [(x2, y2), z2].....]) 的格式。 NAIVE PLA 實作,畫線則是用 ax + by = 0。 ...

2017-04-18 · 1 min read · 79 words · KbWen · ZH
Tensorflow 使用GPUs

Tensorflow 使用GPUs

Tensorflow 支援使用 CPU 和 GPU 做運算: "/cpu:0": The CPU of your machine. "/gpu:0": The GPU of your machine, if you have one. "/gpu:1": The second GPU of your machine, etc. 用 with tf.device() 來分配這個語句下使用的設備。 可以用以下設定來優化運算: log_device_placement = True: 紀錄我們使用 device 的情況。 allow_soft_placement = True: 避免指定的 device 不存在,讓他能自行分配到存在且可運行的地方。 我沒有多顆 CPU,其他的語法先不試。

2017-04-14 · 1 min read · 49 words · KbWen · ZH
Tensorflow 練習1 : Polynomial Regression

Tensorflow 練習1 : Polynomial Regression

使用 Tensorflow 分析 Regression 的基礎練習 Nerual network 分析二維四次多項式 先定義輸入輸出格式,None表示我們不限制它的Row 在 Tensorflow 中,要定義它是常數、變數,或是從外部輸入,必須要分別指定成: tf.constant() tf.Variable() tf.placeholder() 他才會是那個形式;而想使用 Tensorflow 的任何內容,必須要用 sess.run() 去啟動它,不然會是 Tensor 的格式。 其中 sess = tf.Session() 定義一個 Y = W*x + b 的線性方程,在隱藏層中利用 activation function 去改變它。 評估模型好壞常用有 square error 和 cross_entropy,這裡利用 square error 計算 loss。 選擇基本的梯度下降並最小化 loss;optimizer 是個小於 1 的值。 設定要訓練的數值和函數 (記得要有一定的雜訊) W shape = (in_dim, hidden_units) = (10,1) predictions shape = (200,1)*(1,10)*(10,1) = (200,1) 訓練 1000 次每 50 次看結果:視覺化和數據化 ...

2017-04-13 · 1 min read · 86 words · KbWen · ZH