TensorFlow study notes (1): The idea of TensorFlow design model

TensorFlow writing steps:

  1. Variables needed to create the model
  2. Defining a Loss Function and an Optimization Method for Solving the Loss Function
  3. initialize all variables
  4. Iteratively train the model
  5. Final evaluation model

step 1

1) It can be created with placeholders. For example, x = tf.placeholder("float",[None,20])
the x variable is created by the placeholder. Later, a vector of any length (None) can be added to the x variable, and each vector has 20 dimensions.
2) Some weight parameters can be created by assigning initial values. For
example , create a W, which contains a 784-long vector, and each vector has 10 dimensions.W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

Step 2

1) General loss functions include cross entropy loss function, mean square error loss function, logarithmic loss function, exponential loss function, hinge loss function, etc.; for
example: cross_entropy = -tf.reduce_sum(y_*tf.log(y))loss function
2) For the optimization solution method, there is gradient descent method, Newton's method, quasi-Newton's method and momentum method, Adam and so on.
For example, train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)the gradient descent method is used to solve the cross entropy loss function.

Step 3

Initialize parameters, generally use the following code:

init = tf.initialize_all_variables()
sess.run(init)

Step 4

For the training model, you can set many conditions for iterative stop, such as: setting the number of iterations, setting how much accuracy is met, and then stopping, etc. The following example is the set number of iterations

Step 5

There are many methods and indicators for evaluating the model: such as precision rate, recall rate, F-Score value, etc., which I have not learned yet, please look forward to the next article.

The following is the simplest example implemented with tensorflow

#这里实现的是一个softmax回归的示例
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
sess = tf.Session()

#第一步加载数据定义超参数
x = tf.placeholder("float",[None,784])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.add(tf.matmul(x,W) , b))
#y 表示经过模型计算出的预测值
#y_ 表示模型本身的标签值
y_ = tf.placeholder("float",[None,10])

#第二步:构建损失函数

#这里使用交叉熵损失函数
cross_entropy = -tf.reduce_sum(y_*tf.log(y))

#第三步:初始化参数
init = tf.initialize_all_variables()
sess.run(init)

#第四步:训练模型
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

#训练1000次
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, {x:batch_xs, y_:batch_ys})

#第五步:评估模型和进行预测
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325502394&siteId=291194637