Commit feda168b authored by 宋柯's avatar 宋柯

模型上线

parent f00d863e
......@@ -10,74 +10,82 @@ import time
with open('/Users/edz/software/Recommend/train_samples.csv', 'r') as f:
count = 0
examples = []
for line in f:
# print(line)
splits = line.split('|')
features = {
'ITEM_CATEGORY_card_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[0].encode()])),
'USER_CATEGORY_device_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[2].encode()])),
'USER_CATEGORY_os': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[3].encode()])),
'USER_CATEGORY_user_city_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[4].encode()])),
'USER_MULTI_CATEGORY_second_solutions': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[6].split(','))))),
'USER_MULTI_CATEGORY_second_demands': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[7].split(','))))),
'USER_MULTI_CATEGORY_second_positions': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[8].split(','))))),
'USER_MULTI_CATEGORY_projects': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[9].split(','))))),
'ITEM_NUMERIC_click_count_sum': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[10])])),
'ITEM_NUMERIC_click_count_avg': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[11])])),
'ITEM_NUMERIC_click_count_stddev': tf.train.Feature(
float_list=tf.train.FloatList(value=[float(splits[12])])),
'ITEM_NUMERIC_exp_count_sum': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[13])])),
'ITEM_NUMERIC_exp_count_avg': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[14])])),
'ITEM_NUMERIC_exp_count_stddev': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[15])])),
'ITEM_NUMERIC_discount': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[16])])),
'ITEM_NUMERIC_case_count': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[17])])),
'ITEM_NUMERIC_sales_count': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[18])])),
'ITEM_CATEGORY_service_type': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[19].encode()])),
'ITEM_CATEGORY_merchant_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[20].encode()])),
'ITEM_CATEGORY_doctor_type': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[21].encode()])),
'ITEM_CATEGORY_doctor_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[22].encode()])),
'ITEM_CATEGORY_doctor_famous': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[23].encode()])),
'ITEM_CATEGORY_hospital_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[24].encode()])),
'ITEM_CATEGORY_hospital_city_tag_id': tf.train.Feature(
bytes_list=tf.train.BytesList(value=[splits[25].encode()])),
'ITEM_CATEGORY_hospital_type': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[26].encode()])),
'ITEM_CATEGORY_hospital_is_high_quality': tf.train.Feature(
bytes_list=tf.train.BytesList(value=[splits[27].encode()])),
'ITEM_MULTI_CATEGORY_second_demands': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[28].split(','))))),
'ITEM_MULTI_CATEGORY_second_solutions': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[29].split(','))))),
'ITEM_MULTI_CATEGORY_second_positions': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[30].split(','))))),
'ITEM_MULTI_CATEGORY_projects': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[31].split(','))))),
'ITEM_NUMERIC_sku_price': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[32])])),
'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[int(splits[5])])),
}
# print(features)
for _ in range(50):
count = 0
examples = []
for line in f:
# print(splits[32])
tf_features = tf.train.Features(feature=features)
# print(line)
splits = line.split('|')
features = {
'ITEM_CATEGORY_card_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[0].encode()])),
'USER_CATEGORY_device_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[2].encode()])),
'USER_CATEGORY_os': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[3].encode()])),
'USER_CATEGORY_user_city_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[4].encode()])),
'USER_MULTI_CATEGORY_second_solutions': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[6].split(','))))),
'USER_MULTI_CATEGORY_second_demands': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[7].split(','))))),
'USER_MULTI_CATEGORY_second_positions': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[8].split(','))))),
'USER_MULTI_CATEGORY_projects': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[9].split(','))))),
'ITEM_NUMERIC_click_count_sum': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[10])])),
'ITEM_NUMERIC_click_count_avg': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[11])])),
'ITEM_NUMERIC_click_count_stddev': tf.train.Feature(
float_list=tf.train.FloatList(value=[float(splits[12])])),
'ITEM_NUMERIC_exp_count_sum': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[13])])),
'ITEM_NUMERIC_exp_count_avg': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[14])])),
'ITEM_NUMERIC_exp_count_stddev': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[15])])),
'ITEM_NUMERIC_discount': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[16])])),
'ITEM_NUMERIC_case_count': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[17])])),
'ITEM_NUMERIC_sales_count': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[18])])),
'ITEM_CATEGORY_service_type': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[19].encode()])),
'ITEM_CATEGORY_merchant_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[20].encode()])),
'ITEM_CATEGORY_doctor_type': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[21].encode()])),
'ITEM_CATEGORY_doctor_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[22].encode()])),
'ITEM_CATEGORY_doctor_famous': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[23].encode()])),
'ITEM_CATEGORY_hospital_id': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[24].encode()])),
'ITEM_CATEGORY_hospital_city_tag_id': tf.train.Feature(
bytes_list=tf.train.BytesList(value=[splits[25].encode()])),
'ITEM_CATEGORY_hospital_type': tf.train.Feature(bytes_list=tf.train.BytesList(value=[splits[26].encode()])),
'ITEM_CATEGORY_hospital_is_high_quality': tf.train.Feature(
bytes_list=tf.train.BytesList(value=[splits[27].encode()])),
'ITEM_MULTI_CATEGORY_second_demands': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[28].split(','))))),
'ITEM_MULTI_CATEGORY_second_solutions': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[29].split(','))))),
'ITEM_MULTI_CATEGORY_second_positions': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[30].split(','))))),
'ITEM_MULTI_CATEGORY_projects': tf.train.Feature(
bytes_list=tf.train.BytesList(value=list(map(lambda s: s.encode(), splits[31].split(','))))),
'ITEM_NUMERIC_sku_price': tf.train.Feature(float_list=tf.train.FloatList(value=[float(splits[32])])),
'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[int(splits[5])])),
}
# print(features)
tf_example = tf.train.Example(features=tf_features)
# print(splits[32])
tf_features = tf.train.Features(feature=features)
tf_serialized = tf_example.SerializeToString()
tf_example = tf.train.Example(features=tf_features)
examples.append({'b64': base64.b64encode(tf_serialized)})
tf_serialized = tf_example.SerializeToString()
count += 1
if count == 1000:
break
start = time.time()
res = requests.post("http://localhost:8501/v1/models/wide_deep:predict",
json={"inputs": {"examples": examples},
"signature_name": "predict"})
print(res.text)
print(time.time() - start)
examples.append({'b64': base64.b64encode(tf_serialized)})
count += 1
if count == 500:
break
start = time.time()
# res = requests.post("http://localhost:8501/v1/models/wide_deep:predict",
# json={"inputs": {"examples": examples},
# "signature_name": "predict"})
# res = requests.post("http://tensorserving-sk.paas-develop.env/v1/models/service:predict",
# json={"inputs": {"inputs": examples},
# "signature_name": "regression"})
res = requests.post("http://localhost:8000/v1/models/service:predict",
json={"inputs": {"inputs": examples},
"signature_name": "regression"})
# print(res.text)
print(time.time() - start)
......@@ -17,7 +17,8 @@ from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc
import grpc
tf.app.flags.DEFINE_string('server', 'localhost:8502', 'PredictionService host:port')
# tf.app.flags.DEFINE_string('server', 'localhost:8502', 'PredictionService host:port')
tf.app.flags.DEFINE_string('server', 'tensorserving-sk.paas-develop.env:8090', 'PredictionService host:port')
FLAGS = tf.app.flags.FLAGS
def prediction():
......@@ -25,9 +26,9 @@ def prediction():
channel = grpc.insecure_channel(FLAGS.server, options = options)
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = 'wide_deep' #对应上图第一个方框
request.model_spec.name = 'service' #对应上图第一个方框
request.model_spec.signature_name = 'regression' #对应上图第二个方框with open('/Users/edz/software/Recommend/train_samples.csv', 'r') as f:
for _ in range(20):
for _ in range(1):
with open('/Users/edz/software/Recommend/train_samples.csv', 'r') as f:
count = 0
......@@ -92,7 +93,7 @@ def prediction():
examples.append(tf_serialized)
count += 1
if count == 1000:
if count == 1:
break
start = time.time()
# request.inputs['examples'].CopyFrom(tf.make_tensor_proto(examples)) # in对应上图第三个方框,为模型的输入Name
......
......@@ -238,7 +238,7 @@ early_stopping = tf.estimator.experimental.stop_if_no_increase_hook(wideAndDeepM
hooks = [early_stopping]
train_spec = tf.estimator.TrainSpec(input_fn = lambda: input_fn(DATA_DIR + 'train_samples.csv', 100, True, 2048), hooks = hooks)
train_spec = tf.estimator.TrainSpec(input_fn = lambda: input_fn(DATA_DIR + 'train_samples.csv', 1, True, 2048), hooks = hooks)
serving_feature_spec = tf.feature_column.make_parse_example_spec(
linear_feature_columns + dnn_feature_columns)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment