Skip to content

Commit

Permalink
[Neural Search] change output_emb_size to self.ptm.config.hidden_size (
Browse files Browse the repository at this point in the history
…#5127)

* change output_emb_size to self.ptm.config['hidden_size']

* Update applications/neural_search/recall/milvus/base_model.py

Co-authored-by: Sijun He <sijun.he@hotmail.com>

* Update applications/neural_search/recall/milvus/base_model.py

Co-authored-by: Sijun He <sijun.he@hotmail.com>

* Update applications/neural_search/recall/simcse/model.py

Co-authored-by: Sijun He <sijun.he@hotmail.com>

* change to self.ptm.config.hidden_size

---------

Co-authored-by: Sijun He <sijun.he@hotmail.com>
  • Loading branch information
w5688414 and sijunhe authored Mar 7, 2023
1 parent ee390a6 commit e40e40b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def __init__(self, pretrained_model, dropout=None, output_emb_size=None):
self.output_emb_size = output_emb_size
if output_emb_size > 0:
weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.TruncatedNormal(std=0.02))
self.emb_reduce_linear = paddle.nn.Linear(768, output_emb_size, weight_attr=weight_attr)
self.emb_reduce_linear = paddle.nn.Linear(
self.ptm.config.hidden_size, output_emb_size, weight_attr=weight_attr
)

def get_pooled_embedding(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None):
_, cls_embedding = self.ptm(input_ids, token_type_ids, position_ids, attention_mask)
Expand Down Expand Up @@ -95,7 +97,9 @@ def __init__(self, pretrained_model, dropout=None, output_emb_size=None):
self.output_emb_size = output_emb_size
if output_emb_size > 0:
weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.TruncatedNormal(std=0.02))
self.emb_reduce_linear = paddle.nn.Linear(768, output_emb_size, weight_attr=weight_attr)
self.emb_reduce_linear = paddle.nn.Linear(
self.ptm.config.hidden_size, output_emb_size, weight_attr=weight_attr
)

@paddle.jit.to_static(
input_spec=[
Expand Down
11 changes: 6 additions & 5 deletions applications/neural_search/recall/milvus/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
# limitations under the License.

import abc
import sys

import numpy as np

import paddle
import paddle.nn as nn
Expand All @@ -35,7 +32,9 @@ def __init__(self, pretrained_model, dropout=None, output_emb_size=None):
self.output_emb_size = output_emb_size
if output_emb_size > 0:
weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.TruncatedNormal(std=0.02))
self.emb_reduce_linear = paddle.nn.Linear(768, output_emb_size, weight_attr=weight_attr)
self.emb_reduce_linear = paddle.nn.Linear(
self.ptm.config.hidden_size, output_emb_size, weight_attr=weight_attr
)

@paddle.jit.to_static(
input_spec=[
Expand Down Expand Up @@ -106,7 +105,9 @@ def __init__(self, pretrained_model, dropout=None, output_emb_size=None):
self.output_emb_size = output_emb_size
if output_emb_size > 0:
weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.TruncatedNormal(std=0.02))
self.emb_reduce_linear = paddle.nn.Linear(768, output_emb_size, weight_attr=weight_attr)
self.emb_reduce_linear = paddle.nn.Linear(
self.ptm.config.hidden_size, output_emb_size, weight_attr=weight_attr
)

@paddle.jit.to_static(
input_spec=[
Expand Down
10 changes: 4 additions & 6 deletions applications/neural_search/recall/simcse/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import abc
import sys

import numpy as np

import paddle
import paddle.nn as nn
Expand All @@ -36,7 +32,9 @@ def __init__(self, pretrained_model, dropout=None, margin=0.0, scale=20, output_
self.output_emb_size = output_emb_size
if output_emb_size > 0:
weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.TruncatedNormal(std=0.02))
self.emb_reduce_linear = paddle.nn.Linear(768, output_emb_size, weight_attr=weight_attr)
self.emb_reduce_linear = paddle.nn.Linear(
self.ptm.config.hidden_size, output_emb_size, weight_attr=weight_attr
)

self.margin = margin
# Used scaling cosine similarity to ease converge
Expand All @@ -55,7 +53,7 @@ def get_pooled_embedding(
# Note: cls_embedding is poolerd embedding with act tanh
sequence_output, cls_embedding = self.ptm(input_ids, token_type_ids, position_ids, attention_mask)

if with_pooler == False:
if with_pooler is False:
cls_embedding = sequence_output[:, 0, :]

if self.output_emb_size > 0:
Expand Down

0 comments on commit e40e40b

Please sign in to comment.