티스토리 뷰

반응형

 

 

 


자연어처리 ML Model 생성 준비하기
■ Playground 프로젝트 생성

iOS 환경에서 자연어처리를 위한 ML모델을 생성하기 위한 밑단계를 진행해보겠습니다. 

자연어처리 분석을 위한 json 데이터를 사용해서 학습된 MLDataTable을 생성하고, ML Model을 생성할 수 있습니다. 이번에 생성할 MLModel은 아마존의 댓글 리뷰 데이터 셋을 가진 json 파일을 활용해서 MLDataTable을 생성하고, 이를 통해서 학습된 MLModel을 생성할 예정입니다. 해당 MLModel을 통해서 특정 리뷰 내용이 긍정적인지, 부정적인지를 체크하는데 사용합니다. 

먼저, XCode를 실행하고, "Get started with a playground"를 선택해줍니다. 

 

 

이번 단계에서 진행할 MLDataTable 생성은 별도의 뷰를 생성하지 않으므로, Blank 옵션을 선택해서 Playground 프로젝트를 생성합니다. 

 

 

이름은 아마존 댓글 리뷰 데이터셋을 통해서, 특정 댓글이 긍정적인 댓글인지, 부정적인 댓글인지 판별할 MLModel을 생성할 것이기 때문에 ReviewClassifier라고 이름을 짓겠습니다. 

 

 

위와 같이 Playground 프로젝트가 생성되었습니다. 이어서 먼저, json파일을 추가해보겠습니다.

 

 


■ MLDataTable로 만들 json 파일 추가하기

추가해야 하게 될 파일은 위 사진에서 볼 수 있는 amazon-reviews.json, testing-reviews.json 두 개의 파일입니다. 
amazon-reviews.json은 실제 학습시킬 데이터, testing-reviews.json은 테스팅에 사용할 json데이터를 담게 됩니다. 

두 개의 파일을 Playground 프로젝트의 Resources 폴더에 추가시켜줍니다. 해당 두 개의 json파일은 MLDataTable 생성에 사용됩니다.

두 개의 json파일은 모두 "test", "label" 두개의 키를 갖는 객체 리스트형태를 띠고 있습니다. amazon-reviewss.json, testing-reviews.json 파일의 내용은 대략적으로 아래와 같은 내용을 갖고 있습니다.

 


- amazon-reviews.json

[
  {
    "text": "So there is no way for me to plug it in here in the US unless I go by a converter.",
    "label": "negative"
  },
  {
    "text": "Good case",
    "label": "Excellent value."
  },
  {
    "text": "Great for the jawbone.",
    "label": "positive"
  },
  {
    "text": "Tied to charger for conversations lasting more than 45 minutes.MAJOR PROBLEMS!!",
    "label": "negative"
  },
  {
    "text": "The mic is great.",
    "label": "positive"
  },
  {
    "text": "I have to jiggle the plug to get it to line up right to get decent volume.",
    "label": "negative"
  },
  {
    "text": "If you have several dozen or several hundred contacts then imagine the fun of sending each of them one by one.",
    "label": "negative"
  },
 .
 .
 // 중간내용 생략
 .
 .
  {
    "text": "If you have several dozen or several hundred contacts then imagine the fun of sending each of them one by one.",
    "label": "negative"
  }
]

 


- testing-reviews.json

[
    {
        "text": "This is the phone to get for 2005.... I just bought my S710a and all I can say is WOW!",
        "label": "positive"
    },
    {
        "text": "Buttons are too small.",
        "label": "negative"
    },
    {
        "text": "Just reading on the specs alone makes you say WOW.",
        "label": "positive"
    },
    {
        "text": "Love it.. Great armband.",
        "label": "positive"
    },
    {
        "text": "I really like this product over the Motorola because it is allot clearer on the ear piece and the mic.",
        "label": "positive"
    },
   .
   .
   // 중간내용 생략
   .
   .
    {
    "text": "If you have several dozen or several hundred contacts then imagine the fun of sending each of them one by one.",
    "label": "negative"
    }
]

 

 

 

 

 


■ MLDataTable 생성을 위한 코드작성

MLModel 생성 전, macOS환경에서 MLDataTable을 만들기 위해 CreateML, Cocoa를 import 해줍니다. 

 

 

MLDataTable 생성 전, json파일의 경로를 생성하는 코드입니다. Bundle.main.url 타입 메서드를 통해 json파일의 경로를 생성해줍니다. 

Cocoa를 import해주어야 Bundle을 사용할 수 있습니다.

 

위의 코드로 trainingDataFileURL, testingDataFileURL 두개의 URL이 생성됩니다. 이제 MLDataTable을 생성할 준비가 되었습니다.

 

 

MLDataTable 생성을 위해서는 try문을 사용해야합니다. try문 사용을 위해 do-try, catch 문법을 사용합니다. 

14 ~ 15행) 앞서 생성한 두 개의 URL, trainingDataFileURL, testingDataFileURL을 사용해서 trainingDataTable, testingDataTable을 생성하고 있습니다. 

17 ~ 24행) 생성한 MLDataTable의 행, 열 크기 확인을 위한 stats 문자열을 생성합니다. 만약 try문을 통해서 정상적으로 MLDataTable이 생성되었다면, 아래의 stats 문자열 생성 및 출력이 실행되게 됩니다.

다수의 행으로 문자열을 정의하고자 할때 해당 문자열의 위 아래로 """를 지정해서 문자열을 정의할 수 있습니다. 


25 ~ 27행) 만약 MLDataTable 생성을 위한 try 작업이 실패한다면, 해당 catch문이 실행되며, 에러문구를 출력하게 됩니다.

 


■ MLDataTable 생성코드 실행결과

지금까지 자연어처리 목적 MLModel(리뷰 긍정/부정형 분석 모델) 학습에 사용할 데이터셋이 담긴 json파일을 통해 MLDataTable 생성작업을 진행했습니다.

만약 정상적으로 MLDataTable이 생성되었다면, 작업이 진행 된 후, trainingDataTable, testingDataTable의 각 행/열 크기가 출력됩니다. 지금까지 작성한 코드를 실행해보면 아래와 같은 결과를 확인할 수 있습니다. 

 

 

정상적으로 MLDataTable이 생성되었으며, 각각의 MLDataTable trainingDataTable, testingDataTable rows, columns가 출력되는 것을 확인할 수 있습니다. (trainingDataTable : 935행 2열, testingDataTable 46행 2열)

여기에서 2열이 되는 이유는 json파일에 정의된 객체리스트 내 객체가 "test", "label" 두 개의 키 값을 갖고 있기 때문입니다.

 

 

다음 포스팅에서 MLDataTable을 활용해서 MLModel을 생성하고 활용해보도록 하겠습니다. 

 

 

반응형
댓글
반응형
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함