zoukankan      html  css  js  c++  java
  • TensorFlow实现XOR

    TensorFlow基础

    1、概念

    • TF使用图表示计算任务,图包括数据(Data)、流(Flow)、图(Graph)
    • 图中节点称为op,一个op获得多个Tensor
    • Tensor为张量,TF中用到的数据都是Tensor
    • 图必须在会话中启动

    示例

    计算两个矩阵的乘积,

    
    x = tf.constant([[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]])
    y = tf.constant([[0,0,1.0],[0,0,1.0],[0,0,1.0]])
    z = tf.matmul(x3,y3)
    
    # Session激活z,得到计算结果
    with tf.Session() as sess:
        print(sess.run(z))
        
    
    

    2、Tensor类型

    (1)常量

    值不可变

    constant(
    	value,(数值)
    	dtype=None,(数据类型)
    	shape=None,(指定形状)
    	name='Const',(命名)
    	verify_shape=False()
    )
    

    代码

    
    x = tf.constant([[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]],dtype=tf.float32,shape=[3,3],name='x')
    
    # 简写
    x = tf.constant([[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]])
    
    
    

    (2)变量

    代码

    
    v2=tf.Variable(tf.constant(2),name='x')
    
    
    

    (3)占位符

    定义过程,执行时赋值

    placeholder(
    	value,(数值)
    	dtype=None,(数据类型)
    	shape=None,(指定形状)
    )
    

    代码

    
    x = tf.placeholder(tf.float32)
    y = tf.placeholder(tf.float32)
    z = tf.multiply(x, y)
    
    with tf.Session() as sess:
        print(sess.run(z, feed_dict={x:[1.0] , y: [2.0]}))
        
    
    

    (4)平均值

    计算张量的各个维度上的元素的平均值。

    reduce_mean(
        input_tensor,
        axis=None,
        keep_dims=False,
        name=None,
        reduction_indices=None
    )
    
    

    代码

    
    x = tf.constant([[1.0,2.0],[3.0,4.0]],dtype=tf.float32,shape=[2,2])
    tf.reduce_mean(x) ==> 2.5
    tf.reduce_mean(x, 0) ==> [2. 3.]
    tf.reduce_mean(x, 1) ==> [1.5 3.5]
    
    
    

    (5) 优化器

    tf.train.GradientDescentOptimizer 是实现梯度下降算法的优化器。

    机器学习、深度学习概念

    1、代价函数

    整个训练集上所有样本误差的平均。

    2、目标函数

    经过优化后,期望获得的函数。

    3、激活函数

    负责将神经元的输入映射到输出端。增加神经网络模型的非线性

    激活函数几种常见类型:

    • sigmod函数

    [f(x) = frac 1 {1+e^{-1}} ]

    • tanh函数

    [f(x) = frac {e^x-e^{-x}} {e^x+e^{-x}} ]

    • Relu函数

    [f(x)= egin{cases} 0 &x le 0 \\ x &x>0 end{cases} ]

    4、学习率

    学习率决定参数移动到最优值速度快慢。学习率过大,会越过最优值。学习率过小,优化效率低

    5、前向传播(Forward Propagation)

    第n层神经元的值决定第n+1层神经元的值。

    6、反向传播(Back Propagation)

    通过前向传播获取到的结果。为减少误差,进行反向求偏导数,修正参数,再进行前向传播,一直迭代,直到训练获得最小的误差。

    代码实现

    
    import numpy as np
    import tensorflow as tf
    
    # 训练样本占位
    data = tf.placeholder(tf.float32, shape=(4, 2))
    label = tf.placeholder(tf.float32, shape=(4, 1))
    
    with tf.variable_scope('layer1') as scope:
      # 权重
      weight = tf.get_variable(name='weight', shape=(2, 2))
      # 偏置项
      bias = tf.get_variable(name='bias', shape=(2,))
      x = tf.nn.sigmoid(tf.matmul(data, weight) + bias)
    
    with tf.variable_scope('layer2') as scope:
      weight = tf.get_variable(name='weight', shape=(2, 1))
      bias = tf.get_variable(name='bias', shape=(1,))
      x = tf.matmul(x, weight) + bias
    
    # 激活函数
    preds = tf.nn.sigmoid(x)
    
    # 损失函数
    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=label, logits=x))
    
    # 学习率占位
    learning_rate = tf.placeholder(tf.float32)
    
    # 梯度下降优化器
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
    
    # 训练样本
    train_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
    train_label = np.array([[0], [1], [1], [0]])
    
    # 执行
    with tf.Session() as sess:
      
      # 初始化变量
      sess.run(tf.global_variables_initializer())
      
      for step in range(10000):
        if step < 3000:
          lr = 1
        elif step < 6000:
          lr = 0.1
        else:
          lr = 0.01
        _, l, pred = sess.run([optimizer, loss, preds], feed_dict={data: train_data, label: train_label, learning_rate: lr})
        if step % 500:
          print('Step: {} -> Loss: {} -> Predictions: {}'.format(step, l, pred))
          
    
    

    TensorBoard与计算图可视化

    TensorBoard是一个可视化工具,能够有效地展示Tensorflow在运行过程中的计算图、各种指标随着时间的变化趋势以及训练中使用到的数据信息。

    代码

    writer = tf.summary.FileWriter('graphs',tf.get_default_graph())
    writer.close()
    

    打开图,输入命令

    zhijiefang@fangzhijie-PC:~/test$ tensorboard --logdir=graphs
    TensorBoard 1.11.0 at http://fangzhijie-PC:6006 (Press CTRL+C to quit)
    

    计算图显示

    image.png

    运行结果

    
    ...
    
    Step: 9993 -> Loss: 0.3484194874763489 -> Predictions: [[0.00179099]
     [0.49935436]
     [0.9978059 ]
     [0.50105   ]]
    Step: 9994 -> Loss: 0.3484194874763489 -> Predictions: [[0.00179098]
     [0.49935436]
     [0.9978059 ]
     [0.50105   ]]
    Step: 9995 -> Loss: 0.3484194874763489 -> Predictions: [[0.00179098]
     [0.49935436]
     [0.9978059 ]
     [0.50105   ]]
    Step: 9996 -> Loss: 0.3484194874763489 -> Predictions: [[0.00179097]
     [0.49935436]
     [0.9978059 ]
     [0.50105   ]]
    Step: 9997 -> Loss: 0.3484194576740265 -> Predictions: [[0.00179096]
     [0.49935436]
     [0.9978059 ]
     [0.50105   ]]
    Step: 9998 -> Loss: 0.3484194278717041 -> Predictions: [[0.00179096]
     [0.49935436]
     [0.9978059 ]
     [0.50105   ]]
    Step: 9999 -> Loss: 0.3484194278717041 -> Predictions: [[0.00179095]
     [0.49935436]
     [0.9978059 ]
     [0.50104994]]
    
    
    
  • 相关阅读:
    git init 与 git init --bare 区别
    python_集合_笔记
    git笔记
    screen命令
    python的and和or优先级
    计算机语言的发展史
    python3颜色输出
    mysql_windows解压包安装
    那些经常不开心的上班族
    mysql主从搭建
  • 原文地址:https://www.cnblogs.com/fonxian/p/10461811.html
Copyright © 2011-2022 走看看