美文网首页
Tensorflow基础学习

Tensorflow基础学习

作者: 无米的快乐生活 | 来源:发表于2017-11-22 21:18 被阅读0次
    # coding:utf8
    """
    author:无米的快乐生活~
    """
    from __future__ import print_function
    import tensorflow as tf
    
    # 常量节点1
    node1 = tf.constant(3.0, dtype=tf.float32)
    # 常量节点2
    node2 = tf.constant(4.0)
    
    print(node1)
    print(node2)
    #Tensor("Const:0", shape=(), dtype=float32)
    #Tensor("Const_1:0", shape=(), dtype=float32)
    
    # 创建 Session 对象,并调用 run 函数执行节点
    sess = tf.Session()
    print(sess.run([node1, node2]))
    #[3.0, 4.0]
    
    node3 = tf.add(node1, node2)
    print("node3:", node3)
    #node3: Tensor("Add:0", shape=(), dtype=float32)
    print("sess.run(node3):", sess.run(node3))
    #sess.run(node3): 7.0
    
    # 占位符,参数化接受外部输入
    a = tf.placeholder(tf.float32)
    b = tf.placeholder(tf.float32)
    adder_node = a + b
    print(adder_node)
    #Tensor("add:0", dtype=float32)
    print(tf.add(a,b))
    #Tensor("Add_1:0", dtype=float32)
    
    print(sess.run(adder_node, {a:3, b:4.5}))
    #[ 3.  7.]
    print(sess.run(adder_node, {a:[1,3], b:[2,4]}))
    #[ 3.  7.]
    
    add_and_tripple = adder_node * 3
    print(sess.run(add_and_tripple, {a:3, b:4.5}))
    #22.5
    
    
    
    '''
    创建并使用模型
    '''
    # 变量允许我们像graph 中添加可训练的参数,通过类型和初始value 创建
    W = tf.Variable([.3], dtype=tf.float32)
    b = tf.Variable([-.3], dtype=tf.float32)
    x = tf.placeholder(tf.float32)
    linear_model = W * x + b
    
    print(W, b, x, linear_model)
    #<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref> <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref> Tensor("Placeholder_2:0", dtype=float32) Tensor("add_1:0", dtype=float32)
    
    #显式调用初始化所有变量的函数
    init = tf.global_variables_initializer()
    print(W, b, x, linear_model)
    #<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref> <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref> Tensor("Placeholder_2:0", dtype=float32) Tensor("add_1:0", dtype=float32)
    sess.run(init)
    print(W, b, x, linear_model)
    #<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref> <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref> Tensor("Placeholder_2:0", dtype=float32) Tensor("add_1:0", dtype=float32)
    
    
    print(sess.run(linear_model, {x:[1, 2,3, 4]}))
    #[ 0.          0.30000001  0.60000002  0.90000004]
    
    
    
    '''
    评估模型
    '''
    
    y = tf.placeholder(tf.float32)
    squared_deltas = tf.square(linear_model - y)
    loss = tf.reduce_sum(squared_deltas)
    print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
    #23.66
    
    # 我们非常精准地猜出了 W 和 b 的精确值……
    fixW = tf.assign(W, [-1.])
    fixb = tf.assign(b, [1.])
    sess.run([fixW, fixb])
    
    print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
    #0.0
    
    '''
    优化
    '''
    # 梯度下降优化,为什么是0.01?
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = optimizer.minimize(loss)
    
    # 从新初始化参数
    sess.run(init)
    for i in range(1000):
        sess.run(train, {x: [1, 2, 3, 4], y: [ 0, -1, -2, -3]})
    print(sess.run([W, b]))
    #[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]
    print(sess.run([W, b, loss], {x: [1, 2, 3, 4], y: [ 0, -1, -2, -3]}))
    #[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32), 5.6999738e-11]
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:Tensorflow基础学习

          本文链接:https://www.haomeiwen.com/subject/djoovxtx.html