跳到主要內容

發表文章

目前顯示的是 2017的文章

利用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