Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
refactor(score_fn): rename score functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhxiao committed Sep 4, 2019
1 parent e9feaa6 commit d2d578c
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 37 deletions.
21 changes: 21 additions & 0 deletions gnes/component.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# Tencent is pleased to support the open source community by making GNES available.
#
# Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .encoder import base as encoder_base
from .indexer import base as indexer_base
from .preprocessor import base as prep_base
from .router import base as router_base
from .score_fn import base as score_base

# Encoder
BaseEncoder = encoder_base.BaseEncoder
Expand Down Expand Up @@ -35,3 +51,8 @@
BaseTopkReduceRouter = router_base.BaseTopkReduceRouter
BaseMapRouter = router_base.BaseMapRouter
PipelineRouter = router_base.PipelineRouter

# Score_Fn
BaseScoreFn = score_base.BaseScoreFn
ModifierScoreFn = score_base.ModifierScoreFn
CombinedScoreFn = score_base.CombinedScoreFn
15 changes: 15 additions & 0 deletions gnes/composer/http.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Tencent is pleased to support the open source community by making GNES available.
#
# Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import parse_qs

Expand Down
15 changes: 15 additions & 0 deletions gnes/encoder/numeric/pooling.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Tencent is pleased to support the open source community by making GNES available.
#
# Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from typing import Tuple

Expand Down
6 changes: 3 additions & 3 deletions gnes/indexer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

from ..base import TrainableBase, CompositionalTrainableBase
from ..proto import gnes_pb2, blob2array
from ..score_fn.base import get_unary_score, ModifierFn
from ..score_fn.base import get_unary_score, ModifierScoreFn


class BaseIndexer(TrainableBase):
def __init__(self,
normalize_fn: 'BaseScoreFn' = ModifierFn(),
score_fn: 'BaseScoreFn' = ModifierFn(), *args, **kwargs):
normalize_fn: 'BaseScoreFn' = ModifierScoreFn(),
score_fn: 'BaseScoreFn' = ModifierScoreFn(), *args, **kwargs):
super().__init__(*args, **kwargs)
self.normalize_fn = normalize_fn
self.score_fn = score_fn
Expand Down
15 changes: 15 additions & 0 deletions gnes/indexer/doc/dict.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Tencent is pleased to support the open source community by making GNES available.
#
# Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List

from google.protobuf.json_format import MessageToJson, Parse
Expand Down
4 changes: 2 additions & 2 deletions gnes/router/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from collections import defaultdict
from typing import List, Generator

from gnes.score_fn.base import ScoreCombinedFn
from gnes.score_fn.base import CombinedScoreFn
from ..base import TrainableBase, CompositionalTrainableBase
from ..proto import gnes_pb2, merge_routes

Expand Down Expand Up @@ -65,7 +65,7 @@ def __init__(self, reduce_op: str = 'sum', descending: bool = True, *args, **kwa
self.descending = descending

def post_init(self):
self.reduce_op = ScoreCombinedFn(score_mode=self._reduce_op)
self.reduce_op = CombinedScoreFn(score_mode=self._reduce_op)

def get_key(self, x: 'gnes_pb2.Response.QueryResponse.ScoredResult') -> str:
raise NotImplementedError
Expand Down
38 changes: 19 additions & 19 deletions gnes/score_fn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def new_score(self, *, operands: Sequence['gnes_pb2.Response.QueryResponse.Score
**kwargs)


class ScoreCombinedFn(BaseScoreFn):
class CombinedScoreFn(BaseScoreFn):
"""Combine multiple scores into one score, defaults to 'multiply'"""

def __init__(self, score_mode: str = 'multiply', *args, **kwargs):
Expand Down Expand Up @@ -83,7 +83,7 @@ def __call__(self, *last_scores) -> 'gnes_pb2.Response.QueryResponse.ScoredResul
score_mode=self.score_mode)


class ModifierFn(BaseScoreFn):
class ModifierScoreFn(BaseScoreFn):
"""Modifier to apply to the value
score = modifier(factor * value)
"""
Expand Down Expand Up @@ -136,20 +136,20 @@ def __call__(self,


class ScoreOps:
multiply = ScoreCombinedFn('multiply')
sum = ScoreCombinedFn('sum')
max = ScoreCombinedFn('max')
min = ScoreCombinedFn('min')
avg = ScoreCombinedFn('avg')
none = ModifierFn('none')
log = ModifierFn('log')
log1p = ModifierFn('log1p')
log2p = ModifierFn('log2p')
ln = ModifierFn('ln')
ln1p = ModifierFn('ln1p')
ln2p = ModifierFn('ln2p')
square = ModifierFn('square')
sqrt = ModifierFn('sqrt')
abs = ModifierFn('abs')
reciprocal = ModifierFn('reciprocal')
reciprocal1p = ModifierFn('reciprocal1p')
multiply = CombinedScoreFn('multiply')
sum = CombinedScoreFn('sum')
max = CombinedScoreFn('max')
min = CombinedScoreFn('min')
avg = CombinedScoreFn('avg')
none = ModifierScoreFn('none')
log = ModifierScoreFn('log')
log1p = ModifierScoreFn('log1p')
log2p = ModifierScoreFn('log2p')
ln = ModifierScoreFn('ln')
ln1p = ModifierScoreFn('ln1p')
ln2p = ModifierScoreFn('ln2p')
square = ModifierScoreFn('square')
sqrt = ModifierScoreFn('sqrt')
abs = ModifierScoreFn('abs')
reciprocal = ModifierScoreFn('reciprocal')
reciprocal1p = ModifierScoreFn('reciprocal1p')
4 changes: 2 additions & 2 deletions gnes/score_fn/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .base import get_unary_score, ScoreCombinedFn
from .base import get_unary_score, CombinedScoreFn


class WeightedChunkScoreFn(ScoreCombinedFn):
class WeightedChunkScoreFn(CombinedScoreFn):
"""score = d_chunk.weight * relevance * q_chunk.weight"""

def __call__(self, last_score: 'gnes_pb2.Response.QueryResponse.ScoredResult.Score',
Expand Down
4 changes: 2 additions & 2 deletions gnes/score_fn/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .base import get_unary_score, ScoreCombinedFn
from .base import get_unary_score, CombinedScoreFn


class WeightedDocScoreFn(ScoreCombinedFn):
class WeightedDocScoreFn(CombinedScoreFn):
def __call__(self, last_score: 'gnes_pb2.Response.QueryResponse.ScoredResult.Score',
doc: 'gnes_pb2.Document', *args, **kwargs):
d_weight = get_unary_score(value=doc.weight,
Expand Down
10 changes: 5 additions & 5 deletions gnes/score_fn/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .base import ModifierFn, ScoreOps as so
from .base import ModifierScoreFn, ScoreOps as so


class Normalizer1(ModifierFn):
class Normalizer1(ModifierScoreFn):
"""Do normalizing: score = 1 / (1 + sqrt(score))"""

def __init__(self):
Expand All @@ -26,7 +26,7 @@ def __call__(self, last_score, *args, **kwargs):
return super().__call__(so.sqrt(last_score))


class Normalizer2(ModifierFn):
class Normalizer2(ModifierScoreFn):
"""Do normalizing: score = 1 / (1 + score / num_dim)"""

def __init__(self, num_dim: int):
Expand All @@ -40,14 +40,14 @@ def __call__(self, last_score, *args, **kwargs):
return super().__call__(so.sqrt(last_score))


class Normalizer4(ModifierFn):
class Normalizer4(ModifierScoreFn):
"""Do normalizing: score = 1 - score / num_bytes """

def __init__(self, num_bytes: int):
super().__init__(modifier='invert1p', factor=1.0 / num_bytes, factor_name='1/num_bytes')


class Normalizer5(ModifierFn):
class Normalizer5(ModifierScoreFn):
"""Do normalizing: score = 1 / (1 + sqrt(abs(score)))"""

def __init__(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_score_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pprint import pprint

from gnes.proto import gnes_pb2
from gnes.score_fn.base import get_unary_score, ScoreCombinedFn, ModifierFn
from gnes.score_fn.base import get_unary_score, CombinedScoreFn, ModifierScoreFn
from gnes.score_fn.chunk import WeightedChunkScoreFn
from gnes.score_fn.normalize import Normalizer1, Normalizer2, Normalizer3, Normalizer4

Expand All @@ -18,11 +18,11 @@ def test_basic(self):
def test_op(self):
a = get_unary_score(0.5)
b = get_unary_score(0.7)
sum_op = ScoreCombinedFn(score_mode='sum')
sum_op = CombinedScoreFn(score_mode='sum')
c = sum_op(a, b)
self.assertAlmostEqual(c.value, 1.2)

sq_op = ModifierFn(modifier='square')
sq_op = ModifierScoreFn(modifier='square')
c = sum_op(a, sq_op(b))
self.assertAlmostEqual(c.value, 0.99)
print(c)
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_normalizer(self):
pprint(json.loads(b.explained))
self.assertEqual(b.value, 0.75)

norm_op = ModifierFn('none')
norm_op = ModifierScoreFn('none')
b = norm_op(a)
pprint(json.loads(b.explained))
self.assertEqual(b.value, 0.5)
Expand Down

0 comments on commit d2d578c

Please sign in to comment.