PyTorchã«ããç»ååé¡
深局åŠç¿ã®æç§æžã«ã¯ãå°éçã§ç解ã§ããªãçšèªããããããããŸããç§ã¯ãããæå°éã«æããPyTorchã®æäœã«æ £ããŠãããç°¡åã«æ¡åŒµã§ããäŸãåžžã«1ã€æããŸãããã®äŸãæ¬å šäœã§äœ¿çšããŠãã¢ãã«ããããã°ããæ¹æ³ïŒç¬¬7ç« ïŒãŸãã¯ã¢ãã«ãæ¬çªç°å¢ã«ãããã€ããæ¹æ³ïŒç¬¬8ç« ïŒã瀺ããŸãã
ãããã第4ç« ã®çµãããŸã§ãç»ååé¡åãã³ã³ãã€ã«ããŸãããã¥ãŒã©ã«ãããã¯ãŒã¯ã¯ãäžè¬çã«ç»ååé¡åãšããŠäœ¿çšãããŸãããããã¯ãŒã¯ã¯ç»åãæäŸããç°¡åãªè³ªåãããŸãïŒãããã¯äœã§ããïŒã
PyTorchã§ã¢ããªã±ãŒã·ã§ã³ãäœæããããšããå§ããŸãããã
åé¡åé¡
ããã§ã¯ãéãšç«ãåºå¥ã§ããç°¡åãªåé¡åãäœæããŸããã¢ãã«ã®èšèšãšéçºããã»ã¹ãç¹°ãè¿ããŠãã¢ãã«ãããæ£ç¢ºã«ããŸãã
å³ã§ã¯ 2.1ãš2.2ã¯ãéãšç«ã®æ å ãè¡šããŠããŸããéã«ååããããã©ããã¯ããããŸããããç«ã®ååã¯ãã«ããã£ã«ã§ãã
æšæºçãªåé¡ã®åé¡ã®ããã€ãã«ã€ããŠèª¬æããããšããå§ããŸãããã
æšæºçãªé£ãã
éãšç«ãåºå¥ã§ããããã°ã©ã ã®æžãæ¹ã¯ïŒãããããç«ã«å°»å°Ÿãããã®ãââãéã«é±ãããã®ãââã説æããäžé£ã®ã«ãŒã«ãäœæãããããã®ã«ãŒã«ãç»åã«é©çšããŠãããã°ã©ã ãç»åãåé¡ã§ããããã«ããŸããããããããã«ã¯æéãåŽåãã¹ãã«ãå¿ èŠã§ãã Manxç«ã«åºãããããã©ãããŸããïŒæããã«ç«ã§ããã尻尟ã¯ãããŸããã
ãããã®ã«ãŒã«ã䜿çšããŠèãããããã¹ãŠã®ã·ããªãªã説æããããšãããšããããã®ã«ãŒã«ã¯ãŸããŸãè€éã«ãªããŸãããŸããããžã¥ã¢ã«ããã°ã©ãã³ã°ã¯ç§ã«ãšã£ãŠã²ã©ãããšãèªããªããã°ãªããªãã®ã§ãããããã¹ãŠã®ã«ãŒã«ã®ã³ãŒããæåã§äœæããªããã°ãªããªããšããèãã¯æããããã®ã§ãã
ç»åãå ¥åãããšç«ãéãè¿ãæ©èœãå¿ èŠã§ãããã¹ãŠã®åºæºãå®å šã«ãªã¹ãããã ãã§ã¯ããã®ãããªé¢æ°ãæ§ç¯ããããšã¯å°é£ã§ããããããæ·±ãåŠç¿ã¯æ¬è³ªçã«ãæ§é ãäœæãããããã¯ãŒã¯ã«å€§éã®ããŒã¿ãæäŸãããããæ£ããçããäžãããã©ãããç¥ããããšããæ¡ä»¶ã§ãå ã»ã©è©±ããããããã¹ãŠã®ã«ãŒã«ãäœæãããšãã倧å€ãªäœæ¥ãã³ã³ãã¥ãŒã¿ãŒã«åŒ·å¶ããŸãããããç§ãã¡ãããããšããŠããããšã§ããããã«ãPyTorchã䜿çšããããã®ããã€ãã®åºæ¬çãªãã¯ããã¯ãåŠã³ãŸãã
ããããæåã«ããŒã¿
ãŸããããŒã¿ãå¿ èŠã§ããã©ã®ãããã®ããŒã¿ïŒããŸããŸãªèŠå ã«äŸåããŸãã第4ç« ã§ãããããã«ããã£ãŒãã©ãŒãã³ã°ææ³ã§ã¯ãã¥ãŒã©ã«ãããã¯ãŒã¯ããã¬ãŒãã³ã°ããããã«å€§éã®ããŒã¿ãå¿ èŠã§ãããšããèãã¯å¿ ãããçå®ã§ã¯ãããŸããããã ããããã§ã¯æåããå§ããŸããããã«ã¯éåžžã倧éã®ããŒã¿ãžã®ã¢ã¯ã»ã¹ãå¿ èŠã§ããéãç«ã®ç»åãããããå¿ èŠã§ãã
Googleã®ç»åæ€çŽ¢ãã倧éã®ç»åãããŠã³ããŒãããã®ã«æéãè²»ããããšãã§ããŸããããã£ãšç°¡åãªæ¹æ³ããããŸãããã¥ãŒã©ã«ãããã¯ãŒã¯ã®ãã¬ãŒãã³ã°ã«äœ¿çšãããç»åã®æšæºã³ã¬ã¯ã·ã§ã³ã¯ImageNetã§ãã1,400äžãè¶ ããç»åãš2äžã®ç»åã«ããŽãªãå«ãŸããŠããŸããããã¯ããã¹ãŠã®ç»ååé¡åãæ¯èŒããåºæºã§ãããããã£ãŠãå¿ èŠã«å¿ããŠä»ã®ãªãã·ã§ã³ãéžæããããšãã§ããŸãããããããç»åãååŸããŸãã
ããŒã¿ã«å ããŠãPyTorchã«ã¯ãç«ãšã¯äœããéãšã¯äœããå®çŸ©ããæ¹æ³ãå¿ èŠã§ããããã¯ç§ãã¡ã«ãšã£ãŠã¯ååã«ç°¡åã§ãããã³ã³ãã¥ãŒã¿ãŒã«ãšã£ãŠã¯é£ããã§ãïŒãããç§ãã¡ãããã°ã©ã ãäœæããçç±ã§ãïŒïŒãããŒã¿ã«æ·»ä»ãããã©ããªã³ã°ã䜿çšããŸããããã¯ç£èŠä»ãåŠç¿ãšåŒã°ããŸãã ïŒã©ã®ã©ãã«ã«ãã¢ã¯ã»ã¹ã§ããªã
å Žåã¯ããæ³åã®ãšãããç£èŠãããŠããªãæ©æ¢°åŠç¿ã䜿çšãããŸããïŒImageNetããŒã¿ã䜿çšããå Žåãã©ãã«ã«ã¯æ å ±ãå€ãããããã圹ã«ç«ã¡ãŸãããã¿ããŒãã£ããããã©ãŠããã³ã³ãã¥ãŒã¿ãŒã«ããŒã¯ããããšã¯ãç«ãéãšåãã§ã¯ãããŸããã
ãããã®ã©ãã«ãå€æŽããå¿ èŠããããŸããImageNetã¯èšå€§ãªæ°ã®ç»åã®ã³ã¬ã¯ã·ã§ã³ã§ãããããéãšç«ã®ç»åãšã¿ã°ä»ãURLããŸãšããŸããïŒhttps://oreil.ly/NbtEUïŒã
ãã®ãã£ã¬ã¯ããªã§download.pyã¹ã¯ãªãããå®è¡ãããšãURLããç»åãããŠã³ããŒããããé©åãªãã¬ãŒãã³ã°å Žæã«é 眮ãããŸããåã©ãã«ä»ãã¯ç°¡åã§ããã¹ã¯ãªããã¯ãç«ã®ç»åãåè»/ç«ã®ãã£ã¬ã¯ããªã«ä¿åããéã®ç»åãåè»/éã®ãã£ã¬ã¯ããªã«ä¿åããŸããã¹ã¯ãªããã䜿çšããŠããŠã³ããŒãããããªãå Žåã¯ããããã®ãã£ã¬ã¯ããªãäœæãã察å¿ããç»åãé©åãªå Žæã«é 眮ããã ãã§ããããã§ããŒã¿ãã§ããŸããããPyTorchãç解ã§ãã圢åŒã«å€æããå¿ èŠããããŸãã
PyTorchãšããŒã¿ããŒããŒ
ããŒã¿ã®èªã¿èŸŒã¿ãšãã¬ãŒãã³ã°å¯Ÿå¿åœ¢åŒãžã®å€æã¯ãå€ãã®å ŽåãæéãããããããããŒã¿ãµã€ãšã³ã¹ã®1ã€ã®é åã§ãã PyTorchã¯ãç»åãããã¹ãããŸãã¯ãªãŒãã£ãªã®ãããã§äœæ¥ããŠããå Žåã§ããéåžžã«ç°¡åã«ãã確ç«ãããããŒã¿çžäºäœçšèŠä»¶ãéçºããŸããã
ããŒã¿ãæäœããããã®2ã€ã®äž»ãªæ¡ä»¶ã¯ãããŒã¿ã»ãããšããŒã¿ããŒããŒã§ããããŒã¿ã»ããã¯ããã¥ãŒã©ã«ãããã¯ãŒã¯ã«éä¿¡ããããŒã¿ãåä¿¡ã§ããããã«ããPythonã¯ã©ã¹ã§ãã
ããŒã¿ããŒããŒã¯ãããŒã¿ã»ãããããããã¯ãŒã¯ã«ããŒã¿ã転éãããã®ã§ãã ïŒããã«ã¯ã次ã®ãããªæ å ±ãå«ãŸããå ŽåããããŸãïŒãããã¯ãŒã¯ã«ããŒã¿ãã¢ããããŒãããŠããã¯ãŒã«ãŒããã»ã¹ã®æ°ïŒåæã«ã¢ããããŒãããŠããç»åã®æ°ïŒïŒ
æåã«ããŒã¿ã»ãããèŠãŠã¿ãŸããããåããŒã¿ã»ããã¯ãç»åãé³å£°ãããã¹ãã3Dã©ã³ãã¹ã±ãŒããæ ªåŒåžå Žæ å ±ãªã©ãå«ãŸããŠãããã©ããã«ãããããããã®æœè±¡çãªPythonã¯ã©ã¹ã®èŠä»¶ãæºãããŠããéããPyTorchãšå¯Ÿè©±ã§ããŸãã
class Dataset(object):
def __getitem__(self, index):
raise NotImplementedError
def __len__(self):
raise NotImplementedError
éåžžã«ç°¡åã§ããããŒã¿ã»ããã®ãµã€ãºïŒlenïŒãè¿ãã¡ãœãããšãããŒã¿ã»ããããèŠçŽ ããã¢ã§ååŸã§ããã¡ãœããïŒlabelãtensorïŒã䜿çšããå¿ èŠããããŸããããã¯ããã¬ãŒãã³ã°ã®ããã«ããŒã¿ããã¥ãŒã©ã«ãããã¯ãŒã¯ã«äŸçµŠãããšãã«ãããŒã¿ããŒããŒã«ãã£ãŠåŒã³åºãããŸãããããã£ãŠãPyTorchãåŠçã§ããããã«ãç»åãååŸããŠãã³ãœã«ã«å€æããå ã«æ»ããŠããŒã¯ãä»ããããšãã§ããgetitemã¡ãœããã®æ¬äœãäœæããå¿ èŠããããŸããããã¯ãã¹ãŠæããã§ãããæããã«ãã®ã·ããªãªã¯ååã«äžè¬çã§ãããããPyTorchã䜿çšãããšã¿ã¹ã¯ãç°¡åã«ãªãã®ã§ã¯ãªãã§ããããã
ãã¬ãŒãã³ã°ããŒã¿ã»ããã®äœæ
torchvisionããã±ãŒãžã«ã¯ãåãã£ã¬ã¯ããªãã©ãã«ã§ããæ§é ã«ç»åããããšä»®å®ããŠãã»ãšãã©ãã¹ãŠãå®è¡ããImageFolderã¯ã©ã¹ãå«ãŸããŠããŸãïŒããšãã°ããã¹ãŠã®ç«ã¯catãšããååã®ãã£ã¬ã¯ããªã«ãããŸãïŒãç«ãšéã®äŸã«å¿ èŠãªãã®ã¯æ¬¡ã®ãšããã§ãã
import torchvision
from torchvision import transforms
train_data_path = "./train/"
transforms = transforms.Compose([
transforms.Resize(64),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225] )
])
train_data = torchvision.datasets.ImageFolder
(root=train_data_path,transform=transforms)
torchvisionã§ã¯ãç»åããã¥ãŒã©ã«ãããã¯ãŒã¯ã«å ¥ãåã«ç»åã«é©çšããå€æã®ãªã¹ããæå®ã§ãããããããã«ä»ã®äœããè¿œå ãããŸããããã©ã«ãã®å€æã§ã¯ãç»åããŒã¿ãååŸããŠãã³ãœã«ã«å€æããŸãïŒåã®ã³ãŒãã§ç€ºããtrans forms.ToTensorïŒïŒã¡ãœããïŒããããã»ã©æçœã§ã¯ãªãå¯èœæ§ã®ããä»ã®ããã€ãã®åŠçãå®è¡ããŸãã
ãŸããGPUã¯ãé«éã§æšæºãµã€ãºã®èšç®ãå®è¡ããããã«æ§ç¯ãããŠããŸããããããããããå€ãã®è§£å床ã®ç»åã®åæãããããŸããåŠçããã©ãŒãã³ã¹ãåäžãããããã«ããµã€ãºå€æŽå€æïŒ64ïŒã䜿çšããŠãåå ¥åç»åãåã64x64解å床ã«ã¹ã±ãŒãªã³ã°ããŸãã次ã«ãç»åããã³ãœã«ã«å€æããæåŸã«ç¹å®ã®å¹³åç¹ãšæšæºåå·®ç¹ã®ã»ãããäžå¿ã«ãã³ãœã«ãæ£èŠåããŸãã
å ¥åããã¥ãŒã©ã«ãããã¯ãŒã¯ã®ã¬ã€ã€ãŒãééãããšãã«å€æ°ã®ä¹ç®ãå®è¡ãããããšãäºæ³ããããããæ£èŠåã¯éèŠã§ããå ¥åå€ã0ãã1ã®éã«ä¿ã€ããšã§ãåŠç¿ãã§ãŒãºäžã®å€ã®å€§å¹ ãªå¢å ãé²ããŸãïŒççºåŸé åé¡ãšããŠç¥ãããŠããŸãïŒããã®éæ³ã®å身ã¯ãImageNetããŒã¿ã»ããå šäœã®å¹³åãšæšæºåå·®ã«ãããŸãããããªãã¯éãšç«ã®ãµãã»ããã®ããã«ãããç¹å¥ã«èšç®ããããšãã§ããŸããããããã®å€ã¯ããªãä¿¡é Œã§ããŸãã ïŒãŸã£ããç°ãªãããŒã¿ã»ããã§äœæ¥ããŠããå Žåããã®å¹³åãšåæ£ãèšç®ããå¿ èŠããããŸãããå€ãã¯åã«ImageNetå®æ°ã䜿çšãã蚱容å¯èœãªçµæãââå ±åããŸããïŒ
æ§æå¯èœãªå€æã«ãããããŒã¿æ¡åŒµã®ããã®ç»åå転ãç»åã·ãããªã©ã®ã¢ã¯ã·ã§ã³ãç°¡åã«å®è¡ããããšãã§ããŸããããã«ã€ããŠã¯ã第4ç« ã§èª¬æããŸãã
ãã®äŸã§ã¯ãç»åã®ãµã€ãºã64x64ã«å€æŽããŠããŸããæåã®ãããã¯ãŒã¯ã§ã®èšç®ãé«éåããããã«ããã®ã©ã³ãã ãªéžæãè¡ããŸããã第3ç« ã§èª¬æããæ¢åã®ã¢ãŒããã¯ãã£ã®ã»ãšãã©ã¯ãå ¥åã€ã¡ãŒãžã«224x224ãŸãã¯299x299ã䜿çšããŸããäžè¬ã«ãå ¥åãã¡ã€ã«ãµã€ãºã倧ããã»ã©ããããã¯ãŒã¯ã¯ããå€ãã®ããŒã¿ãåŠç¿ã§ããŸããã³ã€ã³ã®è£åŽã¯éåžžãããå°ããªãããã®ç»åãGPUã¡ã¢ãªã«åããããšãã§ããŸãã
ããŒã¿ã»ããã«é¢ããæ å ±ã¯ä»ã«ããããããããŸãããããã ãã§ã¯ãããŸããããããããã¬ãŒãã³ã°ããŒã¿ã»ããã«ã€ããŠãã§ã«ç¥ã£ãŠããã®ã«ããªãå¿ èŠä»¥äžã«ç¥ã£ãŠããå¿ èŠãããã®ã§ããããã
æ€èšŒããã³åç §ããŒã¿ã»ãã
ãã¬ãŒãã³ã°ããŒã¿ã»ãããèšå®ãããŸããããæ€èšŒããŒã¿ã»ããã§åãæé ãç¹°ãè¿ãå¿ èŠããããŸããããã§ã®éãã¯äœã§ããïŒæ·±å±€åŠç¿ïŒãããŠå®éã«ã¯ãã¹ãŠã®æ©æ¢°åŠç¿ïŒã®èœãšãç©Žã®1ã€ã¯ãéå°é©åã§ããã¢ãã«ã¯ããã¬ãŒãã³ã°ãããå 容ãèªèããã®ã«éåžžã«åªããŠããŸãããèŠãããšã®ãªãäŸã§ã¯æ©èœããŸãããã¢ãã«ã¯ç«ã®åçãèŠãŠãä»ã®ãã¹ãŠã®ç«ã®åçããããšããŸã䌌ãŠããªãå Žåãã¢ãã«ã¯ãããç«ã§ã¯ãªããšå€æããŸãããå察ã¯æããã§ãããã¥ãŒã©ã«ãããã¯ãŒã¯ããã®ããã«åäœããã®ãé²ãããã«ãã³ã³ãããŒã«ãµã³ãã«ãdownload.pyã«ããŒãããŸããã€ãŸãããã¬ãŒãã³ã°ããŒã¿ã»ããã«ãªãç«ãšéã®äžé£ã®ç»åã«ããŒãããŸããåãã¬ãŒãã³ã°ãµã€ã¯ã«ïŒãšããã¯ãšãåŒã°ããŸãïŒã®æåŸã«ããã®ã»ãããæ¯èŒããŠããããã¯ãŒã¯ã«åé¡ããªãããšã確èªããŸããå¿é ããªãã§ãã ããããã®ãã§ãã¯ã®ã³ãŒãã¯éåžžã«åçŽã§ããããã¯ãããã€ãã®å€æ°åãå€æŽãããåãã³ãŒãã§ãã
val_data_path = "./val/"
val_data = torchvision.datasets.ImageFolder(root=val_data_path,
transform=transforms)
åå®çŸ©ãã代ããã«ãå€æãã§ãŒã³ã䜿çšããŸããã
æ€èšŒããŒã¿ã»ããã«å ããŠãæ€èšŒããŒã¿ã»ãããäœæããå¿ èŠããããŸããããã¯ããã¹ãŠã®ãã¬ãŒãã³ã°ãå®äºããåŸã«ã¢ãã«ããã¹ãããããã«äœ¿çšãããŸãã
test_data_path = "./test/"
test_data = torchvision.datasets.ImageFolder(root=test_data_path,
transform=transforms)
äžèŠãããšãããŸããŸãªçš®é¡ã®ã»ãããè€éã§æ··ä¹±ããå¯èœæ§ãããããããã¬ãŒãã³ã°ã®ã©ã®éšåã§åã»ããã䜿çšãããã瀺ãè¡šããŸãšããŸããïŒè¡š2.1ïŒã
ããã§ãPythonã³ãŒããããã«æ°è¡äœ¿çšããŠããŒã¿ããŒããŒãäœæã§ããŸãã
batch_size=64
train_data_loader = data.DataLoader(train_data, batch_size=batch_size)
val_data_loader = data.DataLoader(val_data, batch_size=batch_size)
test_data_loader = data.DataLoader(test_data, batch_size=batch_size)
ãã®ã³ãŒãã§æ°ãã泚ç®ã«å€ããã®ã¯ãbatch_sizeã³ãã³ãã§ãã圌女ã¯ããã¬ãŒãã³ã°ãšæŽæ°ãè¡ãåã«ããããã¯ãŒã¯ãééããç»åã®æ°ã瀺ããŠããŸããçè«çã«ã¯ããã¹ãããŒã¿ã»ãããšãã¬ãŒãã³ã°ããŒã¿ã»ããã®äžé£ã®ç»åã«batch_sizeãå²ãåœãŠãŠããããã¯ãŒã¯ãæŽæ°ããåã«åç»åã確èªã§ããããã«ããããšãã§ããŸããå®éã«ã¯ãããã¯éåžžè¡ãããŸãããããã¯ãå°ãããã±ããïŒæç®ã§ã¯ãããã±ãããšããŠããäžè¬çã«ç¥ãããŠããïŒãå¿ èŠãšããã¡ã¢ãªãå°ãªããåç»åã«é¢ãããã¹ãŠã®æ å ±ãããŒã¿ã»ããã«æ ŒçŽããå¿ èŠããªãããã§ãããã±ãããµã€ãºãå°ããã»ã©ããããã¯ãŒã¯ããã®åŠç¿ãéããªããŸããã¯ããã«é«éã«æŽæ°ããŸãã PyTorchããŒã¿ããŒããŒã®å Žåãbatch_sizeã¯ããã©ã«ãã§1ã«èšå®ãããŠããŸããããããããããå€æŽããããšããå§ãããŸããç§ã¯64ãéžã³ãŸããããããªãã¯ç解ããããã«å®éšããããšãã§ããŸãGPUã¡ã¢ãªã䜿ãæããããšãªã䜿çšã§ããããããã±ãŒãžã®æ°ãããã€ãã®è¿œå ãã©ã¡ãŒã¿ãè©ŠããŠãã ãããããšãã°ãããŒã¿ã»ããã®ãã§ããæ¹æ³ãå®ââè¡ãããã³ã«ããŒã¿ã»ããå šäœãã·ã£ããã«ãããã©ãããããŒã¿ã»ããããããŒã¿ãååŸããããã«å¿ èŠãªã¯ãŒã¯ãããŒã®æ°ãæå®ã§ããŸãããã®ãã¹ãŠã¯ã§èŠã€ããããšãã§ããŸãPyTorchããã¥ã¡ã³ãã
ããã¯PyTorchã«ããŒã¿ãæž¡ãããšã«é¢ãããã®ãªã®ã§ãç»åã®åé¡ãéå§ããåçŽãªãã¥ãŒã©ã«ãããã¯ãŒã¯ãæ³åããŠã¿ãŸãããã
æåŸã«ããã¥ãŒã©ã«ãããã¯ãŒã¯ïŒ
ãŸããæãåçŽãªæ·±å±€åŠç¿ãããã¯ãŒã¯ãã€ãŸãå ¥åãã³ãµãŒïŒç»åïŒãšé£æºããå ¥åã¬ã€ã€ãŒããå§ããŸããåºåå±€ïŒ2ïŒã®åºåã¯ã©ã¹ã®æ°ã®ãµã€ãºããããŠãã®éã«é ãããå±€ãæåã®äŸã§ã¯ãå®å šã«ãªã³ã¯ãããã¬ã€ã€ãŒã䜿çšããŸããå³ã§ã¯ 2.3ã¯ã3ã€ã®ããŒãã®å ¥åã¬ã€ã€ãŒã3ã€ã®ããŒãã®é衚瀺
ã¬ã€ã€ãŒãããã³2ã€ã®ããŒãã®åºåã瀺ããŠããŸãã
ãã®äŸã§ã¯ã1ã€ã®ã¬ã€ã€ãŒã®åããŒãã次ã®ã¬ã€ã€ãŒã®ããŒãã«åœ±é¿ãäžããåæ¥ç¶ã«ã¯ããã®ããŒããã次ã®ã¬ã€ã€ãŒãžã®ä¿¡å·ã®åŒ·åºŠã決å®ããéã¿ããããŸããïŒãããã¯ãéåžžã¯ã©ã³ãã ãªåæåããããããã¯ãŒã¯ããã¬ãŒãã³ã°ãããšãã«æŽæ°ãããéã¿ã§ããïŒå ¥åããããã¯ãŒã¯ãééãããšããç§ãã¡ïŒãŸãã¯PyTorchïŒã¯ããã®ã¬ã€ã€ãŒã®éã¿ãšãã€ã¢ã¹ã«å ¥åãåçŽã«ãããªãã¯ã¹ä¹ç®ã§ããŸãããããã次ã®é¢æ°ã«æž¡ãåã«ããã®çµæã¯ã¢ã¯ãã£ããŒã·ã§ã³é¢æ°ã«å ¥ããŸããããã¯ãã·ã¹ãã ã«éç·åœ¢æ§ãå°å ¥ããæ¹æ³ã«ãããŸããã
ã¢ã¯ãã£ããŒã·ã§ã³æ©èœ
ã¢ã¯ãã£ããŒã·ã§ã³æ©èœã¯ããªãããŒã«èãããŸãããçŸåšèŠã€ããããšãã§ããæãäžè¬çãªã¢ã¯ãã£ããŒã·ã§ã³æ©èœã¯ãReLUãã€ãŸãä¿®æ£ãããç·åœ¢ãŠãããã§ããåã³è³¢ãïŒãã ããããã¯maxïŒ0ãxïŒãå®è£ ããé¢æ°ã§ãããããå ¥åãè² ã®å Žåã¯çµæã0ã«ãªããxãæ£ã®å Žåã¯å ¥åïŒxïŒã®ã¿ã«ãªããŸãããšãŠãç°¡åã§ãïŒ
ééããå¯èœæ§ãæãé«ããã1ã€ã®ã¢ã¯ãã£ããŒã·ã§ã³é¢æ°ã¯ãå€å€éããžã¹ãã£ãã¯é¢æ°ïŒsoftmaxïŒã§ããããã¯ãæ°åŠçãªæå³ã§ããå°ãè€éã§ããåºæ¬çã«ã0ãã1ãŸã§ã®å€ã®ã»ãããçæããåèšã§1ïŒç¢ºçïŒïŒã«ãªããå·®ã倧ãããªãããã«å€ã«éã¿ãä»ããŸããã€ãŸããä»ã®ãã¹ãŠããã倧ãããã¯ãã«ã§1ã€ã®çµæãçæããŸããåé¡ãããã¯ãŒã¯ã®æåŸã§ãå ¥åããŒã¿ãã©ã®ã¯ã©ã¹ã§ãããšãããã¯ãŒã¯ãå€æãããã確å®ã«äºæž¬ããããã«äœ¿çšãããããšããããããŸãã
ããããã¹ãŠã®ãã«ãã£ã³ã°ãããã¯ãã§ããã®ã§ãæåã®ãã¥ãŒã©ã«ãããã¯ãŒã¯ã®æ§ç¯ãéå§ã§ããŸãã
ãã¥ãŒã©ã«ãããã¯ãŒã¯ã®äœæ
PyTorchã§ãã¥ãŒã©ã«ãããã¯ãŒã¯ãæ§ç¯ããããšã¯ãPythonã§ããã°ã©ãã³ã°ããããšã«äŒŒãŠããŸãã torch.nn.Networkãšããã¯ã©ã¹ããç¶æ¿ãã__ init__ã¡ãœãããšforwardã¡ãœããã«ããŒã¿ãå ¥åããŸãã
class SimpleNet(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(12288, 84)
self.fc2 = nn.Linear(84, 50)
self.fc3 = nn.Linear(50,2)
def forward(self):
x = x.view(-1, 12288)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.softmax(self.fc3(x))
return x
simplenet = SimpleNet()
ç¹°ãè¿ããŸãããããã¯é£ããããšã§ã¯ãããŸããã initïŒïŒã§å¿ èŠãªèšå®ãè¡ããŸãããã®å Žåãã¹ãŒããŒã¯ã©ã¹ã³ã³ã¹ãã©ã¯ã¿ãŒãš3ã€ã®å®å šã«æ¥ç¶ãããã¬ã€ã€ãŒïŒPyTorchã§ã¯LinearãšåŒã°ããKerasã§ã¯DenseãšåŒã°ããŸãïŒãåŒã³åºããŸãã forwardïŒïŒã¡ãœããã¯ããã¬ãŒãã³ã°ãšäºæž¬ïŒæšè«ïŒã®äž¡æ¹ã§ãããŒã¿ããããã¯ãŒã¯ãä»ããŠã©ã®ããã«éä¿¡ããããã瀺ããŸãããŸããç»åå ã®3Dãã³ãœã«ïŒxãšyã«å ããŠ3ãã£ã³ãã«ã®è²æ å ±ïŒèµ€ãç·ãéïŒïŒãå€æããå¿ èŠããããŸã-泚æïŒ -æåã®ç·åœ¢ã¬ã€ã€ãŒã«æž¡ãããšãã§ããããã«1次å ãã³ãœã«ã«å€æããŸããããã¯ãviewïŒïŒã䜿çšããŠè¡ããŸãããããã£ãŠãã¬ã€ã€ãŒãšã¢ã¯ãã£ããŒã·ã§ã³é¢æ°ãé çªã«é©çšããsoftmaxåºåãè¿ãããã®ç»åã®äºæž¬ãååŸããŸãã
é ãã¬ã€ã€ãŒã®çªå·ã¯ä»»æã§ãããæåŸã®ã¬ã€ã€ãŒã®åºåã§ãã2ã¯ãç«ãŸãã¯éã®2ã€ã®ã¯ã©ã¹ã«äžèŽããŸããã¹ã¿ãã¯å ã§çž®å°ããã«ã€ããŠãã¬ã€ã€ãŒå ã®ããŒã¿ãçž®å°ããå¿ èŠããããŸããããšãã°ãã¬ã€ã€ãŒã50å ¥åãã100åºåã«ç§»è¡ããå Žåããããã¯ãŒã¯ã¯50æ¥ç¶ã100åºåã®ãã¡50ã«æž¡ãã ãã§åŠç¿ããäœæ¥ãå®äºãããšèŠãªãããšãã§ããŸããå ¥åã«é¢é£ããŠåºåã®ãµã€ãºãçž®å°ããããšã«ããããããã¯ãŒã¯ã®ãã®éšåã«ãããå°ãªããªãœãŒã¹ã§å ã®å ¥åã®ä»£è¡šæ§ãåŠç¿ãããããã«ããŠããŸããããã¯ãããããããããã¯ãŒã¯ãç»åã®ããã€ãã®éç«ã£ãç¹åŸŽãå®çŸ©ããããšãæå³ããŸããããšãã°ããã£ã³ãããŒã«ãèªèããããšãåŠç¿ããŸããã
äºæž¬ããããå ã®ç»åã®å®éã®ã©ãã«ãšæ¯èŒããŠããããæ£ãããã©ããã確èªã§ããŸãããã ããPyTorchãäºæž¬ã®æ£ãããäžæ£ç¢ºãã ãã§ãªããäºæž¬ã®æ£ãããäžæ£ç¢ºããå®éåã§ããããã«ããããã®äœããã®æ¹æ³ãå¿ èŠã§ããæ倱é¢æ°ã¯ãããè¡ããŸãã
èè ã«ã€ããŠ
Ian PoynterïŒIan PointerïŒ-Fortune 100ã®å€æ°ã®ã¯ã©ã€ã¢ã³ãåãã®æ©æ¢°åŠç¿ïŒè©³çŽ°ãªæè²æ¹æ³ãå«ãïŒã®ãœãªã¥ãŒã·ã§ã³ãå°éãšãããšã³ãžãã¢ããŒã¿ãµã€ãšã³ã¹ãçŸåšãYangã¯Lucidworksã§åããŠãããé«åºŠãªã¢ããªã±ãŒã·ã§ã³ãšNLPã®éçºã«åŸäºããŠããŸãã
»æ¬ã®è©³çŽ°ã«ã€ããŠã¯ãåºç瀟ã®ãŠã§ããµã€ããã芧ãã ãã
»ç®æ¬¡
» äœæ°åãã®æç²
ã¯ãŒãã³ã®25ïŒ å²åŒ-PyTorch
çŽçã®æ¬ã®æ¯æãæã«ãé»åæžç±ãé»åã¡ãŒã«ã§éä¿¡ãããŸãã