zoukankan      html  css  js  c++  java
  • TensorFlow基本运算

    加法:

    tf.add(x,y,name=None)
    参数说明:
    x:一个张量,必须是下列类型之一:bfloat16/half/float32/uint8/int8/int16/int32/int64/complex64/complex128/string
    y: 一个张量,类型必须同x
    name:操作的名字,可选参数
    返回值: x+y
    一个张量,类型同x

    import tensorflow.compat.v1 as tf
    a = tf.constant(2)
    b = tf.constant(5)
    addOp = tf.add(a,b)
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(addOp))
    print(addOp)
    sess.close()

    减法:

    tf.subtract(x,y,name=None)
    参数说明:
    x:一个张量,必须是以下类型之一:bfloat16/half/float32/float64/uint8/int8/uint16/int16/int32/int64/complex64/complex128
    y:一个张量,类型同x
    name:张量操作的名字,可选参数。
    返回值: x-y
    一个张量,类型同x

    import tensorflow.compat.v1 as tf
    a = tf.constant(2)
    b = tf.constant(5)
    subOp = tf.subtract(a,b)
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(subOp))
    print(subOp)
    sess.close()

    乘法:

    tf.multiply(x,y,name=None)
    参数说明:
    x:一个张量,必须是下列类型之一:bfloat16/half/float32/float64/uint8/uint16/int16/int32/int64/complex64/complex128
    y:一个张量,类型同x
    返回值: x*y
    一个张量,类型同x

    import tensorflow.compat.v1 as tf
    a = tf.constant(2)
    b = tf.constant(5)
    mulOp = tf.multiply(a,b)
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(mulOp))
    print(mulOp)
    sess.close()

    tf.scalar_mul(scalar,x)
    参数说明:
    scalar:标量
    x:一个张量
    返回值: scalar*x

    import tensorflow.compat.v1 as tf
    b = tf.constant(5)
    scalar_mul_Op = tf.scalar_mul(2,b)
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(scalar_mul_Op))
    print(scalar_mul_Op)
    sess.close()

    除法:

    tf.div(x,y,name=None)
    参数说明:
    x:实数数值类型,分子
    y:实数类型,父母
    name:操作名字
    返回值:
    x/y

    import tensorflow.compat.v1 as tf
    a = tf.constant(2)
    b = tf.constant(5)
    div_op = tf.div(a,b)
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(div_op))
    print(div_op)
    sess.close()

  • 相关阅读:
    606. Construct String from Binary Tree
    696. Count Binary Substrings
    POJ 3255 Roadblocks (次短路)
    POJ 2823 Sliding Window (单调队列)
    POJ 1704 Georgia and Bob (博弈)
    UVa 1663 Purifying Machine (二分匹配)
    UVa 10801 Lift Hopping (Dijkstra)
    POJ 3281 Dining (网络流之最大流)
    UVa 11100 The Trip, 2007 (题意+贪心)
    UVaLive 4254 Processor (二分+优先队列)
  • 原文地址:https://www.cnblogs.com/a155-/p/14259596.html
Copyright © 2011-2022 走看看