Autogradã§å¾®åãã¦ãã¢ãã«ãæ§ç¯ããã¨ãã¾ã§ã®è§£èª¬
çããããã«ã¡ã¯
ãå
æ°ã§ããã
Twitterä¸ã§çªç¶è³ãã£ããAutogradã«ã¤ãã¦
æ¸ãã¦ã¿ããã¨ã«ãã¾ãã
Autogradã¨ã¯
Autogradã«ã¤ãã¦ã®èª¬æ
github.com
Autogradã¯numpyãããæ¸ããã¨ãã§ãããã®è¨è¼ããå¼ãå¾®åãã¦ãããã©ã¤ãã©ãªã§ããï¼in Pythonï¼
ç¾ç¶ãPythonã¨torch(lua)ã«ã¦å®è£
ãããããã§ãã
Theanoã¨ã®éãã¯ã·ã³ãã«ãå®ç¾©ãããæ°å¤è¨ç®ããå 容ãç´æ¥æ¸¡ããã¨ãã§ãã¾ããã¨ãã£ãã¨ããã§ãããããæ£ç´èªåå¾®åã¯æ°ãããªããã®ã§ããï¼Theanoãããã¾ãã®ã§ï¼
æ²ãããã¨ã«Pythonã®Autogradã¯ç¾ç¶ãGPUæ¼ç®ãè¡ããã¨ãã§ãã¾ããã
testã®ä¸ã«GPUé¢ä¿ã®å
容ã¯ããããã§ããã
ä¸å¿featureã«GPU operationsã®ãµãã¼ãã¨ãã¦æ²è¼ããã¦ãã¾ããã
Install
sudo pip install autograd
How to use
次ã¯ãã®ä¾¿å©ãã¼ã«ã®ä½¿ãæ¹ãè¦ã¦ã¿ã¾ãã
Tutrialãåèã«ãautogradã使ã£ã¦ã¿ã¾ãããã
åºæ¬çãªææ³ã¯numpyã¨åãã§ããï¼ä¸é¨ä½¿ããªãææ³ã¯ããã¾ããï¼
numpyã§ã¯ import numpy as npã¨ãã§importãã¾ããã
autogradã§ã¯å°ã
ç°ãªãã¾ããç°¡åãªå
容ã«ã¤ãã¦è©¦ãã¦ã¿ã¾ãã
ä»åã¯x ** 2ãå¾®åãã¦ã¿ã¾ãã
Easy Example
Source Code
import autograd.numpy as np from autograd import grad def x_pow(x): return np.power(x,2) grad_pow = grad(x_pow) print grad_pow(2) print (np.power(2.0001,2) - np.power(1.9999,2)) / 0.0002
æ¨æºåºå
4.0 4.0
ã¾ããã¤ã³ãã¼ãããã®ã¯ä»¥ä¸ãå¿
è¦ã«ãªãã¾ãã
numpy ã wrappingããå
容ãããã²ã¨ã¤ã¯å¾®åç¨ã®ã¡ã½ããã§ãã
import autograd.numpy as np from autograd import grad
次ã«å¿
è¦ãªã®ã¯ãå¾®åããé¢æ°ã§ããgradã®å¼æ°ã«ã¯é¢æ°ãä¸ããã®ã§ä»¥ä¸ã®éãã«å®è£
ããã°åé¡ããã¾ããã
å ã¿ã«ããä¸åº¦ãgradãããã¨ã2次微åãå®è¡ãããã¨ãã§ãã¾ãã
grad(x_pow)
Neural Network Example
ãã¡ãã¯å®ä¾ãè¦ã¦ã¿ã¾ããããã½ã¼ã¹ã³ã¼ãå
¨ä½ã¯ä»¥ä¸ãè¦ã¦ããã ãã¨ãã¦
注ç®ããã¹ãã³ã¼ããæ½åºãã¾ãã
autograd/neural_net.py at master · HIPS/autograd · GitHub
ã¢ãã«ã³ã¼ã
def make_nn_funs(layer_sizes, L2_reg): shapes = zip(layer_sizes[:-1], layer_sizes[1:]) N = sum((m+1)*n for m, n in shapes) def unpack_layers(W_vect): for m, n in shapes: yield W_vect[:m*n].reshape((m,n)), W_vect[m*n:m*n+n] W_vect = W_vect[(m+1)*n:] def predictions(W_vect, inputs): for W, b in unpack_layers(W_vect): outputs = np.dot(inputs, W) + b inputs = np.tanh(outputs) return outputs - logsumexp(outputs, axis=1, keepdims=True) def loss(W_vect, X, T): log_prior = -L2_reg * np.dot(W_vect, W_vect) log_lik = np.sum(predictions(W_vect, X) * T) return - log_prior - log_lik def frac_err(W_vect, X, T): return np.mean(np.argmax(T, axis=1) != np.argmax(predictions(W_vect, X), axis=1)) return N, predictions, loss, frac_err
å¦ç¿é¨ã®æ½åº
loss_grad = grad(loss_fun) for epoch in range(num_epochs): print_perf(epoch, W) for idxs in batch_idxs: grad_W = loss_grad(W, train_images[idxs], train_labels[idxs]) cur_dir = momentum * cur_dir + (1.0 - momentum) * grad_W W -= learning_rate * cur_dir
ã¢ãã«é¨åã®é¢æ°ï¼make_nn_funsï¼ã®é¨åã§éè¦ãªã®ã¯ãlosså¤æ°ã§ãã
ãã®losså¤æ°ãå®ç¾©ããå¾®åããã¨Wãæ´æ°ããå¤ã決å®ãããã¨ãã§ãã¾ãã
ã¤ã¾ããAutogradã¯losså®ç¾©ãã¦å¾®åãã¦æ´æ°æ¹æ³ã決å®ãããã¨ã§ã
ããããã¢ãã«ãæ§ç¯ãããã¨ãã§ããã©ã¤ãã©ãªã§ããç§å¦æè¡æ¼ç®ã®ã¾ã¾ã§ããã®ãè¯ãã
ææ³
- ç¾ç¶ãGPUã使ããªãã®ã§ãç°¡åãªè¨ç®ãã¹ãã«ã¯åãã¦ããã§ãããã
- ç°¡åã«ã¤ã³ã¹ãã¼ã«ãæ§ç¯ãã§ããã®ã§å®é¨åãï¼
- èªåå¾®åãæ°ããããã§ã¯å¤åãªãã
- Tutrialãèªãéãnumpyã®å ¨ã¦ããµãã¼ããã¦ããããã§ã¯ãªãã®ã§ãæ¸ãæ¹ã«æ³¨æãã¾ãããã