推荐算法再次踩坑记录

news/2024/5/19 22:37:00 标签: docker, tensorflow, serving, GPU, 推荐算法

去年搞通了EasyRec这个玩意,没想到今年还要用推荐方面的东西,行吧,再来一次,再次踩坑试试。

1、EasyRec训练测试数据下载:

git clone后,进入EasyRec,然后执行:bash scripts/init.sh 将所用到的数据全部下载完成✅

2、模型部署,参考博文docker部署tf-serving

首先必须将final文件,将此文件下的全部复制到/models/half_plus_two/下面。

2.1查看模型基本参数:

saved_model_cli show --dir /models/half_plus_two/00000123/ --tag_set serve --signature_def serving_default
The given SavedModel SignatureDef contains the following input(s):
  inputs['x'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1, 1)
      name: x:0
The given SavedModel SignatureDef contains the following output(s):
  outputs['y'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1, 1)
      name: y:0
Method name is: tensorflow/serving/predict

2.2查看模型是否支持GPU

saved_model_cli show --dir /models/half_plus_two/00000123/
The given SavedModel contains the following tag-sets:
serve#表示不支持GPU,支持的应该是serve,gpu

2.3输入数据查看结果

saved_model_cli run --dir /models/half_plus_two/00000123/ --tag_set serve --signature_def serving_default --input_exprs="x=[[1],[9]]"
[[2.5]
 [6.5]]
结果正确,一半+2,1*0.5+2,9*0.5+2,结果一致

2.4curl: (56) Recv failure: Connection reset by peer

发现docker -p指定端口不能用,其中有鬼,之前服务器就可以指定端口 ,因此仍旧改为8501端口。

docker启动代码见此文。

2.5docker停止及删除容器 ,没有此操作无法重启该名字的容器。

注意,里面有模型名字及容器名字,建议都取一样的名字。比如half_plus_two

docker kill half_plus_two
docker rm half_plus_two

3、部署训练好的dssm模型

3.1查看模型输入参数

saved_model_cli show --dir /models/mydssm/163333/ --tag_set serve --signature_def serving_default
The given SavedModel SignatureDef contains the following input(s):
  inputs['app_category'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_9:0
  inputs['app_domain'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_8:0
  inputs['app_id'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_7:0
  inputs['banner_pos'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_3:0
  inputs['c1'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_2:0
  inputs['c14'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_15:0
  inputs['c15'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_16:0
  inputs['c16'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_17:0
  inputs['c17'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_18:0
  inputs['c18'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_19:0
  inputs['c19'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_20:0
  inputs['c20'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_21:0
  inputs['c21'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_22:0
  inputs['device_conn_type'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_14:0
  inputs['device_id'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_10:0
  inputs['device_ip'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_11:0
  inputs['device_model'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_12:0
  inputs['device_type'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_13:0
  inputs['hour'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_1:0
  inputs['site_category'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_6:0
  inputs['site_domain'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_5:0
  inputs['site_id'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_4:0
The given SavedModel SignatureDef contains the following output(s):
  outputs['logits'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1)
      name: Squeeze:0
  outputs['probs'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1)
      name: Sigmoid:0
Method name is: tensorflow/serving/predict

#同样也不支持GPU
saved_model_cli show --dir /models/mydssm/13339343/
The given SavedModel contains the following tag-sets:
serve

3.2测试请求

curl -d '{"instances": [{"app_category":"1","app_domain":"2","app_id":"3","banner_pos":"4","c1":"5","c14":"6","c15":"7","c16":"8","c17":"9","c18":"10","c19":"11","c20":"12","c21":"13","device_conn_type":"14","device_id":"15","device_ip":"16","device_model":"17","device_type":"18","hour":"19","site_category":"20","site_domain":"21","site_id":"22"}]}'     -X POST http://localhost:8501/v1/models/mydssm:predict
{
    "predictions": [
        {
            "logits": -2.64485741,
            "probs": 0.0663066804
        }
    ]
#https://github.com/tensorflow/serving/issues/2104
>>> import json,requests
>>> heads = {
        "content-type": "application/json"
    }
>>> jd={"signature_name": "serving_default","instances":[{"app_category":"1","app_domain":"2","app_id":"3","banner_pos":"4","c1":"5","c14":"6","c15":"7","c16":"8","c17":"9","c18":"10","c19":"11","c20":"12","c21":"13","device_conn_type":"14","device_id":"15","device_ip":"16","device_model":"17","device_type":"18","hour":"19","site_category":"20","site_domain":"21","site_id":"22"}]}
>>> requests.post(url,data=json.dumps(jd),headers=heads).json()
{'predictions': [{'logits': -2.64485741, 'probs': 0.0663066804}]}

4、部署 tf-serving使用GPU,需要docker安装GPU版本

4.1拉取images

docker pull tensorflow/serving:latest-gpu

4.2安装nvidia docker容器工具

CentOS

sudo dnf clean expire-cache \
    && sudo dnf install -y nvidia-container-toolkit-base
nvidia-ctk --version
sudo nvidia-ctk cdi generate --output=/etc/cdi/nvidia.yaml
grep "  name:" /etc/cdi/nvidia.yaml

Ubuntu

 sudo apt-get update \
    && sudo apt-get install -y nvidia-container-toolkit-base
nvidia-ctk --version
sudo nvidia-ctk cdi generate --output=/etc/cdi/nvidia.yaml
grep "  name:" /etc/cdi/nvidia.yaml

4.3启动

docker run --runtime=nvidia -p 8501:8501 \ --mount type=bind,\ source=/tmp/tfserving/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_gpu,\ target=/models/half_plus_two \ -e MODEL_NAME=half_plus_two -t tensorflow/serving:latest-gpu --per_process_gpu_memory_fraction=0.5
sudo docker run --rm --runtime=nvidia --gpus all nvidia/cuda:11.6.2-base-ubuntu20.04 nvidia-smi

综上所述,不足之处请参考,英伟达官方介绍,tf-serving官方github

拜拜


http://www.niftyadmin.cn/n/159752.html

相关文章

【Linux】信号常见概念

文章目录信号入门生活中的信号技术应用角度的信号signal函数注意事项信号的概念信号的产生信号的记录(保存)信号处理常见方式概述信号入门 生活中的信号 你在网上买了很多件商品,在等待不同商品快递的到来 但即便快递还没有到来,你也知道快递到了的时候应该怎么处理快递,也就…

水声功率放大器与宽带匹配技术研究

作为声呐设备重要的一份子,水声信号发射机承担着非常重要的作用。水声信号发射机其实是由水声功率放大器、信号源、匹配网络以及换能器四部分组成。然而随着水声浮标、水声通信设备以及便携式水下探测设备等的广泛应用,对于发射机高频带和高效率的要求也…

Mybatis是如何访问数据的?

核心组件 Executor介绍 使用过 MyBatis 的都应该知道,Sqlsession 的功能都是基于 Executor 来实现的,Executor 是MyBaits 核心接口之一,定义了数据库操作最基本的方法,在其内部遵循 JDBC 规范完成对数据库的访问;Exec…

项目实战15—登录之后我加入的课程调用接口报错

现象1: 一天之内都会出现登录之后我加入的课程调用接口报错的情况,排查发现了域名不全的问题 而为什么会一天之内一直出现呢?首先存入redis的域名是不全的,是错误的,当然从redis中查询出来的也是错误的。下面的红框的代…

杂记-day1-1

以后每天都会发布两篇,记录这半年来学到的东西,内容有错大家及时指正,不想指正可以当我瞎扯,我不对这个结论负责(KOL脸)今天讲点什么好呢,秉承着先易后难的原则,今天先讲讲容易一点的。讲讲点阵吧&#xff…

【2389. 和有限的最长子序列】

来源:力扣(LeetCode) 描述: 给你一个长度为 n 的整数数组 nums ,和一个长度为 m 的整数数组 queries 。 返回一个长度为 m 的数组 answer ,其中 answer[i] 是 nums 中 元素之和小于等于 queries[i] 的 子…

Java中Thread类基本方法的使用

目录 1.线程的创建 2.线程的中断(终止) 3.线程的等待 4.线程的休眠 5.获取当前线程实例 1.线程的创建 start()方法用于创建并执行线程.它与run()方法的区别是: run()方法是一个特殊方法,相当于线程的入口,其中是线程要实现的逻辑,它一般都是通过系统自动调用,而不是我们手…

遗传算法优化神经网络

%%将神经网络作为遗传算法主函数 function errorBpnetfun(x,P,T,hiddennum,P_test,T_test) %%设置bp神经网络的误差函数作为适应度函数 %%设置函数其中的参数,x为,P为输入的训练样本,T为训练样本的输出 %%hiddennum为隐藏层神经元个数 %%p_te…