logo

Keras, Tensorflow va Torch kutubxonalari.

Загружено в:

08.08.2023

Скачано:

0

Размер:

2081.892578125 KB
Mavzu: Keras, Tensorflow va Torch kutubxonalari.
                                          Reja:
1.  Keras, Tensorflow va Torch kutubxonalari xaqida 
2. K e	r a	sd a c	h iz i	q li  	r e	gr e	ss iy a 	m	od eli	n i  qurish
3. 	
Python  	keras  va 	tensorf low  	kutubx	ona l	aridan	 foyda l	anish.
K eras   –  b u P y t hon-da  y oz i lgan,  T hea n o  y oki   Tensor f low-ning   a sos i ga   quril g an 
O chiq   K odli net ro n  t a r m o q   kut u b x o n a s i dir. U  m odull i ,   te z k o r  v a   ish l at ish uc hu n 
q ulay   holatda  G o o g l e   k o m pa n i y a s i  m uhandisi  F ran s ua  C hol e t  is h l a b   c hi q q a n. 
K eras  –  b u   hisob- ki toblar n i a m a lg a  os hi r ish  u c hun   Tensor Fl ow,   CNTK   y ok i 
Thea n o   “ Backend”  k ut u bxonalar i d a n   f o y d al a n a dig a n ,  y u qo r i   dara j ada  i s hla y d i gan
A PI(a p p lic a ti o n  p rogra mm in g  interfa c e) di r.
K eras ni m a?
K era s ni n g  i sh l a b c hiq ilishiga   a s osiy   sab a b   shuki   –   u n ga c ha bos h qa ne y r o n 
t ar m oqla r i   ku t ub x o n a la r i d a n  f o y dal a ni s h n o qula y roq   bo ` lgan  y a ’ n i   sint a ksisi
qi y in ro q b o ` l g a n.  K e r as  y uqo r i - d ara j a li   A P I   m od ella r n i  y aratish,   qa t la m l ar n i belgila s h  y oki   b i r   n e c h t a   k iri s h- c hiqi s h  m o d e l la r i n i   bo s h qar is h i m konini   ber a di. 
K eras  m od e l n i  y o`q ot ish (Loss   f unction)  v a   opti m i zatsi y a ( O pti m izat i on function)
f u n k si y a s i   bil a n ko m pil y atsi y a   qil a d i ,   va  m odelni  o `q it i sh ni   f i t   f u nksi y asi   o r qa l i 
a m al g a   oshiradi.
K era s d a ne y r o n  t ar m o q qurish
K eras ne y ron  t a r m o q   m ode l ini j u da   tez  t a y y or l ash  i m k o nini  b e r ad i .  B i r   ne c h a 
q ato r l i   oddiy   tar m o q   m ode l ini  y arati s h  u ch un ,  K er a s bu   b o rada   y ordam   b e r i shi
m u m kin .  Q u y id a g i   m i s o lga   qar an g:
K era s d a ne y r o n  t ar m o q qurish   ( I n pu t )
m odel = Sequ e nti a l()
m odel.add(Dense ( 512,  i np u t _ s hap e =(7 8 4 , )))
m od el.a d d (Ac ti v a ti o n('re l u'))
m odel.add(Dro p o ut (0.2)) K era s d a ne y r o n  t ar m o q qurish   ( H idd e n)
m odel = Sequ e nti a l()
m odel. ad d(Dense ( 51 2 ,  i npu t_ shape=(784 , )))
m odel.add(Act i va t i on('relu'))
m odel. ad d(Dropout ( 0. 2 ))
m odel.add(Dense ( 51 2 ))
m odel.add(Activation('re l u'))
m odel. ad d(Dropout ( 0. 2 ))
K era s d a ne y r o n  t ar m o q qurish   ( O ut p ut )
m odel = Sequ e nti a l()
m odel.add(Dense ( 512,  i np u t _ s hap e =(7 8 4 , )))
m odel.add(Activation('re l u'))
m odel.add(Dro p o ut (0.2)) m odel.add(Dense ( 51 2 ))
m od el. ad d( Ac t iva tio n ( 'r e l u ' ))
m odel.add(Dro p o ut (0.2))
m odel. ad d(Dense ( 10 ))
m odel.add(Act i va t i on('sof t m ax'))
K era s ni n g a s o s i y   t u s h un c h a la r i
K era s d a g i  a so s iy   s truk tura   bu  m od e l. M od ellar  b ir qa n cha   q a t l a m lar d an  t as h k i l
t o p i s h i   m u m kin .  K er a s d a   qat l a m larning   bir ne c hta  t uri  m av j u d .
◦   Ket m a- k et tar ti bl i ( S e quent ia l )  m odel
◦   K o n v ol y u t s ion qa t l a m
◦   M a xP o oli n g  q a t l am
◦   Zi c h  ( De n s e )   qatl a m
◦   Dr o p o u t   q a t l am Ket m a-ket  t a r t i b l i ( S e que n tial)  m od e l
Ket m a-ket  t a r t ibli  m ode ln i n g   a so s iy   g' o y asi shun c h a ki Ker a s   q a t la m l ar i ni  k e t m a -
k et   t a r t ib da jo y lash t i r is h dir. Ya ` n i   q a tla m lar  tu r g a n  t ar ti bi b o ` y i c ha  is h g a 
tush i r ila d i
Kon v o l y utsion   q a tl a m
Kon v o l y utsion   q a tl a m   – b u  m as s ivlar  u st i da  b ir   ne c h t a   f i l t r l a r d an  f o y dal a n g a n  
h o l da ula r d a n tu r li   xil   xu s u s i y a t la r n i   a j r a tib   olish uc hu n   qo`ll an ilad i g a n qa t la m di r
Kon v o l y utsion   q a tl a m
ReLU-ni f a o ll as h ti r i s h   fu n k t si y as i d a n  f o y dal a n a dig a n  va   kirish   shak l i(i n put  s h a pe)
3 2 0 x 3 2 0 x3   b o ' lgan   4 8  t a   3x3   o'lcha m dagi f i lt r i bo`lg a n   konvo l yutsi o n  q at la m .
i nput_sh a pe= ( 32 0 , 3 2 0, 3 )   #this is   the  i np u t  sh a p e   of  an   i m a ge 320 x 320 x 3
m odel.add( C o nv2D(48, (3,  3 ), ac t i va ti o n = ' r e l u',   i np u t _ s h a pe=   i n put_ s hape))
Kon v o l y utsion   q a tl a m ning   b oshqacha   k o `rinishi:
m odel. ad d(Conv2 D ( 4 8 ,  ( 3 ,  3 ), a c t ivati o n = 're l u'))
Ma x Po oling   qatlam Maxp o oli n g   -  b u   har  bi r  fi lte r dan   o`tka z il a y otkan  q iy m atlar d a n   e n g katta s i n i
o l i sh di r.  O datda  m axpoo l ing   qan a da y dir  m a ss i v da  q o`llan ilga nda n a tij a viy 
m assivning   o ` l c ha m i   ki chk i na r o q   b o ` la d i .
( 2 , 2)   fi lt e rga  e ga   b o `lg a n  m axpooling qa t l a m i
m odel.add(M a x P ool i ng2D ( po o l_size= ( 2, 2)))
Z i ch   (D e nse) q at lam
B o shqa c ha   qi l i b  a y tga n da,   zi c h   q a t lam   b i r-b i r i ga  t o'l i q bog'l an g a n  q at la m di r,  y a ' n i 
q a t la m d a g i  b a r c h a n e y r onl ar   ke y in gi  q a t l a m   b i lan b o g'langan va bu   q a t l a m d an   j u d a
ko ` p   f o y dal a nil a d i .
2 56   ta  c h iqish   q i y m atla r i bo`lg a n  z ich   q a tl a m
m odel. ad d(Dense ( 25 6 , ac t i vat i o n = ' re l u'))
Dro p out   qa t la m i Dro p out   qatla m i  o `q it ish jara y o n ida   ne y ron   tar m oqd a gi   ah a m i y ati  y o ’ q   b o ’lga n
n e y r o n l a r n i   ta s hl a b yubo r ish   a m a li n i ba j a r i s h   uc hu n   ishl a til a di
K eras  m odelni   ishga  t u s huri s h   , o'q i tish va   b a holash
Model   t u z i l g a n d a n so'n g ,   o'qitish bo s hlan a di.  M o de l n i birin c hi   b o ' lib   lo s s 
f u n k t s i y asi   va  o pti m i za t s i y a funktsi y asi b il an ko m pil y atsi y a q i lish   talab   qilin ad i. 
Bu  t a r m o q  o g ' i r l i k k o effitsi e ntla r i ni (weig h ts)   o' z ga r ti ri shga i m ko n   ber ad i  va  
x atoli k ni(loss)  m ini m alla s htiradi.  L o ss   f unksi y asi   –  b u   q u r il g a n  m odelning  o ’ q u v
t anl a n m aga nisba t an   q a ncha l ik  t o ’ g’ri  s hak l l a nti r ilg a nl i gini   b a h olash   usu l i 
hisoblana d i.
O p ti m i zat s i y a   f un ksi y asi   –   bu  t ar m o qning  o g ' i r l i k koe f fitsi e ntla r ini (wi eg ht s ) va
o z od   ( bias)   la r i n i o` z ga r ti r gan h o ld a   x a tolikni ka m a y tir u vc h i fu n ksi y adir.
m odel.co m pile( l oss = 'm ean_squar ed _error',   opti m izer='ada m ')
K eras  m odelni   ishga tus h uri s h   , o' qi ti s h va   b a hol a sh
Modelni  o `q iti sh n i   b o s h l a s h u c h u n   u n g a   v a lid a tsi y a va  o ` qitish(tra i ni n g) 
m a` lu m otl a r i n i   be r ish   ker a k shuningdek  p a ket   o ` l c ha m i  va  si k l l a r so n i   ham
belgilan ad i
m od el.fi t ( X _tr a i n ,  X_ t r a i n ,   ba t c h _siz e =32,   e p ochs=10,
validation_d ata=( x _ val,  y _va l ) )
Ox i r gi  b o s q ich  e sa  m ode l ni   t e st   m a`lu m o t lari b i lan   b a h olash
s c o re =  m odel. e v a lu a te ( x_t e st,  y _t e st ,   bat c h_size = 32) Ke	r a	sd a c	h iz i	q li  	r e	gr e	ss iy a 	m	od eli	n i 	qu	r ish
Modelni   o`qit g andan   so `ng n a t i ja   qu y ida g i ch a   bo ` l a d i
Bosh l a n g ` ich   o` g irli k   k oeffitsie n tla r i(ini t ial  w e i g h t s ):  w :   0.37,   b :   0.00  
O p t i m a l la s h t i ril g an o` g i rli k  k oeffitsientl a ri:   w:   3.70,   b :   0.61 Py t hon Vir t ual envi r on m ent   (venv)
c:\>p y t hon -m   v env   c:\path\ t o\m y env   M isol  u c hun  “ M a chine L e a rnin g ” no m li  
p ap k a  m a v j ud.   Shu p a p k ani  i c h i g a  k irib c m d o y nasi   o r q ali   y uq o rida g i   k od  i sh g a 
t us huril a d i
N atij a da   M a ch i neLe a r n i ng   pap k as i n i ng   ic h i da  “ v en v ”   no m l i   p a p k a  v a un i ng  
i c h i d a   j o ri y  k o m p y u t er g a o ’r n a t il g an python   n i ng   v irtual   m uhiti  y ar a t i la d i
Py t hon Vir t ual envi r on m ent   (venv) P y thon  	Vi r t	ual   e	n v	i ronm e	n t 	( ve	n v	)  ni  	ish g	a  t	ushi r	ish
Endi yaratilgan   virtual   muh i tni ishga   tushirib ke r akli k u tu b x o nalarni   o’rnatish 
loz i m   b o ’ladi,   bu  a malni c m d   o rqali yoki  “ Machin eL ear n ing”   loyihasini  
PyCha r m   das t uri   orqali   ochib, shu   muhitda   h a m   a malga  o shirish   mumkin.
venv\Script s \activ a te. b at
N aitjada  v i r t u al   muhit ish g a tushadi	
V
e	n v 	da   ker	a k	li  pa k et	la r	ni  	o ’ r	na t	ish  Bu yer d an quyidagi b u yr u q   orqali  
ma v jud   o’rnatilgan  p aketlar n i   ko’r i sh  m u m kin
pip list pip install pa c kage- n a me
pip install  t en s o rfl o w
pip install   numpy
pip install  k eras
Endi shu yerda ba r c h a k er a kli bo’lgan pa k etlar n i
m axsus  b uyruq  o rqali   o’rn a tish  m u m kinV
e	n v 	da   ker	a k	li  	pa k et	la r	ni  	o ’ r	na t	ish  Us h bu m o dullar o’rn a tilgan d an 
k eyin pip li s t buyrugi   o r q ali   tekshirib   ko’r a miz
P yt h on   virt u al   muhitda  ( v e nv)   j o riy   m uhitda   o’rnatilg a n m o dullarni saqlash  v a  
k e yin g i l o y i h a   uc h un shu  mo dullarni i s hl a tish imk o n i ya t i   m a vjud. Bu n ing  u ch u n 
quyidagilar a m alga   oshirili s hi l o zim
pip free z e	
V
e	n v 	da   ker	a k	li  	pa k et	la r	ni  	o ’ r	na t	ish Kompyu t er  x ot irasida  ( mi s ol uchun C:\U s er \ De s c t op) “requir e m e n t s.tx t ”   f a yl  
ya r atiladi  v a ushbu f a ylga ras m da   ko’ r satilgan   m o dul l ar  v a ularning   vers i yalari 
h a qi d agi ma’lum o t  k o’chirib  o lina d iV
e	n v 	da   ker	a k	li  	pa k et	la r	ni  	o ’ r	na t	ish
Us h bu  y aratil g an   “requir e ments . tx t ”   f a yli,  k eyinc h al i k  y ar a tilishi  m u mkin 
b o ’lg a n  v irtual   m uhit   uc h un ta y y or   m o dullar r o ’yhati   hisoblanadi  v a   j o r i y  l o y iha 
u c h un   “v e n v” yar a til g a n dan  k eyin q u yidagi b u yruq  o rqali fa y l   ic h ida  k eltiril ga n  
m o dullarni bir ur u nish d a   o ’ r n atish  m u mkin
D emak  P ython   virtual   m uh i ti  v a   “ pip” orqali  l o yiha uc h un kerakli ba r c h a  
m o dullar   o’rnatilga n dan k e yin virtual   muhitni  “ d eact i va t e ”   qilish  k er a k  b o ’ladi Foydala n ilgan  a d a bi y o tl ar
Aureli a n   Geron,   Hands   on   M a c h in e   Lear n i n g   w i t h   S c ikit- L ea r n
Keras&Tensorfl o w   //   Sec on d   edition   Con c ept s ,   T o o l s,   a n d   Te c hn i q u e s   to   Bu i l d
Int e lli g e n t   S y st e m s ,   20 1 9 ,  5 10 P a g e s .
Pr i m oz   P o to c nik,   Neur a l   Netw o rks:   MA T LAB   e x a m p l es   //   Neur a l   Netwo rk s  
c o u rs e (pra ct ic a l exa m pl e s ) © 2012
h t t ps://towa r dsdat a s c i ence . c o m /f i rst-n e ur al -network- f or - beg in n e rs- e xpl a i n ed-
w ith-code - 4c f d37 e 06eaf
h t t ps://w w w. m athw o rks. c o m /help/ t h in gsp e ak / cre a te- an d-tra i n- a - feed f orwa r d -
neural- n e t w ork.ht m l
ht    t        ps://w    w    w.    m    athw    o        rks.    c   o   m    /help/d    e   e   p   lea    r   ni    n   g/    e   xa    m    ples/c    r   ea    te    -si    m    ple    -       
de    e   p-lea    r   ning-ne    t        w    o   r   k-for-cl    a   ssi    f   ic    a   tion    .h    t   m    l  
https: / /w w w.t u torial s po i n t . c o m /
art i fi c i a l_ n eur a l _ network/a r ti f i c i al_neur al _n e t w or k _b asic_conc e pt s . htm

Mavzu: Keras, Tensorflow va Torch kutubxonalari. Reja: 1. Keras, Tensorflow va Torch kutubxonalari xaqida 2. K e r a sd a c h iz i q li r e gr e ss iy a m od eli n i qurish 3. Python keras va tensorf low kutubx ona l aridan foyda l anish. K eras – b u P y t hon-da y oz i lgan, T hea n o y oki Tensor f low-ning a sos i ga quril g an O chiq K odli net ro n t a r m o q kut u b x o n a s i dir. U m odull i , te z k o r v a ish l at ish uc hu n q ulay holatda G o o g l e k o m pa n i y a s i m uhandisi F ran s ua C hol e t is h l a b c hi q q a n. K eras – b u hisob- ki toblar n i a m a lg a os hi r ish u c hun Tensor Fl ow, CNTK y ok i Thea n o “ Backend” k ut u bxonalar i d a n f o y d al a n a dig a n , y u qo r i dara j ada i s hla y d i gan A PI(a p p lic a ti o n p rogra mm in g interfa c e) di r. K eras ni m a? K era s ni n g i sh l a b c hiq ilishiga a s osiy sab a b shuki – u n ga c ha bos h qa ne y r o n t ar m oqla r i ku t ub x o n a la r i d a n f o y dal a ni s h n o qula y roq bo ` lgan y a ’ n i sint a ksisi qi y in ro q b o ` l g a n. K e r as y uqo r i - d ara j a li A P I m od ella r n i y aratish, qa t la m l ar n i

belgila s h y oki b i r n e c h t a k iri s h- c hiqi s h m o d e l la r i n i bo s h qar is h i m konini ber a di. K eras m od e l n i y o`q ot ish (Loss f unction) v a opti m i zatsi y a ( O pti m izat i on function) f u n k si y a s i bil a n ko m pil y atsi y a qil a d i , va m odelni o `q it i sh ni f i t f u nksi y asi o r qa l i a m al g a oshiradi. K era s d a ne y r o n t ar m o q qurish K eras ne y ron t a r m o q m ode l ini j u da tez t a y y or l ash i m k o nini b e r ad i . B i r ne c h a q ato r l i oddiy tar m o q m ode l ini y arati s h u ch un , K er a s bu b o rada y ordam b e r i shi m u m kin . Q u y id a g i m i s o lga qar an g: K era s d a ne y r o n t ar m o q qurish ( I n pu t ) m odel = Sequ e nti a l() m odel.add(Dense ( 512, i np u t _ s hap e =(7 8 4 , ))) m od el.a d d (Ac ti v a ti o n('re l u')) m odel.add(Dro p o ut (0.2))

K era s d a ne y r o n t ar m o q qurish ( H idd e n) m odel = Sequ e nti a l() m odel. ad d(Dense ( 51 2 , i npu t_ shape=(784 , ))) m odel.add(Act i va t i on('relu')) m odel. ad d(Dropout ( 0. 2 )) m odel.add(Dense ( 51 2 )) m odel.add(Activation('re l u')) m odel. ad d(Dropout ( 0. 2 )) K era s d a ne y r o n t ar m o q qurish ( O ut p ut ) m odel = Sequ e nti a l() m odel.add(Dense ( 512, i np u t _ s hap e =(7 8 4 , ))) m odel.add(Activation('re l u')) m odel.add(Dro p o ut (0.2))

m odel.add(Dense ( 51 2 )) m od el. ad d( Ac t iva tio n ( 'r e l u ' )) m odel.add(Dro p o ut (0.2)) m odel. ad d(Dense ( 10 )) m odel.add(Act i va t i on('sof t m ax')) K era s ni n g a s o s i y t u s h un c h a la r i K era s d a g i a so s iy s truk tura bu m od e l. M od ellar b ir qa n cha q a t l a m lar d an t as h k i l t o p i s h i m u m kin . K er a s d a qat l a m larning bir ne c hta t uri m av j u d . ◦ Ket m a- k et tar ti bl i ( S e quent ia l ) m odel ◦ K o n v ol y u t s ion qa t l a m ◦ M a xP o oli n g q a t l am ◦ Zi c h ( De n s e ) qatl a m ◦ Dr o p o u t q a t l am

Ket m a-ket t a r t i b l i ( S e que n tial) m od e l Ket m a-ket t a r t ibli m ode ln i n g a so s iy g' o y asi shun c h a ki Ker a s q a t la m l ar i ni k e t m a - k et t a r t ib da jo y lash t i r is h dir. Ya ` n i q a tla m lar tu r g a n t ar ti bi b o ` y i c ha is h g a tush i r ila d i Kon v o l y utsion q a tl a m Kon v o l y utsion q a tl a m – b u m as s ivlar u st i da b ir ne c h t a f i l t r l a r d an f o y dal a n g a n h o l da ula r d a n tu r li xil xu s u s i y a t la r n i a j r a tib olish uc hu n qo`ll an ilad i g a n qa t la m di r Kon v o l y utsion q a tl a m ReLU-ni f a o ll as h ti r i s h fu n k t si y as i d a n f o y dal a n a dig a n va kirish shak l i(i n put s h a pe) 3 2 0 x 3 2 0 x3 b o ' lgan 4 8 t a 3x3 o'lcha m dagi f i lt r i bo`lg a n konvo l yutsi o n q at la m . i nput_sh a pe= ( 32 0 , 3 2 0, 3 ) #this is the i np u t sh a p e of an i m a ge 320 x 320 x 3 m odel.add( C o nv2D(48, (3, 3 ), ac t i va ti o n = ' r e l u', i np u t _ s h a pe= i n put_ s hape)) Kon v o l y utsion q a tl a m ning b oshqacha k o `rinishi: m odel. ad d(Conv2 D ( 4 8 , ( 3 , 3 ), a c t ivati o n = 're l u')) Ma x Po oling qatlam