iÆÄÀ̽㠼­¹ö¸¦ mlc¼­¹ö¿¡ ÀÖÀ½ ÄÁÆ®·Ñ ¿£ÅÍ°¡ ½ÇÇà ½¬ÇÁÆ® ¿£ÅÍ°¡ ´Ù¸¥ ½© ½Éº¼¸¯ º¯¼ö´Â, ¹ÌÁö¼ö º¯¼ö x,yµîÀ» ÇÒ´çÇÏ´Â °Í theano.function Àº CUDA CÄڵ带 ÄÄÆÄÀÏÇس»´Â °úÁ¤ÀÌ°í, ¿©±â¼­ ÃÖÀûÈ­°¡ ÀϾ´Ù. ¶ÇÇÑ º´·ÄÈ­µµ ÀÚµ¿À¸·Î ó¸®ÇØÁØ´Ù. theano.function(inputs, outputs) inputs´Â º¯¼öÀÇ ¸®½ºÆ®¸¦ ³Ö¾î¾ßÇÑ´Ù. outputs´Â ÀÎDzµé·ÎºÎÅÍ °è»ê°¡´ÉÇÑ ½ÄÀÌ µé¾î¿È. outputs=f(inputs) ½Éº¼¸¯ º¯¼öµé¿¡´ëÇÑ ¿¬»êÀº ±×·¡ÇÁ·Î½á Ç¥ÇöµÊ. ½Éº¼¸¯ algebraµé·Î¸¸ ¿¬»êÀ» ÇÏ´Ù°¡, ½ÇÁ¦·Î ¼ýÀÚ¸¦ ³Ö¾î¼­ °è»êÇÒ ¶§ theano.function À» ÀÌ¿ëÇؾßÇÔ. (ÇÔ¼öÇü ¾ð¾îÀÇ Ã¶ÇÐÀ» µû¸£´Â ¹æ½Ä) ½ÉÆÄÀÌ, ¸Þ½º¸ÞƼī µîµµ ¸¶Âù°¡ÁöÀÇ ½Éº¼¸¯ ¾ËÁ¦ºê¶ó¸¦ »ç¿ëÇÔ. theano.function Àº ÀÌ ±×·¡ÇÁ¸¦ CUDA ÄÚµå·Î ÄÄÆÄÀÏ ÇØÁÖ´Â °ÍÀÓ CPU/GPU º¯È¯Àº theanoÄڵ带 ½ÇÇà½ÃÅ°±â Àü¿¡ ÇØÁÖ¾î¾ßÇÔ. ±×·¡¾ß theano.function ¿¡¼­ ÄÄÆÄÀÏÀ» ´Ù¸£°Ô ÇÏ°Ú³×. print(theano.config.floatX) : default ŸÀÔ È®ÀÎ #½Éº¼¸¯ º¯¼öÀÇ Å¸ÀÔ T.scalar() : ŸÀÔÀ» ÁöÁ¤ÇÏÁö ¾Ê´Â default ŸÀÔÀÎ float32À¸·Î µÇ´Â µí T.iscalar() : int 32 ŸÀÔÀ» »ç¿ëÇÏ°Ú´Ù T.lscalar() : int 64 T.dscalar() : float64 #½Éº¼¸¯ º¯¼öÀÇ Â÷¿ø T.scalar() : 0Â÷¿ø Á¡ T.vector() : 1Â÷¿ø Á÷¼± T.matrix() : 2Â÷¿ø Æò¸é T.tensor3() : 3Â÷¿ø Å¥ºê T.tensor4() : 4Â÷¿ø x=T.matrix() y=T.matrix() z=T.dot(x,y) ftnSum_mat = theano.function([x,y],z) print(ftnSum_mat(3,4)) ----- 3,4´Â matrix°¡ ¾Æ´Ï¶ó¼­ ¿¡·¯°¡ ³² x=T.matrix() y=T.matrix() z=T.dot(x,y) ftnSum_mat = theano.function([x,y],z) a = np.random.random((3,3)) b = np.random.random((3,3)) print(a,b,ftnSum_mat(a,b)) --------- x=T.matrix() y=T.matrix() z=T.dot(x,y) ftnSum_mat = theano.function([x,y],z) a = np.random.random((3,3)) b = np.random.random((3,3)) print(a,b,ftnSum_mat(a,b)) #ÀÌ·¸°Ô Çصµ º¯¼ö ŸÀÔÀÌ ³ÑÆÄÀÌ´Â float64Àε¥, ¾¾¾Æ³ë´Â float32¶ó¼­ ¿¡·¯°¡³² µû¶ó¼­ ¾Æ·¡¿Í°°ÀÌ asarrayÇÔ¼ö·Î float32ŸÀÔÀ¸·Î ¹Ù²ãÁÖ¾î¾ßÇÔ. ------ x=T.matrix() y=T.matrix() z=T.dot(x,y) ftnSum_mat = theano.function([x,y],z) a = np.asarray(np.random.random((3,3)),dtype=theano.config.floatX) b = np.asarray(np.random.random((3,3)),dtype=theano.config.floatX) print(a,b,ftnSum_mat(a,b)) ------- °á·Ð -> Ç×»ó float32¸¦ ¾²µµ·Ï ÁÖÀÇÇÒ°Í python º¯¼ö´Â ½ÇÁ¦·Î °ªÀ» °¡Áø º¯¼öÀÓ symbolic º¯¼ö´Â ½ÇÁ¦°ªÀÌ ¾ø´Â ¹ÌÁö¼ö º¯¼öÀÓ shared º¯¼ö´Â ½Éº¼¸¯ º¯¼öÀε¥, °ªÀ» °¡Áö°í ÀÖ´Â ³à¼®ÀÓ. GPUÀÇ ¼Î¾îµå ¸Þ¸ð¸®¿¡ ¿Ã¶ó°¡´Â º¯¼ö·Î, theanoº¯¼ö°¡ Á¢±Ù °¡´ÉÇÑ °ªÀ» °¡Áö°í ÀÖÀ½. -- a = np.array([[1,2],[3,4]], dtype = theano.config.floatX) x = theano.shared(a) #a ¸ÅÆ®¸¯½º°¡ gpu ¸Þ¸ð¸®¿¡ ¿Ã¶ó°¡¼­, theano º¯¼ö°¡ Á¢±Ù °¡´ÉÇÔ ----¸¸¾à gpu¸Þ¸ð¸®°¡ ¸ðÀÚ¶ó¸é, ¿©±â¼­ ¿Ã¸®´Ù°¡ ¿¡·¯°¡ ³² print(x.get_value()) x.set_value(x.get_value()+1) print(x.get_value()) # set Àº cpu -> gpu·Î ¸Þ¸ð¸®¸¦ ¿Ã¸®´Â °Í # get Àº gpu -> cpu·Î ¸Þ¸ð¸®¸¦ °¡Á®¿À´Â °Í µû¶ó¼­ get°ú setÀ» ÃÖ´ëÇÑ ¾È½á¾ßÇÔ. ÀÌ°Ç °ÅÀÇ µð¹ö±ë ¿ëÀ̶ó°í ÇÒ ¼ö ÀÖÀ½. ±×¸®°í ¾È¾²°í ¾îÂ÷ÇÇ function¿¡¼­ Çѹ濡 °è»êÇϱ⶧¹®¿¡ ÇÊ¿ä¾øÀ½ --- a = np.array([[1,2],[3,4]], dtype = theano.config.floatX) x = theano.shared(a) y = x**2 ftnShared = theano.function([],y) print(ftnShared()) # ¿©±â¼­´Â inputÀ¸·Î sharedº¯¼ö¸¦ Àü´ÞÇÏ¸é ¾ÈµÊ. input¿¡´Â ½Éº¼¸¯ º¯¼ö¸¸ Àü´ÞÀÌ °¡´ÉÇÔ. shared º¯¼ö´Â ÇÔ¼öÀÇ ÀÎÀÚ°¡¾Æ´Ô. --- T.nnet.conv.conv2d() ÇÔ¼ö µî¿¡ ÄÁ¹ú·ç¼Ç ·¹À̾ Á¤ÀǵǾîÀÖÀ½. - x = T.scalar() y = x**2 diff = theano.function([x],T.grad(y,[x])) print(diff(3)) °á°ú : [array(6.0, dtype=float32)] # array°¡ ºÙÀº ÀÌÀ¯´Â T.gradÀÇ ¹Ýȯ °ªÀÌ array¶ó¼­ ±×·³. ¿Ö³ÄÇϸé x Çϳª°¡¾Æ´Ï¶ó ¿©·¯°³ÀÇ º¯¼öµéÀÇ ¸®½ºÆ®·Î Æí¹ÌºÐÀ» Çؼ­ T.grad(y,[x]) Áï, y ÇÔ¼ö¸¦ x·Î Æí¹ÌºÐÇÏ´Â °ÍÀÓ, -- x = T.scalar() y = x**2 diff = theano.function([x],T.grad(y,[x])[0]) print(diff(3)) °á°ú: 6.0 [0]À» Çؼ­, ù¹ø° º¯¼ö·Î ¹ÌºÐÇÑ °ª¸¸ array¿¡¼­ °¡Á®¿È -- x = T.scalar() w = theano.shared(np.array(3,dtype=theano.config.floatX),borrow=True) obj = (1 - x*w)**2 learn_w = (w, w-0.1*T.grad(obj,w)) learn = theano.function([x],obj,updates=[learn_w]) print(learn(2)) updates ´Â learn_w = (w, w-0.1*T.grad(obj,w)) ÀÌ·±½ÄÀ¸·Î Á¤ÀǸ¦ ÇØÁÖ¾î¾ßÇÔ. Áï w¸¦ ¸Å ½ºÅǸ¶´Ù ¹Ù²Ù¾î°¥ ½ÄÀ» Á¤ÀÇÇÔ functionÀÇ ¼ø¼­°¡ input ¿¡¼­ outputÀ» °è»êÇÏ°í, ±×´ÙÀ½ update ½ÄÀ» ¼öÇàÇÔ while(){ learn(2)¸¦ ÇØÁÖ¾î¾ß ÀÌÁ¦ °è¼Ó ¾÷µ¥ÀÌÆ® ÇÏ´Â °ÍÀÓ } --- x = T.scalar() w = theano.shared(np.array(3,dtype=theano.config.floatX),borrow=True) obj = (1 - x*w)**2 learn_w = (w, w-0.1*T.grad(obj,w)) learn = theano.function([x],obj,updates=[learn_w]) print(learn(2),w.get_value()) print(learn(2),w.get_value()) print(learn(2),w.get_value()) print(learn(2),w.get_value()) ---- theano´Â update°¡ ´º·²³ÝÀ» ÀüÁ¦·Î ±ò°í Á¤ÀǵÊ. ±»ÀÌ ÀÌ°É ¾È ¾²°í, ¾÷µ¥ÀÌÆ®¸¦ function À¸·Î ÇØÁ൵µÊ -- import theano import theano.tensor as T class opt_problem: def __init__(self): # optimization variables self.x = theano.shared(numpy.array(100.,dtype=theano.config.floatX), borrow=True) self.y = theano.shared(numpy.array(100.,dtype=theano.config.floatX), borrow=True) self.pos = [self.x, self.y] # hyperparameters self.lr = theano.shared(np.array(0.05, dtype=theano.config.floatX), borrow=True) # learning rate # optimization objective self.ftn_eqn = (self.x ** 2) + 10*(self.y ** 2) self.grad = T.grad(self.ftn_eqn, self.pos) # evaluating current value self.ftn = theano.function([], self.ftn_eqn) def plot(self, scope=20.): # visualizing code x = np.arange(-scope, scope, 0.05) y = np.arange(-scope, scope, 0.05) X, Y = np.meshgrid(x, y) X = np.asarray(X, dtype=theano.config.floatX) Y = np.asarray(Y, dtype=theano.config.floatX) Z = (X ** 2) + 10*(Y ** 2) plt.figure() C = plt.contour(X,Y,Z) def build_gd(self): self.updates = [] for posi, grad in zip(self.pos, self.grad): self.updates.append((posi, posi - self.lr*grad)) # zipÀº posµµ ¸®½ºÆ®°í, gradµµ ¸®½ºÆ®Àε¥ °¢°¢À» ÁöÆÛ·Î ¿¬°áÇؼ­ for¹®À» µ¹¸² # gradient descent function self.gd = theano.function([], self.ftn_eqn, updates=self.updates) problem = opt_problem() problem.plot() problem.build_gd() Xs = [] Ys = [] Xs.append(problem.x.get_value()) Ys.append(problem.y.get_value()) for epoch in xrange(20): if(epoch%5)==1: problem.lr.set_value(problem.lr.get_value()/2) f_val = problem.gd() Xs.append(problem.x.get_value()) Ys.append(problem.y.get_value()) plt.plot(Xs, Ys, '-ro') print(f_val) ---- numpy¿¡¼­ ¾ÆÀÌ°Õº¤ÅÍ, ¾ÆÀÌ°Õ º§·ù °è»ê °¡´É - matplotlib ´Â ¸ÅƲ·¦ÇÏ°í °ÅÀÇ ¶È°°Àº ÀÎÅÍÆäÀ̽º·Î ±¸¼ºµÇ¾îÀÖÀ½ - ´º·²³Ý ©¶§, ¹éÇÁ·ÎÆÛ°ÔÀÌ¼Ç ±¸Çö¾ÈÇÏ°í © ¼ö ÀÖÀ½. -- x = T.scalar() w = theano.shared(np.array(3,dtype=theano.config.floatX),borrow=True) obj = (1 - x*w)**2 #learn_w = (w, w-0.1*T.grad(obj,w)) learn = theano.function([x],obj) w = w-0.1*T.grad(obj,w) m_update = theano.function([],w) print(learn(2), m_update()) print(learn(2), m_update()) print(learn(2), m_update()) print(learn(2), m_update()) # updateµû·Î±¸ÇöÇغ¸±â