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 类型改变

提供以下函数来更改张量中的数值类型:
Tensorflow框架学习记录--day01--张量与变量

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

版权声明:本文为博主炮炮炮~~原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/qq_22658373/article/details/123393841

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2022年3月11日
下一篇 2022年3月11日

相关推荐