NCF代码运行

keras 2.1.4
tensorflow 1.12.0
python 3.6.4
numpy 1.14.5

一、准备工作

1、安装虚拟环境

conda create -n tensorflow python=3.6.4

在这里插入图片描述

conda activate tensorflow
conda install tensorflow=1.12.0

在这里插入图片描述

conda install keras=2.1.4

在这里插入图片描述

conda info

在这里插入图片描述

2、安装相应依赖

cd Py3_Recommender-Systems-Samples-master/RecSysAndDeepLearning/DNN/ncf

运行指令

python GMF.py --dataset ml-1m --epochs 20 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1

报错:
python GMF.py --dataset ml-1m --epochs 20 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1
WARNING (theano.configdefaults): g++ not available, if using conda: conda install m2w64-toolchain
C:\Users\fff36\AppData\Roaming\Python\Python310\site-packages\theano\configdefaults.py:560: UserWarning: DeprecationWarning: there is no c++ compiler.This is deprecated and with Theano 0.11 a c++ compiler will be mandatory
warnings.warn(“DeprecationWarning: there is no c++ compiler.”
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
Traceback (most recent call last):
File “D:\学习路线\NCF\Py3_Recommender-Systems-Samples-master\RecSysAndDeepLearning\DNN\ncf\GMF.py”, line 10, in
from keras.layers.core import Dense, Lambda, Activation
ModuleNotFoundError: No module named ‘keras.layers.core’

根据您提供的错误信息,有几个问题需要解决:

  1. Theano编译器警告
    Theano在尝试使用C++编译器时遇到了问题。这通常是因为系统中没有安装必要的编译工具链。如果您使用的是Windows系统,可以通过安装m2w64-toolchain来解决这个问题,如警告信息中所建议的。

  2. Theano性能警告
    由于缺少g++,Theano无法执行优化的C实现,将默认使用Python实现,这会严重影响性能。解决第一个问题后,这个警告应该也会消失。

  3. Keras模块错误
    您遇到了ModuleNotFoundError,表明Python无法找到名为keras.layers.core的模块。这可能是因为Keras库没有正确安装,或者您的环境中存在多个Python版本,导致依赖项安装在了错误的Python版本上。

conda install m2w64-toolchain

验证

g++ --version

在这里插入图片描述

conda install theano

搞半天原来是忘记进去tensorflow虚拟环境了

在这里插入图片描述
输入指令

python GMF.py --dataset ml-1m --epochs 20 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1

在这里插入图片描述

报错:
(tensorflow) D:\学习路线\NCF\Py3_Recommender-Systems-Samples-master\RecSysAndDeepLearning\DNN\ncf>python GMF.py --dataset ml-1m --epochs 20 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1
C:\Users\fff36.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:523: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint8 = np.dtype([(“qint8”, np.int8, 1)])
C:\Users\fff36.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:524: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_quint8 = np.dtype([(“quint8”, np.uint8, 1)])
C:\Users\fff36.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint16 = np.dtype([(“qint16”, np.int16, 1)])
C:\Users\fff36.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_quint16 = np.dtype([(“quint16”, np.uint16, 1)])
C:\Users\fff36.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:527: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint32 = np.dtype([(“qint32”, np.int32, 1)])
C:\Users\fff36.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:532: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
np_resource = np.dtype([(“resource”, np.ubyte, 1)])
Traceback (most recent call last):
File “GMF.py”, line 6, in
from keras import backend as K
ModuleNotFoundError: No module named ‘keras’

错误信息显示您遇到了两个主要问题:

  1. FutureWarning
    这些警告来自TensorFlow,指出在创建NumPy数据类型时使用了已弃用的方法。这些警告通常不会影响当前代码的运行,但它们提示未来的NumPy版本中将不再支持这种用法。对于TensorFlow来说,这可能意味着在未来的版本中需要更新代码以适应NumPy的新API。

  2. ModuleNotFoundError
    这个错误表明Python无法在您的环境中找到名为keras的模块。这可能是因为Keras没有安装,或者安装在了错误的Python环境中。

要解决这些问题,请尝试以下步骤:

  • 更新NumPy
    如果您使用的是较旧版本的NumPy,尝试更新到最新版本。您可以使用以下命令来更新NumPy:

    pip install --upgrade numpy
    

    或者,如果您使用conda:

    conda update numpy
    
  • 安装或更新Keras
    确保Keras已经安装在您当前激活的Python环境中。您可以使用以下命令来安装或更新Keras:

    pip install --upgrade keras
    

    或者,如果您使用conda:

    conda install keras
    

    请确保您在tensorflow环境中运行这些命令,以便在正确的环境中安装Keras。


在pycharm终端安装keras

pip install keras==2.1.4

在这里插入图片描述
验证安装

 python -c "import keras; print(keras.__version__)"
 
# 2.1.4
python GMF.py --dataset ml-1m --epochs 20 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1

3、运行GMF.py文件

# 运行成功

(tensorflow) D:\学习路线\NCF\Py3_Recommender-Systems-Samples-master\RecSysAndDeepLearning\DNN\ncf>python GMF.py --dataset ml-1m --epochs 20 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1 
C:\Users\fff36\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Users\fff36\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Users\fff36\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Users\fff36\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Users\fff36\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
Using TensorFlow backend.
regs [0, 0]
GMF arguments: Namespace(batch_size=256, dataset='ml-1m', epochs=20, learner='adam', lr=0.001, num_factors=8, num_neg=4, out=1, path='Data/', regs='[0,0]', verbose=1)     
Load data done [9.5 s]. #user=6040, #item=3706, #train=994169, #test=6040
2024-04-10 15:05:18.117373: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Init: HR = 0.1031, NDCG = 0.0473         [3.4 s]
Iteration 0 [28.0 s]: HR = 0.5439, NDCG = 0.3064, loss = 0.3491 [2.8 s]
Iteration 1 [26.8 s]: HR = 0.5772, NDCG = 0.3261, loss = 0.2996 [2.8 s]

从您提供的输出信息来看,您正在运行一个基于神经协同过滤(Neural Collaborative Filtering, NCF)的推荐系统模型,具体是GMF(Generalized Matrix Factorization)模型。这个模型是一种深度学习方法,用于预测用户对物品的评分或偏好,从而进行个性化推荐。

首先,您在运行脚本时遇到了一些FutureWarning,这些警告是由于在numpy的数据类型定义中使用了已弃用的方式。这通常不会影响当前的运行,但是建议更新您的numpy库到最新版本,以避免将来可能的兼容性问题。

接下来,您的模型使用了以下参数:

  • dataset: ‘ml-1m’,表示使用的是MovieLens 1M数据集,这是一个公开的电影评分数据集。
  • epochs: 20,表示模型将在整个训练集上训练20次。
  • batch_size: 256,表示每次迭代中,模型将处理256个样本。
  • num_factors: 8,表示嵌入向量的维度,这是模型中用于捕捉用户和物品特征的参数数量。
  • regs: ‘[0,0]’, 表示没有使用正则化项,这可能有助于防止过拟合。
  • num_neg: 4,表示对于每个正样本,模型将生成4个负样本进行训练。
  • lr: 0.001,表示学习率,这是优化算法中的一个重要参数,用于控制权重更新的步长。
  • learner: ‘adam’,表示使用的优化器是Adam,这是一种常用的自适应学习率优化算法。
  • verbose: 1,表示训练过程中将输出详细信息。

模型的输出显示了训练过程中的一些关键指标:

  • HR(Hit Rate):准确率,衡量推荐系统准确性的一个指标。
  • NDCG(Normalized Discounted Cumulative Gain):归一化折扣累积增益,衡量推荐系统质量的另一个指标。
  • loss:损失函数的值,表示模型预测值与实际值之间的差异。

在第一次迭代后,模型的性能有所提升,HR从0.1031提升到0.5439,NDCG从0.0473提升到0.3064,损失从0.3491降低到0.2996。这表明模型正在学习并逐渐改进其预测能力。

然而,您的CPU并不支持TensorFlow二进制文件使用的某些指令集(AVX和AVX2),这可能会影响模型的训练速度。如果可能的话,考虑升级到支持这些指令集的CPU,或者在支持这些指令集的环境中运行您的代码。

最后,请注意,虽然模型性能有所提升,但是这些指标只是初步的评估。为了全面评估模型的性能,您可能需要进行更多的实验,包括交叉验证和长期的性能跟踪。此外,您可能还需要考虑其他评价指标,如精确率、召回率、F1分数等,以及用户的多样性和新颖性等其他因素。

Iteration 4 [30.8 s]: HR = 0.6205, NDCG = 0.3553, loss = 0.2748 [3.1 s]
Iteration 5 [28.9 s]: HR = 0.6255, NDCG = 0.3627, loss = 0.2731 [2.9 s]
Iteration 6 [29.5 s]: HR = 0.6253, NDCG = 0.3609, loss = 0.2722 [3.2 s]
Iteration 7 [29.4 s]: HR = 0.6290, NDCG = 0.3606, loss = 0.2711 [3.0 s]
Iteration 8 [34.3 s]: HR = 0.6298, NDCG = 0.3609, loss = 0.2707 [3.0 s]
Iteration 9 [32.2 s]: HR = 0.6310, NDCG = 0.3605, loss = 0.2704 [3.0 s]
Iteration 10 [33.3 s]: HR = 0.6262, NDCG = 0.3615, loss = 0.2698 [3.1 s]
Iteration 11 [32.3 s]: HR = 0.6301, NDCG = 0.3638, loss = 0.2695 [3.0 s]
Iteration 12 [30.9 s]: HR = 0.6285, NDCG = 0.3635, loss = 0.2693 [3.0 s]
Iteration 13 [32.9 s]: HR = 0.6293, NDCG = 0.3628, loss = 0.2689 [3.2 s]
Iteration 14 [30.7 s]: HR = 0.6318, NDCG = 0.3641, loss = 0.2689 [3.1 s]
Iteration 15 [32.2 s]: HR = 0.6326, NDCG = 0.3637, loss = 0.2684 [3.0 s]
Iteration 16 [32.6 s]: HR = 0.6336, NDCG = 0.3645, loss = 0.2680 [3.2 s]
Iteration 17 [31.1 s]: HR = 0.6318, NDCG = 0.3640, loss = 0.2682 [4.0 s]
Iteration 18 [40.0 s]: HR = 0.6288, NDCG = 0.3628, loss = 0.2679 [3.2 s]
Iteration 19 [37.0 s]: HR = 0.6315, NDCG = 0.3650, loss = 0.2679 [3.1 s]
End. Best Iteration 16:  HR = 0.6336, NDCG = 0.3645.
The best GMF model is saved to Pretrain/ml-1m_GMF_8_1712732708.h5

numpy版本有问题

从您提供的输出信息来看,您正在运行一个基于神经协同过滤(Neural Collaborative Filtering, NCF)的推荐系统模型,具体是GMF(Generalized Matrix Factorization)模型。这个模型是一种深度学习方法,用于预测用户对物品的评分或偏好,从而进行个性化推荐。
首先,您在运行脚本时遇到了一些FutureWarning,这些警告是由于在numpy的数据类型定义中使用了已弃用的方式。这通常不会影响当前的运行,但是建议更新您的numpy库到最新版本,以避免将来可能的兼容性问题。

python -c "import numpy; print(numpy.__version__)"
1.19.2
pip install --upgrade numpy

在这里插入图片描述
还是不行


pip uninstall numpy
pip install numpy==1.14.0

运行文件

python GMF.py --dataset ml-1m --epochs 5 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1
(tensorflow) D:\学习路线\NCF\Py3_Recommender-Systems-Samples-master\RecSysAndDeepLearning\DNN\ncf>python GMF.py --dataset ml-1m --epochs 5 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1
C:\Users\fff36\.conda\envs\tensorflow\lib\site-packages\scipy\__init__.py:143: UserWarning: NumPy 1.14.5 or above is required for this version of SciPy (detected version 1.14.0)
  UserWarning)
Using TensorFlow backend.
regs [0, 0]
GMF arguments: Namespace(batch_size=256, dataset='ml-1m', epochs=5, learner='adam', lr=0.001, num_factors=8, num_neg=4, out=1, path='Data/', regs='[0,0]', verbose=1)      
Load data done [9.6 s]. #user=6040, #item=3706, #train=994169, #test=6040
2024-04-10 15:24:04.050450: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Init: HR = 0.0969, NDCG = 0.0438         [2.8 s]

Iteration 1 [27.0 s]: HR = 0.5806, NDCG = 0.3264, loss = 0.3016 [2.8 s]
Iteration 2 [26.0 s]: HR = 0.6101, NDCG = 0.3464, loss = 0.2855 [2.9 s]
Iteration 3 [25.5 s]: HR = 0.6215, NDCG = 0.3595, loss = 0.2778 [2.7 s]
Iteration 4 [25.7 s]: HR = 0.6207, NDCG = 0.3613, loss = 0.2748 [2.9 s]
End. Best Iteration 3:  HR = 0.6215, NDCG = 0.3595.
The best GMF model is saved to Pretrain/ml-1m_GMF_8_1712733834.h5

最终numpy改成1.14.5版本

(tensorflow) D:\学习路线\NCF\Py3_Recommender-Systems-Samples-master\RecSysAndDeepLearning\DNN\ncf>python GMF.py --dataset ml-1m --epochs 2 --batch_size 256 --num_factors 8 --regs [0,0] --num_neg 4 --lr 0.001 --learner adam --verbose 1 --out 1 
Using TensorFlow backend.
regs [0, 0]
GMF arguments: Namespace(batch_size=256, dataset='ml-1m', epochs=2, learner='adam', lr=0.001, num_factors=8, num_neg=4, out=1, path='Data/', regs='[0,0]', verbose=1)      
Load data done [9.0 s]. #user=6040, #item=3706, #train=994169, #test=6040
2024-04-10 15:28:35.969964: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Init: HR = 0.1007, NDCG = 0.0460         [2.8 s]
Iteration 0 [25.5 s]: HR = 0.4940, NDCG = 0.2778, loss = 0.3652 [3.0 s]
Iteration 1 [24.5 s]: HR = 0.5560, NDCG = 0.3156, loss = 0.3089 [2.8 s]
End. Best Iteration 1:  HR = 0.5560, NDCG = 0.3156.
The best GMF model is saved to Pretrain/ml-1m_GMF_8_1712734106.h5

在这里插入图片描述

4、运行MLP.py文件

将epochs 改成3

python MLP.py --dataset ml-1m --epochs 3 --batch_size 256 --layers 
上一篇:【Webpack】TreeShaking与静态分析


下一篇:Javaweb开发:html【1】