此处有详细说明:https://github.com/tensorflow/tensorflow/tree/v1.2.0/tensorflow/contrib/cmake
1.安装anacoda3,即安装python即相关库:https://www.continuum.io/downloads#windows(python为3.6版本)
2.从git中下载tensorflow源码:https://github.com/tensorflow/tensorfl:ow
3.下载windows版本的swig:http://www.swig.org/download.html,本例解压到C盘
4.进入tensorflow源码目录:D:\tensorflow-1.2.0\tensorflow\contrib\cmake,新建build文件夹,进入build后,并执行命令如下(本例为CPU版)
cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release -DSWIG_EXECUTABLE=C:/swigwin-3.0.12/swig.exe -DPYTHON_EXECUTABLE=C:/ProgramData/Anaconda3/python.exe -DPYTHON_LIBRARIES=C:/ProgramData/Anaconda3/libs/python36.lib
cmake中就会卡住,原因不明
注:
原来按照tensorflow官方的windows方法安装,使用anacoda下的pip进行安装,但无法针对CPU的SSE等指令集进行优化(没有GPU情况下),故采用源码方法安装。
但据说https://stackoverflow.com/questions/42603407/how-to-compile-tensor-flow-with-sse-and-and-avx-instructions-on-windows
即使cpu优化 也没有什么好处, 故放弃这种方法
Tensorflow学习笔记
20170710
TensorFlow’shigh-level machine learning API (tf.contrib.learn)
训练及识别过程:
1. Load CSVs containing Iris training/testdata into a TensorFlowDataset
2. Construct aneural network classifier
3. Fit the model using the training data
4. Evaluate the accuracy of the model
5. Classify new samples
Where isos.path?
# 打开数据集
load_csv_with_header()method in learn.datasets.base
,返回值为
dataset
数据集格式:Csv文件
第一行特殊:样本的个数特征向量的维度类别的名称
余下若干行:特征向量的内容类别标签
dataset是named tuples,training_set.data
与training_set.target
分别表示特征向量和标签。
# 定义网络结构
#Specify that all features have real-value data
feature_columns =[tf.contrib.layers.real_valued_column("", dimension=4)]
# Build 3 layer DNN with 10, 20, 10 unitsrespectively.
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10,20,10],
n_classes=3,
model_dir="/tmp/iris_model")
# 开始训练
#Define the training inputs
def get_train_inputs():
x = tf.constant(training_set.data)
y = tf.constant(training_set.target)
return x, y
# Fitmodel.
classifier.fit(input_fn=get_train_inputs, steps=2000)
#验证结果
#Define the test inputs
def get_test_inputs():
x = tf.constant(test_set.data)
y = tf.constant(test_set.target)
return x, y
# Evaluate accuracy.
accuracy_score =classifier.evaluate(input_fn=get_test_inputs,
steps=1)["accuracy"]
print("\nTest Accuracy: {0:f}\n".format(accuracy_score))
针对数据集训练迭代4000次loss为0.0287,测试集准确率为0.966
※Custom Input Pipelines withinput_fn
defmy_input_fn():
# Preprocessyour data here...
# ...thenreturn 1) a mapping of feature columns to Tensors with
# thecorresponding feature data, and 2) aTensor containing labels
returnfeature_cols, labels
输出前,应该将数组转变为Tensor[tf.constant(…)]
或者SparseTensor[tf.SparseTensor(…)](三参数定义dense_shape, indices, values
)
※在尝试cwTH训练时侯出现错误
raise type(e)(node_def, op, message)
更改DNNClassifier中model_dir目录即可,否则会在这目录训练好的基础上继续训练
训练结果显示四层(56,48,32,16)隐层的网络两万次迭代可以实现训练集90+的结果,但泛化能力差
20170711
C API 中包括了 run apredefined graph的功能:
A language binding isexpected to define the following classes:
·Graph: A graph representing a TensorFlow computation. Consists ofoperations (represented in the client language by Operations) and corresponds to a TF_Graph in the C API. Mainly used as an argument when creatingnew Operation objects and when starting a Session. Also supports iterating through the operations in the graph (TF_GraphNextOperation), looking up operations by name (TF_GraphOperationByName), and converting to and from a GraphDef protocol message (TF_GraphToGraphDef and TF_GraphImportGraphDef in the C API).
·Operation: Represents a computation node in the graph. Corresponds toa TF_Operation in the C API.
·Output: Represents one of the outputs of an operation in the graph.Has a DataType (and eventually a shape). May be passed as an inputargument to a function for adding operations to a graph, or to a Session's Run()method to fetchthat output as a tensor. Corresponds to a TF_Output in the C API.
·Session: Represents a client to a particular instance of the TensorFlowruntime. Its main job is to be constructed with a Graph and some options and then field calls to Run() the graph. Corresponds to a TF_Session in the C API.
·Tensor: Represents an N-dimensional (rectangular) array with elementsall the same DataType.Gets data into and out of a Session's Run() call.Corresponds to a TF_Tensor inthe C API.
·DataType: An enumerant with all the possible tensor types supported byTensorFlow. Corresponds to TF_DataType in the C API and often referred to as dtype in the Python API.