栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Tensorflow框架学习记录--day01--张量与变量

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Tensorflow框架学习记录--day01--张量与变量

Tensorflow框架学习记录–day01–张量与变量 张量(Tensor)

tensorflow的张量就是一个n维数组,类型维tf.Tensor。
tensor具有以下两个重要的属性:

1. type:数据类型
2. shape:形状(阶)

张量在计算机中如何存储?
标量 一个数字 0阶张量
向量 一维数组 1阶张量
矩阵 二维数组 2阶张量

张量 n维数组 n阶张量

创建张量的时候,如果不指定类型
默认:tf.float32
整型 tf.int32
浮点型 tf.float32

创建张量的指令

固定值张量:

tf.zeros(shape,dtype=tf.float32,name=None)

tf.ones(shape,dtype=tf.float32,name=None)

tf.constant(value,dtype=None,shape=None,name='Const')

创建随机张量:
(从正态分布中输出随机值,由随机正态分布的数字组成矩阵)

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

其他特殊的创建张量的op:

tf.Variable

tf.placeholder
张量的变换 1 类型改变

提供了如下一些改变张量中数值类型的函数:

2 形状改变

tensorflow的张量具有两种形状变换,动态形状和静态形状

#动态
tf.reshape
#静态
tf.setshape

关于动态形状和静态形状必须符合以下规则:

1. 静态形状:
转换静态形状的时候,1-D到1-D,2-D到2-D,不能跨阶数改变形状。
对于已经固定的张量的静态形状的张量,不能再次设置静态形状。
2. 动态形状:
tf.reshape()动态创建新张量时,张量的元素个数必须匹配。

张量的数学运算

算数运算符
基本数学函数
矩阵运算
reduce操作
序列索引操作

详细请参考:
https://www.tensorflow.org/versions/r1.8/api_guides/python/math_ops
(这个api好像要翻墙才能使用)

下面po一些相关的代码:

def tensor_demo():
    """
    张量的演示
    :return:
    """
    tensor1 = tf.constant(4.0)
    tensor2 = tf.constant([1, 2, 3, 4])
    linear_squares = tf.constant([[4], [9], [16], [25]], dtype=tf.int32)

    print("tensor1:n", tensor1)
    print("tensor2:n", tensor2)
    print("linear_squares_before:n", linear_squares)

    # 张量类型的修改
    l_cast = tf.compat.v1.cast(linear_squares, dtype=tf.float32)
    print("linear_squares_after:n", linear_squares)
    print("l_cast:n", l_cast)

    # 更新/改变静态形状
    # 定义占位符
    # 没有完全固定下来的静态形状
    a_p = tf.compat.v1.placeholder(dtype=tf.float32, shape=[None, None])
    b_p = tf.compat.v1.placeholder(dtype=tf.float32, shape=[None, 10])
    c_p = tf.compat.v1.placeholder(dtype=tf.float32, shape=[3, 2])#三行二列
    print("a_p:n", a_p)
    print("b_p:n", b_p)
    print("c_p:n", c_p)

    # 更新形状未确定的部分
    a_p.set_shape([2, 3])
    b_p.set_shape([2, 10])
    print("a_p:n", a_p)
    print("b_p:n", b_p)

    # 动态形状修改
    a_p_reshape = tf.reshape(a_p, shape=[2, 3, 1])
    print("a_p_reshape:n", a_p_reshape)

    return None
变量OP

tensorflow变量是表示程序处理的共享持久状态的最佳方法,变量通过tf.Variable OP类进行操作,变量的特点:
存储持久化
可修改值
可指定被训练

创建变量

tf.Variable(initial_value=None,trainable=True,collections=None,name=None)
initial_value:初始化的值
trainable:是否被训练
collections:新变量将添加到列出的图的集合中collections,如果trainabel是True变量也被添加到图形集合GraphKeys.TRAINABLE_VARIABLES

变量需要显式初始化,才能运行值。

使用tf.variable_scope()修改变量的命名空间

会在OP的名字前面增加命名空间的指定名字。

以下是相关代码:

def variable_demo():
    """
    变量的演示
    """
    # 创建变量
    with tf.compat.v1.variable_scope("my_scope"):
        a = tf.Variable(initial_value=50)
        b = tf.Variable(initial_value=40)
    with tf.compat.v1.variable_scope("your_scope"):
        c = tf.add(a, b)

    print("a:n", a)
    print("b:n", b)
    print("c:n", c)

    # 初始化变量
    init = tf.compat.v1.global_variables_initializer()

    # 开启会话
    with tf.compat.v1.Session() as  sess:
        # 运行初始化
        sess.run(init)
        a_value, b_value, c_value = sess.run([a, b, c])
        print("a_value:n", a_value)
        print("b_value:n", b_value)
        print("c_value:n", c_value)

    return None
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号