2017年12月14日 星期四

Vim tips - Comparing file with hotkey - 揤一个就會使比較仝名檔案

若是頭一擺來遮,建議先來踏話
請共教育部閩南語常用辭典開--咧,隨時會當查。

The following article are written with Taigi. If you prefer English. Please do directly to the middle of this page


為啥乜愛按呢做?

因為工課 ê 關係, 我定定比較兩 ê 仝款名 ê 檔案. 這款情形定定發生佇咧仝一咧程式, 毋過無仝版本 ê 原始碼, . 你有可能用版本控制軟體親像 git, svn, mercury 來共 in 領--出來, 抑是 kā 壓縮 ê 包袱仔 (tarball) 敨--開. 伊的特色是:
  • 這兩 ê 原始碼 ê 目錄結構是仝款
  • 內底 ê 檔案量其約仔名嘛是相siâng
有時我想欲比較看捌的版本寫啥? 就拍開比較軟體, 親像 meld, 抑是 kdiff. 比如講兩个目錄樹 /home/black/A/a/b/f, 和 /home/black/B/a/b/f, 就算你是 GUI, 嘛是愛(準做 TOPA 是 /home/black/A ê 簡單寫, TOPB 是 /home/black/B ê 簡單寫) TOPA/  TOPA/a  ⇒ TOPA/a/b  TOPB/a/b/f, 按呢順伊 ê 路線行落來, 才閣 TOPB 閣行一遍, 才有通看 in ê 比較.

因為我慣勢用 vim, 而且 vim 本身就有檔案比較 ê 功能, 敢有法度佇 vim 裡, 揤--一个都自動來比較咧?

先揣工具

揣出相對路草

欲按怎佇 vim 內, 當你佇編輯抑是寫程式, 比如講咱當佇寫, 欲按怎知影目前 ê 檔案 ê 相對路草咧?
Vim 裡有一咧現成 ê register '@', 準講咱佇 /home/black/A 內底, vi a/b/f

:echo @%
a/b/f

揣出比較ê命令

佇 Vim 裡 ê 比較命令就是

:vertical diffsplit /home/black/B/a/b/f

所以合起來就是:

:vertical diffsplit /home/black/B/@

欲按怎揤一咧都比較?

這時, 咱愛利用 map 來 kā 鍵對應到一咧燒燙燙 ê 鍵, 比如講 F6, 咱 kā 伊寫佇 .vimrc 內底, 按呢逐擺開 vim 伊就會自動對好:

:map <F6> :vertical diffsplit /home/black/B/@

毋過, 按呢有一咧問題: TOPB ê 所在定定改變, 因為無仝 ê 計畫, 無仝版本, 你可能囥足濟版, 按呢你就愛去改 .vimrc, 傷麻煩. 這時咱會使來利用環境變數:
kā .vimrc ê map 改做:


:map <F6> :vertical diffsplit $TOPB/@



咱佇開 vim 進前, 先 kā TOPB 這个變數設好:

$ export TOPB=/home/black/B
$ cd /home/black/A
$ vim a/b/f

按呢咱一揤 F6, 伊就會隨開 /home/black/B/a/b/f 來和你的 f 比較--阿

紲落來下跤个是英文版--ê


Why I would this?

I often do the file comparison, especially the file with the same name. How would this happen? It often happens when you checkout or extract the same software tarball with different version to different directory. Since they are the same software package, they should share the same directory structure.
Sometimes I want to compare the file opened in vim for editing, I have to open the file comparison application, typing or clicking, following the path to the peer file, and the path my editing file. Now that the vim has the comparison capability, why not I use it? And make it easy to do this within vim?

Get the tool required

Get the relative path

Since these two directory has the same file structure, I must know the relative path to the top of source tree, it is good that vim has provided the ready register '@':

:echo @%

The command to compare

If you are editing a file, say "f", where its path "/home/black/A/a/b/f", and you have another source tree at "/home/black/B/a/b/f"
You can use the command in vim to do the file comparing:

:vertical diffsplit /home/black/B/a/b/f

where the other sourec tree top is "/home/black/B".

And the you can combine these two tools:

:vertical diffsplit /home/black/B/%

And it is done.

Making it easy with key mapping and variables

The next step is to make it easy with kep mapping. To do this we can set the TOPB with either environment variables, or specify it in your customized .vimrc, or set it manually with the 'set' command in vim.

I prefer to use the environment variables before I start the vim, so that I can write a script the enclose it. And it would easier to change whenever I change the project.

Here I map the key, here is what I add in my .vimrc

:map <F6>  :vertical diffsplit $TOPB/%

Then when you set the $TOPB environment variable before you sart the vim, bash shell as an example,


$ export TOPB=/home/black/B

$ cd /home/black/A
$ vim f

When you hit the <F6>, it would compare the /home/black/A/a/b/f with your current editing /home/black/B/a/b/f.



2017年12月11日 星期一

Python Deep Learning 深學筆記 - 用 Module 來重複使用



請共教育部閩南語常用辭典開--咧,隨時會當查。

用 Module 來重複使用咱的程式

Module ê 概念, 請參考 Python ê 基礎 -- Module

咱先建立一个檔案 logic.py, 共進前寫好的 function 囥入去:

def AND(x1, x2):
    import numpy as np

    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.7
    tmp = np.sum(w*x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

def OR(x1, x2):
    import numpy as np

    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.2
    tmp = np.sum(w*x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

def NAND(x1, x2):
    import numpy as np

    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    tmp = np.sum(w*x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

徙落來, 咱來寫 xor.py, 干焦共 xor.pylogic.py 囥佇仝一个目錄就會用得:

#!/usr/bin/python3
from logic import AND, OR, NAND

def XOR(x1, x2):
    s1 = NAND(x1, x2)
    s2 = OR(x1, x2)
    y = AND(s1, s2)
    return y

print(XOR(0,0))
print(XOR(1,0))
print(XOR(0,1))
print(XOR(1,1))

伊就會算出 XOR gate ê 結果. 若是結果著,你嘛會使共新 ê XOR 囥入去 logic.py, 日後使用!


其實, 這就是濟棧 ê 神經元

佇前一篇:Python Deep Learning 深學筆記 - XOR ê 實作 tiőng, 是兩棧 ê Ló-tsik, 咱嘛會使共伊看做兩棧 ê 感知器 (Perceptron):






Python Deep Learning 深學筆記 - XOR (Ek-Óo-ké) ê 實作

←前一篇     後一篇→

若是頭一擺來遮,建議先來踏話頭

請共教育部閩南語常用辭典開--咧,隨時會當查。


Ek-Óo-ké (XOR) 會使用 AND, NAND, OR 來組--起來

親像這張圖, 

咱共 (x1, x2) 成做 NAND, NOR ê 輸入.
NAND ê 輸出是 s1, OR ê 輸出 s2, (s1, s2) 才閣成做 AND gate ê 輸入.
落尾, y 都是咱欲愛--ê:


x1     x2 s1     s2 y
0      0
1      0
0
1      0
1      1
1
0      1
1      1
1
1      1
0      1
0


用 Python 來做 XOR Gate

def XOR(x1, x2):
    s1 = NAND((x1, x2)
    s2 = OR(x1, x2)
    y = AND(s1, s2)
    return y
    

佇遮,咱看著 XOR 愛利用著咱進前用過 ê AND, OR, NAND Gate.當然, 咱會使共進前佇 ê AND, OR, NAND gate ê Python 程式, kā 伊複製閣貼過來 (Copy and paste). 毋過, 這馬咱欲來介紹一種我較佮意 ê 方式. 才來分享實際會振動ê程式

←前一篇     後一篇→




2017年12月4日 星期一

Python Deep Learning 深學筆記 - 用非直線 ê 思考來戳破


若是頭一擺來遮,建議先來踏話頭

請共教育部閩南語常用辭典開--咧,隨時會當查。



用彎曲來解決問題

前一篇落尾 ê 問題? 逐个敢有想出答案? 若是干焦欲用一條直--直直 ê 線來kā (1, 0) , (0, 1) 囥佇仝一爿,是無可能ê代誌。欲按怎? 就是愛會曉轉斡。親像下跤這張圖:
Nonlinear
這種彎曲ê線,佇數學上ê專門用詞號做非線性(Nonlinear)。

欲按怎予伊彎曲咧

普通非線性 ê 線,伊的方程式是二次以上--ê,才有可能毋是直線。毋閣佇電腦裡,咱目前干焦想欲用伊上基本 ê Logic Gate 來鬥出 XOR Gate,無想欲開時間去用方程式來表示。

咱閣來激頭殼一咧,若是予你上基本 ê AND,OR,NAND 三種 Gate,你敢有法度鬥出 XOR Gate? 佇遮,咱先複習 in ê 代表符號:


咱先佇遮予恁兩的提示:
1 會使幾落棧,差不多兩棧就會用得
2 佇仝一棧,會使有濟-ê Logic gate,比如講,會使按呢?




你敢想有?

2017年12月1日 星期五

耍電腦愛顧目睭


若是頭一擺來遮,建議先來踏話頭

請共教育部閩南語常用辭典開--咧,隨時會當查。

因為工課 ê 關係,逐工目睭掠螢幕一直繩,目睭退化真緊。毋過,有時佇想代誌,閣真僫控制時間予家己歇睏。這時,咱就愛有物件來鬥相工。

佇 Linux 頂仔,有一个程式 workrave:

安裝

$ sudo apt install workrave

才閣運行:

$ workrave

你就會看著:

伊有分三種模式:
一支手是簡單目睭瞌一咧, 差不多半分鐘, 這是提醒爾爾, 你無插伊嘛會使繼續做工課
一杯咖啡是叫你歇睏去啉咖啡。這會跳出一咧窗仔,你愛家己做決定,看你欲揤 Lock, Skip, 抑是 Postpone,無, 你啥物代誌嘛無法都做。
一支箭頭是叫你出去走走--ê。

當然,你會當共伊延。毋過,顧腹肚嘛著顧健康,顧耍嘛愛顧目睭。抑是起來行行 ê 較好。

2017年11月29日 星期三

Python Deep Learning 深學筆記 - Perceptron 盡磅


若是頭一擺來遮,建議先來踏話頭

請共教育部閩南語常用辭典開--咧,隨時會當查。


孤一个神經元到 XOR gate (Ek-Óo-ké) 就盡磅

XOR 是基本 Ló-tsik-ké 中較特別--ê。特別 ê 原因是欲kā伊做出來,無親像 AND gate, OR gate, NAND gate 遐爾簡單。這毋是干焦 Perceptron 本身 ê 問題,親像欲用電子電路來鬥出 XOR 嘛是較厚工。
咱先看 XOR gate ê 真值表 (Truth table):
XOR truth table
(From wikipeia)
InputOutput
AB
000
011
101
110
伊特別 ê 所在是: 干焦 A 佮 B 無仝,才會輸出 1。這種性質予做互斥.


咱這馬看用 Perception ê 數學表示式,欲按怎做出 XOR ?
y = 1 if (w1x1 + w2x2 ≦ θ)  
y = 0 if (w1x1 + w2x2 >  θ)

咱若是看 w1x1 + w2x2 其實是一个二元一次方程式,伊其實是共一咧平面,分做兩爿:

w1x1 + w2x2 = θ 就是彼條斜斜 ê 線, w1, w2 決定伊偌斜,θ 決定伊離 (0, 0) 偌遠。
若是 (x1, x2) 佇正手頂爿,伊的輸出 y = 1。若是佇倒手下爿,也就是藍色彼爿,就輸出 y = 0。
因為 x1, x2 嘛是干焦會是 0 抑是 1,所以 (x1, x2) 就是干焦有圖裡彼四點: (0, 0), (0, 1), (1, 0), (1, 1) ,對應著真值表 ê 四種 (A, B) 。
Or Gate

像頂懸彼咧圖 (0,1), (1,0), (1, 1) 是對應 OR gate, 你會始轉去看 OR gate  ê  真值表
咱調整 w1, w2, θ ,就是予伊踮這四 ê 點徙振動,下跤就是 AND Gate。

AND Gate

因為有無限濟條線,所以咱進前有講,有無限 ê w1, w2, θ  會當做 AND, OR, NAND。
到今,咱來激頭殼一咧,你敢有法度徙動這條線,來予 (0, 1) 和 (1, 0) 佇仝一爿,(0, 0) 和 (1, 1) 佇另外一爿? 兩爿田無交,水無流?

2017年11月12日 星期日

用 Python 來做分數運算

若是頭一擺來遮,建議先來踏話頭

請共教育部閩南語常用辭典開--咧,隨時會當查。

我佇教囡仔小學 ê 數學時,有時想欲驗算一遍。毋過,咱這馬雖然有計算機,毋過,伊無法度算分數, 拄好我佇耍 Python, 來揣看覓 Python 有法度做--無?

這 "分數" 毋是老師佇共學生拍分數,是二分一 (1/2),三分二(2/3) 這種分數。


參考文章: Dealing with fractions in Python


基礎

首先,咱愛 uì fractions 這咧 Module 共 Fraction 這个函式搝--入來:



from fractions import Fraction
Fraction 佇英文都是 "分數" ê 意思,  頭一咧參數是分子,第二个是分母,譬如講, 3/12 是寫做:
>>> Fraction(3, 12)
Fraction(1, 4)
Fraction(1, 4)。 1/4 是 3/12 化簡過來ê。

若欲看落去較直覺,也都是和數學寫的 3/12 較相仝,你嘛會使用:
>>> Fraction('3/12')
Fraction(1, 4)
你嘛會使共小數(佇電腦伊是用浮點數表示)成做分數:
>>> Fraction(2.5)
Fraction(5, 2)
雖然伊會自動共浮點數(float)轉換, 毋過驚有精差(電腦計算小數有伊的限制), 也是用單引號共伊挾起來較安全:
>>> Fraction('2.5')
Fraction(5, 2)
>>> Fraction(1.1)
Fraction(2476979795053773, 2251799813685248)
>>> Fraction('1.1')
Fraction(11, 10)

約分

咱若有看較斟酌--ê ,頂仔的例,伊會自動共分數約分,也就是共伊化做上簡單 ê 分數:
>>> Fraction(153, 272)
Fraction(9, 16)

通分

你若是共兩的分數加--起來抑是減--落去, 伊會自動通分:
>>> Fraction(1, 2) + Fraction(3, 4)
Fraction(5, 4)
按呢有讚--無? 閣較讚个是, 若是一个分數和整數相加--起來, 伊嘛會自動轉做分數: 
>>> Fraction(5, 16) + 3
Fraction(53, 16)

## 加 .0 佇後壁, 伊都共 3.0 當做浮點數囉!
>>> Fraction(5, 16) + 3.0
3.3125

賰的二元運算 (Binary Operations)

除了加,減,乘佮除嘛是會用得:
>>> Fraction(5, 16) - Fraction(1, 4)
Fraction(1, 16)
>>> Fraction(1, 16) * Fraction(3, 16)
Fraction(3, 256)
>>> Fraction(3, 16) / Fraction(1, 8)
Fraction(3, 2)

共分子抑是分母提出來

你若是干焦欲挃分子,會使用 .numerator 這个方法。若是分母, 用 .denomintor
>>> f = Fraction(221, 234) + Fraction(1, 2)
>>> f.numerator
13
>>> f.denominator
9

共假分數 (imprpoer fraction) 成做帶分數 (mixed numeral)

Fraction() 所算出來个結果,攏是分數,無論是真分數(proper fraction) 抑是假分數 (improper fraction),有時咱愛家己轉換:
>>> int(73/14)
5                   # 這是整數部分
>>> str(Fraction(73%14, 14))
'3/14'              # 這是分數部分


寫一咧小學生看有ê程式


當然,小學生按呢共in講可能嘛是傷複雜,你會用寫簡單 ê 函式來轉換:
from fractions import Fraction
def Frac(x):                    # 單純化簡
    return str(Fraction(x))

def FracOp(x, op, y):           # +, -, *, / 四種運算
    if op == '+':
        result = Fraction(x) + Fraction(y)
    elif op == '-':
        result = Fraction(x) - Fraction(y)
    elif op == '*':
        result = Fraction(x) * Fraction(y)
    elif op == '/':
        result = Fraction(x) / Fraction(y)


    return str(result)


def FracMix(x):                    # 共假分數成做帶分數
    num, dem = x.split('/')
    i = int(int(num)/int(dem))
    dem = Fraction(int(num)%int(dem), int(dem))
    return str(i)+'+'+str(dem)

咱來奕看覓:
>>> from myfrac import Frac,FracOp,FracMix
>>> FracMix('10/3')
'3+1/3'                        # 這是帶分數
>>> Frac('12/24')
'1/2'                           # 這是約分
>>> FracOp('12/24', '+', '3/9')   #這是加法,自動通分
'5/6'
>>> FracOp('12/24', '/', '3/9')   #這是除法
'3/2'
按呢看來, 都共數學式較sîng,都賰 Frac, FracOp, FracMix 三的函式來使用都好阿!

咱若是會共 jupyter 敆做伙使用,無定著是一種袂䆀 ê 教材呢,因為佇線頂,袂曉 python ê 人,干焦拍開網頁, 共事先準備好个稿掠入來,就會當用這三的函式阿,下一回來介紹 jupyter!

佇 Linux 來看GPX 檔案

最近定定有戶外活動。使用𤆬路機 (GPS) 來記錄行過的路線。普通我記錄路線,攏是用手機仔抑是專門个𤆬路機,罕得用電腦來看。 毋過,"仙人拍鼓有時錯,跤步踏差啥人無"。有一擺我無細膩,袂記得共一擺活動的路線收煞起來,閣直接開始記錄下一擺的活動,按呢共幾落...