跳到主要內容

發表文章

ECMA TC39 將實作 javascript private的語言特性

這次ECMA TC39 技術委員在Github上通過了ECMAScript語言特性的草案 詳情可以看這個  https://github.com/tc39/proposal-class-fields class的private property將可以用 # 來表示 現在 如果class要使用private property的話,必須利用Symbol或是WeakMap特性或是其他方法來實作 var _action = Symbol ( 'action' ) ; class Person { getXXX ( ) { return this [ _action ] ( ) ; } [ _action ] ( ) { return 'hi' ; } } 未來 而現在更新之後,直接偷範例過來用 class Counter extends HTMLElement { #xValue = 0 ; get # x ( ) { return #xValue ; } set # x ( value ) { this . #xValue = value ; window . requestAnimationFrame ( this . #render . bind ( this ) ) ; } # clicked ( ) { this . #x ++ ; } constructor ( ) { super ( ) ; this . onclick = this . #clicked . bind ( this ) ; } connectedCallback ( ) { this . # render ( ) ; } # render ( ) { this . textContent = this . #x . toString ( ) ; } } window . cus...

PEP 572爭議事件,以及個人對PEP 572 (Python 3.8) 的看法

前情提要 PEP 572是python 3.8 中的一個feature (Assignment Expressions) 而這個Assignment Expressions語法在最近掀起了Python生態的腥風血雨,先說明一下這是什麼改動 首先先貼一下PEP 572當中的介紹 In most contexts where arbitrary Python expressions can be used, a named expression can appear. This is of the form NAME := expr where expr is any valid Python expression other than an unparenthesized tuple, and NAME is an identifier.  以下是範例 if (match := pattern.search(data)) is not None: # Do something with match # A loop that can't be trivially rewritten using 2-arg iter() while chunk := file.read(8192): process(chunk) # Reuse a value that's expensive to compute [y := f(x), y**2, y**3] 而這個用法並沒有限制變數的位置,如下 def foo(answer = p := 42): # INVALID ... def foo(answer=(p := 42)): # Valid, though not great style ... 所以 當然也可以這樣 def foo(answer: p := 42 = 5): # INVALID ... def foo(answer: (p := 42) = 5): # Valid, but probably never useful 開了這個洞給程式碼,一定會有非常恐怖的程式碼出現 如同以下的程式碼,我們已經可以預測未來不久就會有這種寫法誕生在各個proje...

利用Hyper Parameters自動調整深度學習參數

利用Hyper Parameters Optimization自動調整深度學習混合參數 深度學習通常需要大量的調整參數才能夠得到最好的結果,而為了解決這個問題,有團隊利用了Tree of Parzen Estimators (TPE), Random Search, Grid Search, Bayesian Optimization各種不同的演算法自動調整混合參數(hyper parameters) 而剛好python上有套件為hyperopt,在keras神經網路框架中有人實作為 hyperas 用法如下 from __future__ import print_function from hyperopt import Trials, STATUS_OK, tpe from keras.datasets import mnist from keras.layers.core import Dense, Dropout, Activation from keras.models import Sequential from keras.utils import np_utils from hyperas import optim from hyperas.distributions import choice, uniform, conditional def data(): """ Data providing function: This function is separated from model() so that hyperopt won't reload data for each evaluation run. """ (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(60000, 784) x_test = x_test.reshape(10000, 784) x_train = x_train.astype('float32') x_tes...

keras利用多張gpu進行快速訓練

keras利用多張gpu進行有效率的訓練 由於實驗室電腦目前裝上了三張高效能的gpu,但為了讓keras跑起來符合他的效益,所以需要更改code 目前環境為keras配上tensorflow backend, python3 顯卡三張1080ti 原先我們的網路架構為Sequential架構(粗略的介紹) 所以每次的batch都是利用gpu接task的方式執行 也就是平均分配給每個gpu 那想當然全部的gpu就沒辦法好好的善用 所以我們要把batch slice成gpu的個數(有三片就切成三等份) 再把所有batch結果合併起來成為一個輸出,如此就會加快運算速度(當然也會有每個gpu的輸出限制) 利用下面的code把x切成(n_gpus)等份,注意此時的x並不是真正的數字而是tensor(也就是還沒run出來的值) def slice_batch (x, n_gpus, part) : sh = K.shape(x) L = sh[ 0 ] // n_gpus if part == n_gpus - 1 : return x[part * L:] return x[part * L:(part + 1 ) * L] 之後把model傳入下面的function就會把目前這個model加上(n_gpus)個lambda layer接上input 再把這些結果全部合併為輸出(並且以batch的axis合併) def to_multi_gpu (model, n_gpus= 3 ) : with tf.device( '/cpu:0' ): print(model.input_shape) x = Input(model.input_shape[ 1 :], name= "input" ) towers = [] for g in range(n_gpus): with tf.device( '/gpu:' + str(g)): slice_g = Lambda(slice_batch, lamb...

Codeforces Round #407 (Div. 2)

Codeforces Round #407 (Div. 2) A. Anastasia and pebbles 貪心的拿取就可以了 規則是每一個口袋只能裝一種鵝卵石,且要把所有石頭拿完 所以就是儘量裝就結束了 總之就是貪心法 # include <cstdio> # include <cstdlib> using namespace std ; /* * 20/4=5..0 * 13/4=3..1 */ int main() { // freopen("input.txt", "r", stdin); int n, k; int a[ 100005 ]; int cnt = 0 ; scanf ( "%d %d\n" , &n, &k); for ( int i = 0 ; i < n; i++) { scanf ( "%d" , &a[i]); } for ( int i = 0 ; i < n;) { if (a[i] <= 0 ) { i++; continue ; } if (a[i] >= (k + k)) { cnt += a[i] / (k + k); a[i] %= (k + k); } else if (a[i] > k) { a[i]= 0 ; cnt++; } else { cnt++; a[i]-=k; a[i+ 1 ]-=k; } } printf ( "%d\n" , cnt); } B. Masha and geometric depression 簡單來講只要利用unordered_se...