• 机器学习系列-tensorflow-02-基本操作运算


    tensorflow常数操作

    ``` import tensorflow as tf # 定义两个常数,a和b a = tf.constant(2) b = tf.constant(3) # 执行默认图运算 with tf.Session() as sess: print("a=2, b=3") print("Addition with constants: %i" % sess.run(a+b)) print("Multiplication with constants: %i" % sess.run(a*b)) ``` **结果** a=2, b=3 Addition with constants: 5 Multiplication with constants: 6

    tensorflow变量操作

    变量作为图形输入,构造器的返回值作为变量的输出,在运行会话时,传入变量的值,在进行运算。 ``` import tensorflow as tf # 定义两个变量 a = tf.placeholder(tf.int16) b = tf.placeholder(tf.int16)

    定义加法和乘法运算

    add = tf.add(a, b)
    mul = tf.multiply(a, b)

    启动默认图进行运算

    with tf.Session() as sess:
    # 传入变量值,进行运算
    print("Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3}))
    print("Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3}))

    **结果**
    Addition with variables: 5
    Multiplication with variables: 6
    
    ----------------------
    <h1 style="background-color:#FF69B4; font-family:verdana; font-size:25px; text-align:center; font-weight:bold; color:white; ">tensorflow矩阵常量操作</h1>
    
    

    import tensorflow as tf

    创建一个1X2的常数矩阵

    matrix1 = tf.constant([[3., 3.]])

    创建一个2X1的常数矩阵

    matrix2 = tf.constant([[2.],[2.]])

    定义矩阵乘法运算multiplication

    product = tf.matmul(matrix1, matrix2)

    启动默认图进行运算

    with tf.Session() as sess:
    result = sess.run(product)
    print(result)

    **结果**
    [[12.]]
    
    <h1 style="background-color:#FF69B4; font-family:verdana; font-size:25px; text-align:center; font-weight:bold; color:white; ">简单例子</h1>
    

    import tensorflow as tf
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(hello))

    **结果**
    b'Hello, TensorFlow!'
    
    
    
    
    
    
    -------------------------------------
    参考:
    Author: Aymeric Damien
    Project: https://github.com/aymericdamien/TensorFlow-Examples/
  • 相关阅读:
    linux tcp中time_wait
    linux查看系统信息
    运行程序出错无法找到库文件
    安装keepalived
    python 深拷贝与浅拷贝
    python import eventlet包时提示ImportError: cannot import name eventlet
    [原创] 用两个queue实现stack的功能
    [原创] 用两个stack实现queue的功能
    [原创] 编写函数,实现对链表元素的排序与分类
    python 装饰器
  • 原文地址:https://www.cnblogs.com/brightyuxl/p/9880304.html
Copyright © 2020-2023  润新知