zoukankan      html  css  js  c++  java
  • TensorFlow — 相关 API

    TensorFlow — 相关 API

    TensorFlow 相关函数理解

    任务时间:时间未知

    tf.truncated_normal

    truncated_normal(
        shape,
        mean=0.0,
        stddev=1.0,
        dtype=tf.float32,
        seed=None,
        name=None
    )
    

    功能说明:

    产生截断正态分布随机数,取值范围为[mean - 2 * stddev, mean + 2 * stddev]

    参数列表:

    参数名必选类型说明
    shape 1 维整形张量或array 输出张量的维度
    mean 0 维张量或数值 均值
    stddev 0 维张量或数值 标准差
    dtype dtype 输出类型
    seed 数值 随机种子,若 seed 赋值,每次产生相同随机数
    name string 运算名称

    示例代码:

    现在您可以在 /home/ubuntu 目录下创建源文件 truncated_normal.py

    示例代码:/home/ubuntu/truncated_normal.py
    #!/usr/bin/python
    
    import tensorflow as tf
    initial = tf.truncated_normal(shape=[3,3], mean=0, stddev=1)
    print tf.Session().run(initial)
    

    然后执行:

    python /home/ubuntu/truncated_normal.py
    

    执行结果:

    将得到一个取值范围 [-2, 2] 的 3 * 3 矩阵,您也可以尝试修改源代码看看输出结果有什么变化?

    tf.constant

    constant(
        value,
        dtype=None,
        shape=None,
        name='Const',
        verify_shape=False
    )
    

    功能说明:

    根据 value 的值生成一个 shape 维度的常量张量

    参数列表:

    参数名必选类型说明
    value 常量数值或者 list 输出张量的值
    dtype dtype 输出张量元素类型
    shape 1 维整形张量或 array 输出张量的维度
    name string 张量名称
    verify_shape Boolean 检测 shape 是否和 value 的 shape 一致,若为 Fasle,不一致时,会用最后一个元素将 shape 补全

    示例代码:

    现在您可以在 /home/ubuntu 目录下创建源文件 constant.py,内容可参考:

    示例代码:/home/ubuntu/constant.py
    #!/usr/bin/python
    
    import tensorflow as tf
    import numpy as np
    a = tf.constant([1,2,3,4,5,6],shape=[2,3])
    b = tf.constant(-1,shape=[3,2])
    c = tf.matmul(a,b)
    
    e = tf.constant(np.arange(1,13,dtype=np.int32),shape=[2,2,3])
    f = tf.constant(np.arange(13,25,dtype=np.int32),shape=[2,3,2])
    g = tf.matmul(e,f)
    with tf.Session() as sess:
        print sess.run(a)
        print ("##################################")
        print sess.run(b)
        print ("##################################")
        print sess.run(c)
        print ("##################################")
        print sess.run(e)
        print ("##################################")
        print sess.run(f)
        print ("##################################")
        print sess.run(g)
    

    然后执行:

    python /home/ubuntu/constant.py
    

    执行结果:

    • a: 2x3 维张量;
    • b: 3x2 维张量;
    • c: 2x2 维张量;
    • e: 2x2x3 维张量;
    • f: 2x3x2 维张量;
    • g: 2x2x2 维张量。

    您也可以尝试修改源代码看看输出结果有什么变化?

    tf.placeholder

    placeholder(
        dtype,
        shape=None,
        name=None
    )
    

    功能说明:

    是一种占位符,在执行时候需要为其提供数据

    参数列表:

    参数名必选类型说明
    dtype dtype 占位符数据类型
    shape 1 维整形张量或 array 占位符维度
    name string 占位符名称

    示例代码:

    现在您可以在 /home/ubuntu 目录下创建源文件 placeholder.py,内容可参考:

    示例代码:/home/ubuntu/placeholder.py
    #!/usr/bin/python
    
    import tensorflow as tf
    import numpy as np
    
    x = tf.placeholder(tf.float32,[None,10])
    y = tf.matmul(x,x)
    with tf.Session() as sess:
        rand_array = np.random.rand(10,10)
        print sess.run(y,feed_dict={x:rand_array})
    

    然后执行:

    python /home/ubuntu/placeholder.py
    

    执行结果:

    输出一个 10x10 维的张量。您也可以尝试修改源代码看看输出结果有什么变化?

    tf.nn.bias_add

    bias_add(
        value,
        bias,
        data_format=None,
        name=None
    )
    

    功能说明:

    将偏差项 bias 加到 value 上面,可以看做是 tf.add 的一个特例,其中 bias 必须是一维的,并且维度和 value 的最后一维相同,数据类型必须和 value 相同。

    参数列表:

    参数名必选类型说明
    value 张量 数据类型为 float, double, int64, int32, uint8, int16, int8, complex64, or complex128
    bias 1 维张量 维度必须和 value 最后一维维度相等
    data_format string 数据格式,支持'NHWC'和'NCHW'
    name string 运算名称

    示例代码:

    现在您可以在 /home/ubuntu 目录下创建源文件 bias_add.py,内容可参考:

    示例代码:/home/ubuntu/bias_add.py
    #!/usr/bin/python
    
    import tensorflow as tf
    import numpy as np
    
    a = tf.constant([[1.0, 2.0],[1.0, 2.0],[1.0, 2.0]])
    b = tf.constant([2.0,1.0])
    c = tf.constant([1.0])
    sess = tf.Session()
    print sess.run(tf.nn.bias_add(a, b)) 
    #print sess.run(tf.nn.bias_add(a,c)) error
    print ("##################################")
    print sess.run(tf.add(a, b))
    print ("##################################")
    print sess.run(tf.add(a, c))
    

    然后执行:

    python /home/ubuntu/bias_add.py
    

    执行结果:

    3 个 3x2 维张量。您也可以尝试修改源代码看看输出结果有什么变化?

    tf.reduce_mean

    reduce_mean(
        input_tensor,
        axis=None,
        keep_dims=False,
        name=None,
        reduction_indices=None
    )
    

    功能说明:

    计算张量 input_tensor 平均值

    参数列表:

    参数名必选类型说明
    input_tensor 张量 输入待求平均值的张量
    axis None、0、1 None:全局求平均值;0:求每一列平均值;1:求每一行平均值
    keep_dims Boolean 保留原来的维度,降为 1
    name string 运算名称
    reduction_indices None 和axis等价,被弃用

    示例代码:

    现在您可以在 /home/ubuntu 目录下创建源文件 reduce_mean.py,内容可参考:

    示例代码:/home/ubuntu/reduce_mean.py
    #!/usr/bin/python
    
    import tensorflow as tf
    import numpy as np
    
    initial = [[1.,1.],[2.,2.]]
    x = tf.Variable(initial,dtype=tf.float32)
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_op)
        print sess.run(tf.reduce_mean(x))
        print sess.run(tf.reduce_mean(x,0)) #Column
        print sess.run(tf.reduce_mean(x,1)) #row
    

    然后执行:

    python /home/ubuntu/reduce_mean.py
    

    执行结果:

    1.5
    [ 1.5  1.5]
    [ 1.  2.]
    

    您也可以尝试修改源代码看看输出结果有什么变化?

    tf.squared_difference

    squared_difference(
        x,
        y,
        name=None
    )
    

    功能说明:

    计算张量 x、y 对应元素差平方

    参数列表:

    参数名必选类型说明
    x 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
    y 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
    name string 运算名称

    示例代码:

    现在您可以在 /home/ubuntu 目录下创建源文件 squared_difference.py,内容可参考:

    示例代码:/home/ubuntu/squared_difference.py
    #!/usr/bin/python
    
    import tensorflow as tf
    import numpy as np
    
    initial_x = [[1.,1.],[2.,2.]]
    x = tf.Variable(initial_x,dtype=tf.float32)
    initial_y = [[3.,3.],[4.,4.]]
    y = tf.Variable(initial_y,dtype=tf.float32)
    diff = tf.squared_difference(x,y)
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_op)
        print sess.run(diff)
    

    然后执行:

    python /home/ubuntu/squared_difference.py
    

    执行结果:

    [[ 4.  4.]
     [ 4.  4.]]
    

    您也可以尝试修改源代码看看输出结果有什么变化?

    tf.square

    square(
        x,
        name=None
    )
    

    功能说明:

    计算张量对应元素平方

    参数列表:

    参数名必选类型说明
    x 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
    name string 运算名称

    示例代码:

    现在您可以在 /home/ubuntu 目录下创建源文件 square.py,内容可参考:

    示例代码:/home/ubuntu/square.py
    #!/usr/bin/python
    import tensorflow as tf
    import numpy as np
    
    initial_x = [[1.,1.],[2.,2.]]
    x = tf.Variable(initial_x,dtype=tf.float32)
    x2 = tf.square(x)
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_op)
        print sess.run(x2)
    

    然后执行:

    python /home/ubuntu/square.py
    

    执行结果:

    [[ 1.  1.]
     [ 4.  4.]]
    

    您也可以尝试修改源代码看看输出结果有什么变化?

    TensorFlow 相关类理解

    任务时间:时间未知

    tf.Variable

    __init__(
        initial_value=None,
        trainable=True,
        collections=None,
        validate_shape=True,
        caching_device=None,
        name=None,
        variable_def=None,
        dtype=None,
        expected_shape=None,
        import_scope=None
    )
    

    功能说明:

    维护图在执行过程中的状态信息,例如神经网络权重值的变化。

    参数列表:

    参数名类型说明
    initial_value 张量 Variable 类的初始值,这个变量必须指定 shape 信息,否则后面 validate_shape 需设为 False
    trainable Boolean 是否把变量添加到 collection GraphKeys.TRAINABLE_VARIABLES 中(collection 是一种全局存储,不受变量名生存空间影响,一处保存,到处可取)
    collections Graph collections 全局存储,默认是 GraphKeys.GLOBAL_VARIABLES
    validate_shape Boolean 是否允许被未知维度的 initial_value 初始化
    caching_device string 指明哪个 device 用来缓存变量
    name string 变量名
    dtype dtype 如果被设置,初始化的值就会按照这个类型初始化
    expected_shape TensorShape 要是设置了,那么初始的值会是这种维度

    示例代码:

    现在您可以在 /home/ubuntu 目录下创建源文件 Variable.py,内容可参考:

    示例代码:/home/ubuntu/Variable.py
    #!/usr/bin/python
    
    import tensorflow as tf
    initial = tf.truncated_normal(shape=[10,10],mean=0,stddev=1)
    W=tf.Variable(initial)
    list = [[1.,1.],[2.,2.]]
    X = tf.Variable(list,dtype=tf.float32)
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_op)
        print ("##################(1)################")
        print sess.run(W)
        print ("##################(2)################")
        print sess.run(W[:2,:2])
        op = W[:2,:2].assign(22.*tf.ones((2,2)))
        print ("###################(3)###############")
        print sess.run(op)
        print ("###################(4)###############")
        print (W.eval(sess)) #computes and returns the value of this variable
        print ("####################(5)##############")
        print (W.eval())  #Usage with the default session
        print ("#####################(6)#############")
        print W.dtype
        print sess.run(W.initial_value)
        print sess.run(W.op)
        print W.shape
        print ("###################(7)###############")
        print sess.run(X)
    

    然后执行:

    python /home/ubuntu/Variable.py
    

    完成

    任务时间:时间未知

    恭喜,您已完成本实验内容

    您可进行更多 TensorFlow 的系列教程:

    • TensorFlow - 线性回归
    • TensorFlow - 基于 CNN 数字识别

    关于 TensorFlow 的更多资料可参考 TensorFlow 官网

  • 相关阅读:
    取得窗口大小和窗口位置兼容所有浏览器的js代码
    一个简单易用的导出Excel类
    如何快速启动chrome插件
    网页表单设计案例
    Ubuntu下的打包解包
    The source file is different from when the module was built. Would you like the debugger to use it anyway?
    FFisher分布
    kalman filter
    Group delay Matlab simulate
    24位位图格式解析
  • 原文地址:https://www.cnblogs.com/feige1314/p/7563458.html
Copyright © 2011-2022 走看看