Skip to content

Commit

Permalink
change expit to softmax (PaddlePaddle#5006)
Browse files Browse the repository at this point in the history
  • Loading branch information
w5688414 authored Feb 28, 2023
1 parent 428c21a commit 91b6d44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,32 @@
import os
import sys

from scipy.special import softmax
from scipy.special import expit
import numpy as np
import pandas as pd

import paddle
from paddle import inference
import paddle.nn.functional as F
from paddlenlp.data import Stack, Tuple, Pad
from scipy.special import softmax

from paddlenlp.data import Pad, Tuple
from paddlenlp.datasets import load_dataset
from paddlenlp.utils.log import logger
from paddlenlp.transformers import AutoTokenizer
from paddlenlp.utils.log import logger

sys.path.append(".")

# yapf: disable
parser = argparse.ArgumentParser()
parser.add_argument("--model_dir", type=str, required=True,
help="The directory to static model.")

parser.add_argument("--input_file", type=str, required=True,
help="The test set file.")
parser.add_argument("--max_seq_length", default=128, type=int,
help="The maximum total input sequence length after tokenization. Sequences "
"longer than this will be truncated, sequences shorter will be padded.")
parser.add_argument("--batch_size", default=32, type=int,
help="Batch size per GPU/CPU for training.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu'], default="gpu",
help="Select which device to train model, defaults to gpu.")

parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False],
help='Enable to use tensorrt to speed up.')
parser.add_argument("--precision", default="fp32", type=str, choices=["fp32", "fp16", "int8"],
help='The tensorrt precision.')

parser.add_argument('--cpu_threads', default=10, type=int,
help='Number of threads to predict when using cpu.')
parser.add_argument('--enable_mkldnn', default=False, type=eval, choices=[True, False],
help='Enable to use mkldnn to speed up when using cpu.')

parser.add_argument("--benchmark", type=eval, default=False,
help="To log some information about environment and running.")
parser.add_argument("--save_log_path", type=str, default="./log_output/",
help="The file path to save log.")
parser.add_argument("--model_dir", type=str, required=True, help="The directory to static model.")
parser.add_argument("--input_file", type=str, required=True, help="The test set file.")
parser.add_argument("--max_seq_length", default=128, type=int, help="The maximum total input sequence length after tokenization. Sequences longer than this will be truncated, sequences shorter will be padded.")
parser.add_argument("--batch_size", default=32, type=int, help="Batch size per GPU/CPU for training.")
parser.add_argument('--device', choices=['cpu', 'gpu', 'xpu'], default="gpu", help="Select which device to train model, defaults to gpu.")
parser.add_argument('--use_tensorrt', default=False, type=eval, choices=[True, False], help='Enable to use tensorrt to speed up.')
parser.add_argument("--precision", default="fp32", type=str, choices=["fp32", "fp16", "int8"], help='The tensorrt precision.')
parser.add_argument('--cpu_threads', default=10, type=int, help='Number of threads to predict when using cpu.')
parser.add_argument('--enable_mkldnn', default=False, type=eval, choices=[True, False], help='Enable to use mkldnn to speed up when using cpu.')
parser.add_argument("--benchmark", type=eval, default=False, help="To log some information about environment and running.")
parser.add_argument("--save_log_path", type=str, default="./log_output/", help="The file path to save log.")
parser.add_argument('--model_name_or_path', default="rocketqa-base-cross-encoder", help="The pretrained model used for training")

args = parser.parse_args()
# yapf: enable

Expand Down Expand Up @@ -203,7 +183,7 @@ def predict(self, data, tokenizer):
sim_score = self.output_handle.copy_to_cpu()
if args.benchmark:
self.autolog.times.stamp()
sim_score = expit(sim_score)[:, 1]
sim_score = softmax(sim_score)[:, 1]

if args.benchmark:
self.autolog.times.end(stamp=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import sys
import json

from scipy.special import expit
from paddle_serving_server.web_service import WebService, Op
from paddle_serving_server.web_service import Op, WebService
from scipy.special import softmax


def convert_example(example, tokenizer, max_seq_length=512):
Expand All @@ -38,7 +36,7 @@ def init_op(self):
self.tokenizer = AutoTokenizer.from_pretrained("rocketqa-base-cross-encoder")

def preprocess(self, input_dicts, data_id, log_id):
from paddlenlp.data import Stack, Tuple, Pad
from paddlenlp.data import Pad, Tuple

((_, input_dict),) = input_dicts.items()
print("input dict", input_dict)
Expand All @@ -60,7 +58,7 @@ def preprocess(self, input_dicts, data_id, log_id):

def postprocess(self, input_dicts, fetch_dict, data_id, log_id):
new_dict = {}
sim_score = expit(fetch_dict["predict"])[:, 1]
sim_score = softmax(fetch_dict["predict"])[:, 1]
new_dict["predict"] = str(sim_score)
return new_dict, None, ""

Expand Down

0 comments on commit 91b6d44

Please sign in to comment.