---
title: 运行和验证
description: "编写推理脚本用以验证模型是否可以正常推理。"
url: https://www.hikunpeng.com/document/detail/zh/SRA/ecosystemEnable/dlrm/kunpengdlrm_02_0012.html
sourcePath: /source/zh/SRA/ecosystemEnable/dlrm/kunpengdlrm_02_0012.html
indexId: 51220f4725ebba0d536b9bfe3bb610aa24b0509f25209d51ae9016174c32447c60
---
# 运行和验证

编写推理脚本用以验证模型是否可以正常推理。

1. 创建并编写测试脚本dlrm_test.py。

  a. 创建“dlrm_test.py”文件。
    1 vi dlrm_test.py

  b. 按“i”进入编辑模式，编写“dlrm_test.py”文件，添加如下部分。

```
import logging
import numpy as np
import tensorflow as tf
from tensorflow.data import Dataset
from noddlrm.recommenders import DLRM
def load_criteo(dataset_folder='dataset/'):
with np.load(dataset_folder + 'criteo/kaggle_processed.npz') as data:
X_int = data["X_int"]
X_cat = data["X_cat"]
y = data["y"]
counts = data["counts"]
indices = np.arange(len(y))
indices = np.array_split(indices, 7)
test_indices = indices[-1]
val_indices, test_indices = np.array_split(test_indices, 2)
raw_data = dict()
raw_data['counts'] = counts
raw_data['X_cat_test'] = X_cat[test_indices]
raw_data['X_int_test'] = np.log(X_int[test_indices]+1).astype(np.float32)
raw_data['y_test'] = y[test_indices]
return raw_data
@tf.function
def eval_step(dense_features, sparse_features, label):
pred = dlrm_model.inference(dense_features, sparse_features)
auc.update_state(y_true=label, y_pred=pred)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format='%(message)s')
batch_size = 1024
raw_data = load_criteo('../../dataset/')
test_dataset = Dataset.from_tensor_slices({
'dense_features': raw_data['X_int_test'][:],
'sparse_features': raw_data['X_cat_test'][:],
'label': raw_data['y_test'][:]
}).batch(batch_size).prefetch(1)
dlrm_model = DLRM(
m_spa=4,
ln_emb=raw_data['counts'],
ln_bot=[8, 4],
ln_top=[128, 64, 1]
)
dlrm_model.load_weights('mymodel')
auc = tf.keras.metrics.AUC()
for _, batch_data in enumerate(test_dataset):
eval_step(**batch_data)
logging.info('auc: %s', auc.result().numpy())
```


  c. 按“Esc”键，输入:wq!，按“Enter”保存并退出编辑。
2. 运行测试。
  1 python dlrm_test.py

  若测试程序运行无报错，回显信息显示auc值，则表示DLRM正常推理。auc值与上文训练DLRM模型小节所训练的模型效果有关，如果训练的auc与本文不同，则不必与图片保持一致。但在没更换模型的情况下，按上文提供的验证程序运行，多次推理结果应保持一致。
