zoukankan      html  css  js  c++  java
  • 2.2tensorflow2.3常量定义、类型、使用实例、运算

    自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

    https://www.cnblogs.com/bclshuai/p/11380657.html

    1.1  常量constant

    1.1.1         常量的声明

    值不能改变的张量。创建通过constant来实现, constant()是一个函数,提供在tensorflow中定义常量(不可更改的张量)的方法。

    tf.constant(

        value,//值列表,多个值用[]包括,逗号分开,按照shape指定的形状去生成,列表中的值数量等于shape(例如2*3)定义的大小,否则tensorflow2.3会报错。

        dtype=None,.//定义常量的数据类型

        shape=None,//如果要设置行列,可以用[2,3]设置,表示2行3列

        name='Const',//定义变量的名字

        verify_shape=False// verify_shape默认为False,如果修改为True的话表示检查value的形状与shape是否相符,如果不符会报错。

    )

    1.1.2         常量的类型

    (1)标量常量

    t_1 = tf.constant(4)

    (2)向量常量1行3

    t_2 = tf.constant([4,3,2])

    (3)[M,N] 的零元素矩阵

    M行N列的零元素矩阵,数据类型(dtype)可以是 int32、float32 等:

    tf.zeros([M,N],tf.dtype);

    zero_t = tf.zeros([2,3],tf.int32) # Results in an 2x3 array of zeros:[[0 0 0],[0 0 0]]

    (4)[M,N]元素均为 1 的矩阵

    ones_t = tf.ones([2,3],tf.int32) # Results in an 2x3 array of ones:[[1 1 1],[1 1 1]]

    (5)等差排布的序列

    tf.linspace(start,stop,num)#起始值和总的数量

    相应的值为 (stop-start)/(num-1)。例如:

    range_t = tf.linspace(2.0,5.0,5)

    #We get:[2. 2.75 3.5 4.25 5.]

    从开始(默认值=0)生成一个数字序列,增量为 delta(默认值=1),直到终值(但不包括终值):

    tf.range(start,limit,delta)//起始值和增量

    下面给出实例:

    range_t = tf.range(10)

    #Result:[0 1 2 3 4 5 6 7 8 9]

    (6)形状为 [M,N] 的正态分布随机数组

    t_random=tf.random_normal([M,N],mean=2.0,stdev=4,seed=12)

    其中random_normal是随机数的类型,还有另外两种:截尾正太分布随机数组truncated_normal, M和N表示M行N列,mean表示随机数组中值的平均值,stddev表示随机值的标准差,seed表示随机种子。

    (7)限定范围的随机正太分布数组

    [minval(default=0),maxval] 范围内创建形状为 [M,N] 的给定伽马分布随机数组

    t_random=tf.random_uniform([2,3],minval=1,maxval=4,seed=12)。

    (8)从随机数组中裁剪出指定大小的数组,行列都要小于等于原矩阵

    tf.random_crop(t_random,[2,2],seed=12)

    1.1.3         常量的使用实例

    import tensorflow as tf
    # int型
    a = tf.constant(1)
    print(a)
    # float型
    b = tf.constant(1.)
    print(b)
    # double型
    c = tf.constant(2.,dtype=tf.float64)
    print(c)
    # bool型
    d = tf.constant([True,False])
    print(d)
    # string型
    e = tf.constant('hello world')
    print(e)
    #向量
    f=tf.constant([1,2,3])
    print(f)
    #矩阵
    g=tf.constant([1,2,3,4,5,6],shape=[2,3])
    print(g)
    h=tf.constant(1,shape=[3,2])
    print(h)
    i=tf.compat.v1.constant([[1,2],[4,5]],shape=[2,2],verify_shape=False)
    print(i)
    #元素值都为0的矩阵zeros([M,N],tf.dtype)
    zero_t = tf.zeros([2,3],tf.int32)
    print(zero_t)
    """输出
    tf.Tensor(
    [[0 0 0]
     [0 0 0]], shape=(2, 3), dtype=int32)
     """
    #元素都为1的矩阵
    one_t=tf.ones([3,2],tf.int32)
    print(one_t)
    """输出
    tf.Tensor(
    [[1 1]
     [1 1]
     [1 1]], shape=(3, 2), dtype=int32)
     """
    #等差分布的向量
    range_t =tf.linspace(2.0, 5.0, 5)
    print(range_t)#输出tf.Tensor([2.   2.75 3.5  4.25 5.  ], shape=(5,), dtype=float32)
    #形状为 [M,N] 的正态分布随机数组
    t_randomnormal=tf.compat.v1.random_normal([4,4],mean=2.0,stddev=4,seed=12)
    print(t_randomnormal)
    """输出
    [[ 0.25347447  5.37991     1.9527606  -1.5376031 ]
     [ 1.2588985   2.8478067  11.545206   -1.0216885 ]
     [ 3.6549058   6.6507907   4.42358     1.4614596 ]
     [ 1.4433938   1.7380679  -1.6191797   2.0041263 ]], shape=(4, 4), dtype=float32)
     """
    #限定范围的随机正太分布数组
    t_randomuniform=tf.compat.v1.random_uniform([2,2],minval=1,maxval=4,seed=12)
    print(t_randomuniform)
    """输出
    tf.Tensor(
    [[2.9084575 3.7722745]
     [3.028832  2.5063753]], shape=(2, 2), dtype=float32)
     """
    #从大的数组中裁剪出出小的矩阵,行列都要小于等于大的矩阵
    t_randomcrop=tf.compat.v1.random_crop(t_randomnormal,[2,3],seed=12)
    print(t_randomcrop)
    """输出
    tf.Tensor(
    [[ 2.8478067 11.545206  -1.0216885]
     [ 6.6507907  4.42358    1.4614596]], shape=(2, 3), dtype=float32)
    """

    1.1.4         算术运算符运算

    对于除法多说两句,Tensor有两种除法,一种是”/”,另一种是”//”。”/”是浮点除法,对应的是tf.truediv,而”//”是计算整除,对应tf.floordiv。

    d01 = tf.constant(1)
    d02 = tf.constant(2)
    d_add = d01 + d02
    print(d_add)#Tensor("add:0", shape=(), dtype=int32)

    d_sub = d01 - d02
    print(d_sub)#Tensor("sub:0", shape=(), dtype=int32)

    d_mul = d01 * d02
    print(d_mul)#Tensor("mul:0", shape=(), dtype=int32)
    d_fdiv = d01 / d02#浮点数运算
    print(d_fdiv)#Tensor("truediv:0", shape=(), dtype=float64)
    d_idiv=d01//d02#整型运算
    print(d_idiv)#Tensor("floordiv:0", shape=(), dtype=int32)
    d_mod = d01 % d02
    print(d_mod)#Tensor("mod:0", shape=(), dtype=int32)

    d_minus = -d01
    print(d_minus)#Tensor("Neg:0", shape=(), dtype=int32)

    1.1.5         比较运算符运算

    对于>, <, >=, <=等关系,都会生成一个需要Session来运算的Tensor对象。只有==是例外,它会立即返回这两个Tensor是否是同一对象的结果

    d11 = d01 > d02
    d12 = d01 < d02
    d13 = d01 == d02
    d14 = d01 >= d02
    d15 = d01 <= d02

    print(d11)#Tensor("Greater:0", shape=(), dtype=bool)
    print(d12)#Tensor("Less:0", shape=(), dtype=bool)
    print(d13)#False
    print(d14)#Tensor("GreaterEqual:0", shape=(), dtype=bool)
    print(d15)#Tensor("LessEqual:0", shape=(), dtype=bool)

    1.1.6         数学运算

    整形,一定要先转换成浮点型才能进行sqrt,sin等数学函数计算。


    d31 = tf.constant(100, dtype=tf.float64)
    d32 = tf.sqrt(d31)
    print(ss.run(d32))#10.0
  • 相关阅读:
    CentOS 7.0关闭默认防火墙启用iptables防火墙
    centos7 启动httpd的时候为什么显示是这样的
    CentOS配置本地yum源/阿里云yum源/163yuan源,并配置yum源的优先级
    Linux如何用yum安装软件或服务
    IE浏览器和Firefox浏览器兼容性问题及解决办法
    Input的size与maxlength属性的区别
    下拉框默认选择数据库取出数据
    登录到 SQL Server 实例
    安装sql server 2008重启失败
    值栈
  • 原文地址:https://www.cnblogs.com/bclshuai/p/13856975.html
Copyright © 2011-2022 走看看