Import or generate data
我们所有的机器学习算法都是依赖于数据,所以我们首先得准备好数据。Transform and normalize data
我们准备的数据很有可能不是算法所希望的格式,所以我们必须把数据转化为我们可以用的格式。还有大多数算法希望数据是经过标准化的,所以我们需要对数据进行标准化。
data = tf.nn.batch_norm_withglobal_normalization(...)
- Set algorithm parameters
我们的算法通常有一系类参数需要我们去设置,这些参数在算法的整个运行过程中是不变的。
learning rate = 0.01 iteration = 1000
- Initialize variables and placeholder
Tensorflow 取决于我们去告诉它什么是可以改变的,什么是不可以改变的。Tensorflow将在最小化损失函数的过程中改变变量。为了完成这个过程,我们通过定义placeholder和变量,并且初始化placeholder和variables的size和type.
a_var = tf.constant(42)
x_input = tf.placeholder(tf.float32,[None,input_size])
y_input = tf.placeholder(tf.float32,[None,Num_classes])
- Define the model structure
在我们准备好数据,并且初始化 varialbles 和 placeholder 之后,我们必须定义模型。这个听过建立一个计算图来完成的。我们告诉Tensorflow在varialbles和placeholder上执行什么操作来达到完成我们预测
y_pred = tf.add(tf.mul(x_inpt,weight_matrix),n_matrix)
- Declare the loss functions
在定义好模型之后,我们必须能够评估我们的输出,即声明损失函数
loss = tf.reduce_mean(tf.square(y_actual-y_pred))
- Initialize and train the model
现在一切都准备就绪啦,我们创建一个计算图实例,通过placeholder网计算图中喂数据,让Tensorflow通过改变变量来更好的拟合我们的数据。
with tf.Session(graph=graph) as session:
session.run(.....)
- Evaluate the model
- Predict new outcomes