

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# Neptune ML의 Gremlin 노드 분류 쿼리
<a name="machine-learning-gremlin-vertex-classification-queries"></a>

Neptune ML의 Gremlin 노드 분류의 경우:
+ 모델은 버텍스의 한 속성에 대해 훈련됩니다. 이 속성의 고유한 값 세트를 노드 클래스 세트 또는 간단히 클래스라고 합니다.
+ 버텍스 속성의 노드 클래스 또는 범주형 속성 값은 노드 분류 모델에서 유추할 수 있습니다. 이는 이 속성이 아직 버텍스에 연결되지 않은 경우에 유용합니다.
+ 노드 분류 모델에서 하나 이상의 클래스를 가져오려면 조건자 `Neptune#ml.classification`과 함께 `with()` 단계를 사용하여 `properties()` 단계를 구성해야 합니다. 출력 형식은 버텍스 속성일 때 예상할 수 있는 것과 비슷합니다.

**참고**  
노드 분류는 문자열 속성 값에서만 작동합니다. 즉, 문자열에 해당하는 항목인 `"0"` 및 `"1"`은 지원되지만, `0` 또는 `1`과 같은 숫자 속성 값은 지원되지 않습니다. 마찬가지로, 부울 속성 값 `true` 및 `false`도 작동하지 않지만, `"true"` 및 `"false"`는 작동합니다.

다음은 샘플 노드 분류 쿼리입니다.

```
g.with( "Neptune#ml.endpoint","node-classification-movie-lens-endpoint" )
 .with( "Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role" )
 .with( "Neptune#ml.limit", 2 )
 .with( "Neptune#ml.threshold", 0.5D )
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre").with("Neptune#ml.classification")
```

이 쿼리의 출력은 다음과 같습니다.

```
==>vp[genre->Action]
==>vp[genre->Crime]
==>vp[genre->Comedy]
```

위 쿼리에서는 `V()` 및 `properties()` 단계가 다음과 같이 사용됩니다.

이 `V()` 단계에는 노드 분류 모델에서 클래스를 가져오려는 버텍스 세트가 포함됩니다.

```
 .V( "movie_1", "movie_2", "movie_3" )
```

`properties()` 단계에는 모델이 훈련된 키가 포함되어 있으며, 노드 분류 ML 추론 쿼리임을 나타내는 `.with("Neptune#ml.classification")`가 있습니다.

여러 속성 키는 현재 `properties().with("Neptune#ml.classification")` 단계에서 지원되지 않습니다. 예를 들어, 다음과 같은 쿼리로 인해 예외가 발생합니다.

```
g.with("Neptune#ml.endpoint", "node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre", "other_label").with("Neptune#ml.classification")
```

구체적인 오류 메시지는 [Neptune ML 예외 목록](machine-learning-gremlin-exceptions.md)을 참조하세요.

`properties().with("Neptune#ml.classification")` 단계는 다음 단계 중 하나와 함께 사용할 수 있습니다.
+ `value()`
+ `value().is()`
+ `hasValue()`
+ `has(value,"")`
+ `key()`
+ `key().is()`
+ `hasKey()`
+ `has(key,"")`
+ `path()`

## 기타 노드 분류 쿼리
<a name="machine-learning-gremlin-node-class-other-queries"></a>

추론 엔드포인트와 해당 IAM 역할을 모두 DB 클러스터 파라미터 그룹에 저장한 경우 노드 분류 쿼리는 다음과 같이 간단하게 나타날 수 있습니다.

```
g.V("movie_1", "movie_2", "movie_3").properties("genre").with("Neptune#ml.classification")
```

다음 `union()` 단계를 사용하여 쿼리에 버텍스 속성과 클래스를 혼합할 수 있습니다.

```
g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .union(
   properties("genre").with("Neptune#ml.classification"),
   properties("genre")
 )
```

다음과 같이 무한 쿼리를 만들 수도 있습니다.

```
g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V()
 .properties("genre").with("Neptune#ml.classification")
```

다음 `as()` 단계와 함께 `select()` 단계를 사용하여 버텍스와 노드 클래스를 검색할 수 있습니다.

```
g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" ).as("vertex")
 .properties("genre").with("Neptune#ml.classification").as("properties")
 .select("vertex","properties")
```

다음 예제에 나와 있는 것처럼 노드 클래스를 기준으로 필터링할 수도 있습니다.

```
g.with("Neptune#ml.endpoint", "node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre").with("Neptune#ml.classification")
 .has(value, "Horror")

g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre").with("Neptune#ml.classification")
 .has(value, P.eq("Action"))

g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre").with("Neptune#ml.classification")
 .has(value, P.within("Action", "Horror"))
```

`Neptune#ml.score` 조건자를 사용하여 노드 분류 신뢰도 점수를 얻을 수 있습니다.

```
 g.with("Neptune#ml.endpoint","node-classification-movie-lens-endpoint")
 .with("Neptune#ml.iamRoleArn","arn:aws:iam::0123456789:role/sagemaker-role")
 .V( "movie_1", "movie_2", "movie_3" )
 .properties("genre", "Neptune#ml.score").with("Neptune#ml.classification")
```

응답은 다음과 같습니다.

```
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.01234567]
==>vp[genre->Crime]
==>vp[Neptune#ml.score->0.543210]
==>vp[genre->Comedy]
==>vp[Neptune#ml.score->0.10101]
```

## 노드 분류 쿼리에서 유도 추론 사용
<a name="machine-learning-gremlin-node-class-inductive"></a>

Jupyter Notebook의 기존 그래프에 다음과 같이 새 노드를 추가한다고 가정해 보겠습니다.

```
%%gremlin
g.addV('label1').property(id,'101').as('newV')
 .V('1').as('oldV1')
 .V('2').as('oldV2')
 .addE('eLabel1').from('newV').to('oldV1')
 .addE('eLabel2').from('oldV2').to('newV')
```

그런 다음 유도 추론 쿼리를 사용하여 새 노드를 반영하는 장르 및 신뢰도 점수를 얻을 수 있습니다.

```
%%gremlin
g.with("Neptune#ml.endpoint", "nc-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('101').properties("genre", "Neptune#ml.score")
 .with("Neptune#ml.classification")
 .with("Neptune#ml.inductiveInference")
```

하지만 쿼리를 여러 번 실행하면 결과가 약간 다를 수 있습니다.

```
# First time
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.12345678]

# Second time
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.21365921]
```

동일한 쿼리를 결정적으로 만들 수 있습니다.

```
%%gremlin
g.with("Neptune#ml.endpoint", "nc-ep")
 .with("Neptune#ml.iamRoleArn", "arn:aws:iam::123456789012:role/NeptuneMLRole")
 .V('101').properties("genre", "Neptune#ml.score")
 .with("Neptune#ml.classification")
 .with("Neptune#ml.inductiveInference")
 .with("Neptune#ml.deterministic")
```

그러면 결과는 매번 거의 동일하게 나타납니다.

```
# First time
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.12345678]
# Second time
==>vp[genre->Action]
==>vp[Neptune#ml.score->0.12345678]
```