Watson Machine Learning의 AI Function 기능을 사용하여 SPSS Modeler의 기계 학습 모델을 Function 정의

10673 단어 WatsonMachineLearning

소개



IBM Watson의 새로운 서비스 AI OpenScale은 타사 클라우드의 머신러닝 모델을 포함하여 관리할 수 있는 프로덕션 운영 중인 머신러닝 모델을 위한 서비스입니다.
그러나 대상이 될 수 있는 서비스에는 몇 가지 제약이 있습니다. 예를 들어, IBM Cloud에서 작성한 기계 학습 모델이라도 SPSS Modeler는 관리할 수 없습니다. 그러한 경우에 이용할 수 있는 것이 Watson Machine Learing의 새로운 기능인 AI Function입니다. 이것은 Python의 함수를 래퍼로 하고, 이 함수를 Watson Machine Learning에 등록하는 것으로, 결과적으로 AI OpenScale의 관리 대상으로 할 수 있습니다.
이 기사에서는 Watson Studio의 SPSS Modeler에서 만든 기계 학습 모델을 AI Function에 등록하는 절차를 샘플로 제공합니다.

전제



AI Function은 아직 베타 버전의 단계이며 지금 이 기능을 실행하려면 Watson Machine Learing의 베타 버전 인스턴스를 만들어야 합니다. 지금이라면 무료로 만들 수 있기 때문에 기회입니다.



SPSS의 기계 학습 모델은 SPSS 클라우드 버전으로 간단한 기계 학습에서 만든 것을 대상으로 합니다.

Python을 통한 기계 학습 모델 호출 코딩



첫 번째는 Python으로 기계 학습 모델 호출을 코딩하는 것입니다. 아래의 예는 우연히 Watson Studio의 SPSS Modeler에서 만든 기계 학습 모델을 호출하는 코드입니다.

# 認証情報
wml_credentials = {
  "apikey": "xxxxxx"
# 以下省略
}

from watson_machine_learning_client import WatsonMachineLearningAPIClient
client = WatsonMachineLearningAPIClient(wml_credentials)

# scoring url はMachine Learningの管理機能で取得したものを利用します。
spss_scoring_url = 'xxxxx'

# 呼出し時のパラメータ
spss_params = {
    "fields": ["CLASS", "AGE", "BP", "AL", "SC", "POT", "PCV"],
    "values": [[None, 75, 70, 0, 0.8, 3.5, 46]]}

# モデル呼出し
spss_scores = client.deployments.score(spss_scoring_url, spss_params)

# 結果表示
print(spss_scores)

이 호출 결과가 다음과 같은 형태라고 가정합니다.
{'values': [[None, 75, 70, 0, 0.8, 3.5, 46, '1_Training', 'ckd', 0.998594297142512, 0.998594297142512, 'ckd', 0.998594297142512]], 'fields': ['CLASS', 'AGE', 'BP', 'AL', 'SC', 'POT', 'PCV', 'Partition', '$L-CLASS', '$LP-CLASS', 'prediction', 'prediction_classes', 'probability']}

SPSS 호출 함수 정의



이제 다음과 같은 코드로 SPSS 호출을 위한 함수를 정의합니다.
조금 어려운 구현이지만, score_generator 라고 하는 인스턴스를 생성하면(자), 이것이 기계 학습 모델 호출의 결과를 돌려주는 함수가 되어 있는 것을 알 수 있습니다.
ai_params = {"wml_credentials": wml_credentials, 
             "spss_scoring_url": spss_scoring_url}
def score_generator(params=ai_params):
    from watson_machine_learning_client import WatsonMachineLearningAPIClient
    wml_credentials = params["wml_credentials"]
    spss_scoring_url = params["spss_scoring_url"]
    client = WatsonMachineLearningAPIClient(wml_credentials)
    def score(payload):
        spss_scores = client.deployments.score(spss_scoring_url, payload)
        fields = spss_scores['fields']
        values = spss_scores['values']
        ret_scores = {'values': values, 'fields': fields}
        return ret_scores
    return score

정말 그렇게 되어 있는지, 아래의 코드로 테스트해 보겠습니다.
score = score_generator()
score(spss_params)

아래의 결과가 오기 때문에, 좋은 것 같은 느낌입니다.
{'fields': ['CLASS',
  'AGE',
  'BP',
  'AL',
  'SC',
  'POT',
  'PCV',
  'Partition',
  '$L-CLASS',
  '$LP-CLASS',
  'prediction',
  'prediction_classes',
  'probability'],
 'values': [[None,
   75,
   70,
   0,
   0.8,
   3.5,
   46,
   '1_Training',
   'ckd',
   0.998594297142512,
   0.998594297142512,
   'ckd',
   0.998594297142512]]}

Python 함수를 AI Function으로 등록



여기까지 주면, AI Function화는 곧 가능합니다.
다음 코드는 AI Function으로 등록합니다.
meta_data = { client.repository.FunctionMetaNames.NAME: 'SPSS Kidney AI Function v8' }
function_details = client.repository.store_function(meta_props=meta_data, function=score_generator)

다음과 같은 결과가 돌아오면 등록에 성공하고 있습니다.
No RUNTIME_UID passed. Creating default runtime... SUCCESS

Successfully created default runtime with uid: aab70361-3c9a-4f53-804d-411e39ee1978

다음 명령으로 등록 결과를 확인해 봅니다.
print(json.dumps(function_details, indent=2))

잘하면 다음과 같이됩니다.
{
  "metadata": {
    "guid": "d936336d-d9a3-444f-beec-f504dbc00f94",
    "created_at": "2018-12-28T12:09:19.244Z",
    "url": "https://us-south.ml.cloud.ibm.com/v4/functions/d936336d-d9a3-444f-beec-f504dbc00f94"
  },
  "entity": {
    "type": "python",
    "runtime": {
      "url": "https://us-south.ml.cloud.ibm.com/v4/runtimes/aab70361-3c9a-4f53-804d-411e39ee1978"
    },
    "instance_id": "f7e16611-81a0-442b-95b4-9dd993ffa0e9",
    "revision_number": "1",
    "name": "SPSS Kidney AI Function v9",
    "region": "us-south",
    "function_revision": {
      "url": "https://us-south.ml.cloud.ibm.com/v4/functions/d936336d-d9a3-444f-beec-f504dbc00f94/revisions/65e0971f-ab64-4183-b022-a2074e8f57b7",
      "content_url": "https://us-south.ml.cloud.ibm.com/v4/functions/d936336d-d9a3-444f-beec-f504dbc00f94/revisions/65e0971f-ab64-4183-b022-a2074e8f57b7/content"
    }
  }
}

등록한 AI Function은 Watson Studio의 관리 화면에서 아래와 같이 Deployment 중 하나로 보입니다.



일반 웹 서비스처럼 scoring-endpoint URL을 가지고 있고 여기를 향해 요청을 던지면 결과가 바뀝니다. 이 동작은 기계 학습 모델에서 직접 만든 웹 서비스와 완전히 동일합니다.

좋은 웹페이지 즐겨찾기