zoukankan      html  css  js  c++  java
  • 1 TensorFlow入门笔记之基础架构

    ————————————————————————————————————

    写在开头:此文参照莫烦python教程(墙裂推荐!!!)

    ————————————————————————————————————

    TensorFlow入门笔记之基础架构

    1 构建简单神经网络:一维线性预测

    #导入相关库
    import tensorflow as tf
    import numpy as np
    #用随机数生成x
    x_data = np.random.rand(100).astype(np.float32)  #生成100个x
    y_data = x_data * 0.1 + 0.3      #设定y=0.1x+0.3
    #构建神经网络
    Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))#初始化Weights为-1.0到1.0的某个随机数
    biases = tf.Variable(tf.zeros([1]))  #biases初始化为0
    
    y = Weights*x_data + biases   #计算当前神经网络预测的y
    
    loss = tf.reduce_mean(tf.square(y-y_data)) #损失函数
    
    optimizer = tf.train.GradientDescentOptimizer(0.5) #神经网络优化器,0.5为学习效率,一般小于1
    train = optimizer.minimize(loss)  #训练使得损失函数最小
    
    init = tf.initialize_all_variables() #初始化神经网络的结构
    #激活神经网络
    sess = tf.Session()
    sess.run(init)   #激活
    for step in range(201):
        sess.run(train)
        if step % 20 == 0:  #每20步打印训练的Weights和biases
            print(step,sess.run(Weights),sess.run(biases))  #依次输出步数和当前Weights和当前biases
    0 [0.0999992] [0.3000004]
    20 [0.09999981] [0.3000001]
    40 [0.0999999] [0.30000007]
    60 [0.0999999] [0.30000007]
    80 [0.0999999] [0.30000007]
    100 [0.0999999] [0.30000007]
    120 [0.0999999] [0.30000007]
    140 [0.0999999] [0.30000007]
    160 [0.0999999] [0.30000007]
    180 [0.0999999] [0.30000007]
    200 [0.0999999] [0.30000007]
    

    因为我们的Weights真实值为0.1,biases真实值为0.3,所以从上面结果可知,这个网络的效果还是挺不错的!

    2 Session 会话控制

    可以用Session.run()来运行已经创好了的结构

    import tensorflow as tf
    
    matrix1 = tf.constant([[3,3]])
    matrix2 = tf.constant([[2],
                           [2]])
    product = tf.matmul(matrix1,matrix2)  #矩阵相乘

    下面有两种方式来进行会话控制

    #方式一
    sess = tf.Session()
    result = sess.run(product)
    print(result)
    sess.close()
    [[12]]
    
    #方式二,with部分运行后,会自动close Session,而不同显示close
    with tf.Session()as sess:
        result2 = sess.run(product)
        print(result2)
    [[12]]
    

    上面两种方式得到的结果都是一样的,均为12

    3 Variable变量

    state = tf.Variable(0,name='counter')
    print(state.name)#打印看看是啥
    counter:0
    
    one = tf.constant(1)  #常量1
    
    new_value = tf.add(state,one) 
    update = tf.assign(state,new_value)  #把new_value赋给state
    
    init = tf.initialize_all_variables()#初始化所有变量.一定要记得init所有的Variables
    
    with tf.Session() as sess:
        sess.run(init)
        for _ in range(3):
            sess.run(update)
            print(sess.run(state))
    1
    2
    3
    

    4 Placeholder传入值

    Placeholder主要用来代替某些你不想马上给定值的变量,而是在run或者特定位置再传值给它,有点类似于用户输入?。。。下面是一个例子。

    import tensorflow as tf
    
    input1 = tf.placeholder(tf.float32)#也可以是(tf.float32,[2,2]),这样给定input1是2行2列的float32
    input2 = tf.placeholder(tf.float32)
    
    output = tf.multiply(input1,input2)  #相乘
    
    with tf.Session() as sess:
        print(sess.run(output,feed_dict={input1:[7.],input2:[2.]})) #这里才给input1,input2值
    [14.]
    

    5 激励函数

    • 激励函数可以把线性函数变成非线性函数,常见的激励函数有:relu/sigmoid/tanh。当然啦,你也可以自己定义激励函数,但你要保证你的函数是可微分的!
    • 多层神经网络时要慎重选择激励函数。
    • 少量层神经网络结构可以随便尝试激励函数;卷积神经网络推荐使用relu;循环神经网络推荐使用relu/tanh。

    *点击[这儿:TensorFlow]发现更多关于TensorFlow的文章*


  • 相关阅读:
    SVN使用指引(Windows)
    Android开发14——监听内容提供者ContentProvider的数据变化
    Android开发13——内容提供者ContentProvider的基本使用
    Android开发12——Andorid中操作数据库的insert的两种方法以及nullColumnHack
    Android开发11——手机横屏和竖屏与android:configChanges
    Android开发10——Activity的跳转与传值
    Android开发9——Activity的启动模式
    Android开发8——利用pull解析器读写XML文件
    Android开发7——android.database.CursorIndexOutOfBoundsException:Index -1 requested
    Android开发6——布局中的wrap_content和fill_parent以及match_parent
  • 原文地址:https://www.cnblogs.com/surecheun/p/9648970.html
Copyright © 2011-2022 走看看