版权声明:本文为博主原创文章,转载请告知一声 https://blog.csdn.net/zmdsjtu/article/details/85089289
利用 Anaconda 创建虚拟环境(python3.6暂时不支持)
conda create -n tensorflow pip python=3.5
activate tensorflow
conda install numpy
用VS2017的 x64 Native Tools Command Prompt for VS 2017 下载tensorflow源码
(如果下载失败, git config --global http.postBuffer 5242880000)
git clone https://github.com/tensorflow/tensorflow.git v1.5.0
cd v1.5.0
git checkout tags/v1.5.0
创建目录
cd tensorflow\contrib\cmake
mkdir build
cd build
利用cmake生成项目
cmake .. -A x64 ^
-DCMAKE_BUILD_TYPE=Release ^
-DSWIG_EXECUTABLE=C:\Users\Zhumingde\Desktop\swigwin-3.0.12\swig.exe ^
-DPYTHON_EXECUTABLE=C:\Users\Zhumingde\AppData\Local\conda\conda\envs\tensorflow\python.exe ^
-DPYTHON_LIBRARIES=C:\Users\Zhumingde\AppData\Local\conda\conda\envs\tensorflow\libs\python35.lib ^
-Dtensorflow_ENABLE_GPU=ON ^
-DCUDNN_HOME="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0" ^
-Dtensorflow_BUILD_PYTHON_BINDINGS=OFF ^
-Dtensorflow_ENABLE_GRPC_SUPPORT=OFF ^
-Dtensorflow_BUILD_SHARED_LIB=ON ^
-DCUDA_HOST_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC\bin/amd64/cl.exe"
1.5.0里的一行代码会让VS2017报错,将tensorflow\contrib\boosted_trees\lib\utils\sparse_column_iterable.cc里的
reference operator*() { return iter_->ix()(row_idx_, 0); }
替换为
reference operator*() const { return iter_->ix()(row_idx_, 0); }
然后就是愉快地编译了(或许要来两次……)
“C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\amd64\MSBuild.exe” ^
/m:1 ^
/p:CL_MPCount=1 ^
/p:Configuration=Release ^
/p:Platform=x64 ^
/p:PreferredToolArchitecture=x64 ALL_BUILD.vcxproj ^
/filelogger
安安静静干了三个小时别的事情,终于编完了
在生成的Release目录下可以看到tensorflow.lib以及tensorflow.dll,大功告成!
========================环境配置=====================
包含目录:
库目录
链接器-->输入加入tensorflow.lib
将tensorflow.dll从编译生成的release文件夹下拷贝到项目EXE同目录
测试代码:
#include <vector>
#include <eigen/Dense>
#include "tensorflow/core/public/session.h"
#include "tensorflow/cc/ops/standard_ops.h"
using namespace tensorflow;
GraphDef CreateGraphDef()
{
Scope root = Scope::NewRootScope();
auto X = ops::Placeholder(root.WithOpName("x"), DT_FLOAT,
ops::Placeholder::Shape({ -1, 2 }));
auto A = ops::Const(root, { { 3.f, 2.f },{ -1.f, 0.f } });
auto Y = ops::MatMul(root.WithOpName("y"), A, X,
ops::MatMul::TransposeB(true));
GraphDef def;
TF_CHECK_OK(root.ToGraphDef(&def));
return def;
}
int main()
{
GraphDef graph_def = CreateGraphDef();
// Start up the session
SessionOptions options;
std::unique_ptr<Session> session(NewSession(options));
TF_CHECK_OK(session->Create(graph_def));
// Define some data. This needs to be converted to an Eigen Tensor to be
// fed into the placeholder. Note that this will be broken up into two
// separate vectors of length 2: [1, 2] and [3, 4], which will separately
// be multiplied by the matrix.
std::vector<float> data = { 1, 2, 3, 4 };
auto mapped_X_ = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>
(&data[0], 2, 2);
auto eigen_X_ = Eigen::Tensor<float, 2, Eigen::RowMajor>(mapped_X_);
Tensor X_(DT_FLOAT, TensorShape({ 2, 2 }));
X_.tensor<float, 2>() = eigen_X_;
std::vector<Tensor> outputs;
TF_CHECK_OK(session->Run({ { "x", X_ } }, { "y" }, {}, &outputs));
// Get the result and print it out
Tensor Y_ = outputs[0];
std::cout << Y_.tensor<float, 2>() << std::endl;
session->Close();
system("pause");
}
运行结果: