Use a small case tensorflow

Previous blog we have installed TensorFlow, there are a lot of people might be afraid of machine learning, but in fact there are a lot of principles that we now do not need to understand, we have to find out what role TensorFlow on the line, then the following me through a small case to illustrate how the use of TensorFlow


Inverse probability a probability of science

  • What is the inverse probability

  • We know for sure positive probability, for example is that the box has five black ball five white balls, then the probability of black ball and white ball you randomly get is 50%, now I do not know the box how many black balls white ball, then I should determine how the ball through continuous box how many black ball white ball of it, this is the famous inverse probability
  • The case in fact, machine learning and many times is the problem inverse probability, I have a lot of real examples, allowing the machine to find from these examples common features, such as a ten thousand cats pictures to machine learning, and then to find common characteristics ( two ears, four legs, beard, hairy, has a tail and other characteristics)
  • According to the concept of the inverse probability we cite other scenes

    • y = Ax + B (A, B are constants), this is a very simple mathematical equation, there are school-based people should know.
    • I now have a lot of x and y values, so the question is how to get the values ​​of A and B by the x and y values?

    Here I use TensorFlow to solve this problem!

    Second, the simple case

    Some basic information:

    1. The system is 16.04 LTS Ubuntu
    2. I use the sublime text3 to write code
    3.Python using a 3.5
    4.TensorFlow use is 0.12.0

    The example code

    #导入依赖库
    import numpy as np #这是Python的一种开源的数值计算扩展,非常强大
    import tensorflow as tf  #导入tensorflow 
    
    ##构造数据##
    x_data=np.random.rand(100).astype(np.float32) #随机生成100个类型为float32的值
    y_data=x_data*0.1+0.3  #定义方程式y=x_data*A+B
    ##-------##
    
    ##建立TensorFlow神经计算结构##
    weight=tf.Variable(tf.random_uniform([1],-1.0,1.0)) 
    biases=tf.Variable(tf.zeros([1]))     
    y=weight*x_data+biases
    ##-------##
    
    
    loss=tf.reduce_mean(tf.square(y-y_data))  #判断与正确值的差距
    optimizer=tf.train.GradientDescentOptimizer(0.5) #根据差距进行反向传播修正参数
    train=optimizer.minimize(loss) #建立训练器
    
    init=tf.initialize_all_variables() #初始化TensorFlow训练结构
    sess=tf.Session()  #建立TensorFlow训练会话
    sess.run(init)     #将训练结构装载到会话中
    
    for  step in range(400): #循环训练400次
         sess.run(train)  #使用训练器根据训练结构进行训练
         if  step%20==0:  #每20次打印一次训练结果
            print(step,sess.run(weight),sess.run(biases)) #训练次数,A值,B值
      
      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    The final result Figure:
    result

    Written in the last words

    Of course, this is just a very simple case, but as an entry TensorFlow very suitable, many people have been looking at the theory, not practice, which is fatal and program development, and you only practice will have a deeper It is understanding, and when the program runs successfully will bring you a lot of confidence to go on supporting you.

    Guess you like

    Origin blog.csdn.net/l641208111/article/details/87857929