zoukankan      html  css  js  c++  java
  • tensorflow常用函数

    1、numpy数组转为张量

    1 import tensorflow as tf
    2 import numpy as np
    3 a = np.arange(0, 5)
    4 b = tf.convert_to_tensor(a, dtype=tf.int64)  #numpy 转为张量,本机默认dtype为int32
    5 print("a:", a)
    6 print("b:", b)

    2、创建张量

     1 import tensorflow as tf
     2 import numpy as np
     3 x=np.zeros([2,3],dtype=np.int64)
     4 a = tf.zeros([2, 3])
     5 b = tf.ones(4)
     6 c = tf.fill([2, 2], 9)
     7 d=tf.convert_to_tensor(x)
     8 print("a:", a)
     9 print("b:", b)
    10 print("c:", c)
    11 print('d',d)

    3、使用函数创建张量

    import tensorflow as tf
    
    d = tf.random.normal([2, 2], mean=0.5, stddev=1)#d为2*2的张量,均值为0.5,标准差为1
    print("d:", d)
    e = tf.random.truncated_normal([2, 2], mean=0.5, stddev=1,dtype=tf.float64)#正态分布e,shape2*2,均值0.5,标准差1,
    print("e:", e)

    4、创建均匀分布随机浮点数张量

    import tensorflow as tf
    import numpy as np
    a = tf.random.uniform([2, 2], minval=0, maxval=1)
    b = tf.random.uniform((1,10), minval=1, maxval=10,dtype=tf.float64)#范围为[low, high]的均匀分布随机浮点数张量
    print("f:", a)
    print('a:',b)

    5、类型转换

    import tensorflow as tf
    
    x1 = tf.constant([1.1, 2.2, 3.3], dtype=tf.float64)
    print("x1:", x1)
    x2 = tf.cast(x1, tf.int32)
    print("x2", x2)
    print("minimum of x2:", tf.reduce_min(x2))
    print("maxmum of x2:", tf.reduce_max(x2))

    6、求均值,求和

    import tensorflow as tf
    import numpy  as np
    
    x=tf.constant([[1,2,3],[2,3,4]])
    print('x:',x)
    print('均值:',tf.reduce_sum(x))
    print('行求和:',tf.reduce_sum(x,axis=1))

    7、加减乘除

    import tensorflow as tf
    import numpy  as np
    a=tf.ones((1,5),dtype=tf.int32)
    b=tf.fill((1,5),4)
    c=tf.fill((5,1),6)
    print('a+b:',tf.add(a,b))
    print('a-b:',tf.subtract(a,b))
    print('a*c:',tf.multiply(a,c))
    print('a/b:',tf.divide(a,b))

    8、指数运算

    1 import numpy  as np
    2 a=tf.fill([1,2],9.)
    3 print('a的立方:',tf.pow(a,3))
    4 print('a的平方:',tf.square(a))
    5 print('a的开方:',tf.sqrt(a))
    6 print('a的开方:',tf.pow(a,0.5))

     9、求导

    1 import tensorflow as tf
    2 with tf.GradientTape() as tape:
    3     x=tf.Variable(tf.constant(3.0))
    4     y=tf.pow(x,2)
    5 grad=tape.gradient(y,x)
    6 print(grad.numpy())

    10

    1 import tensorflow as tf
    2 
    3 classes = 3
    4 labels = tf.constant([1, 0, 2])  # 输入的元素值最小为0,最大为2
    5 output = tf.one_hot(labels, depth=classes)
    6 print("result of labels1:", output)

    11、

    1 import tensorflow as tf
    2 import math
    3 y = tf.constant([1.01, 2.01, -2.01])
    4 y_pro = tf.nn.softmax(y)# y_pro 符合概率分布
    5 a1=math.exp(1.01)/(math.exp(1.01)+math.exp(2.01)+math.exp(-2.01))
    6 print( y_pro)  # y_pro 符合概率分布y_pro[0]=a1
    7 print("The sum of y_pro:", tf.reduce_sum(y_pro))  # 通过softmax后,所有概率加起来和为1
  • 相关阅读:
    Redis-内存优化(一)
    window激活
    ArrayDeque原理详解
    CountDownLatch原理详解
    DelayQueue延迟队列原理剖析
    浅析PriorityBlockingQueue优先级队列原理
    修改QT库的路径
    数据同步Datax与Datax_web的部署以及使用说明
    HTTP头的Expires与Cache-control
    python生成随机数、随机字符串
  • 原文地址:https://www.cnblogs.com/hsy1941/p/12969102.html
Copyright © 2011-2022 走看看