博客
关于我
TensorFlow之张量
阅读量:789 次
发布时间:2019-03-24

本文共 1730 字,大约阅读时间需要 5 分钟。

TensorFlow之张量

1. 张量的基本概念

张量(Tensor)是TensorFlow中最基本的数据结构,类似于numpy中的数组。它是一个n维数组,数据类型为tf.Tensor。张量在TensorFlow中具有两个关键属性:【type和shape】。

  • type: 数据类型,如int32、float32等。
  • shape: 形状描述张量的维度,如 (3,4)表示3行4列的矩阵。

张量的阶数(rank)从0开始计算,如0阶为标量,1阶为1维数组(列表形式),2阶为矩阵形式等。

2. 张量的创建指令

创建张量可以分为几种类型:

  • 固定值张量(Constant Tensor):

    • 使用tf.constant函数创建常数值的张量。
    • 示例代码:
      import tensorflow as tf# 创建常数张量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(shape))
  • 零张量(Zero Tensor):

    • 使用tf.zeros函数创建全零值的张量。
    • 示例:
      import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'import tensorflow as tfzeros = tf.zeros([3, 5])zeros_like = tf.zeros_like(zeros)
  • 一张量(One Tensor):

    • 使用tf.ones函数创建全一值的张量。
    • 示例:
      one = tf.ones([3, 4])ones_like = tf.ones_like(zeros)
  • 随机值张量(Random Tensor):

    • 使用tf.random函数生成随机数。
    • 示例:
      random_tensor = tf.random_normal([3, 5])
  • 特殊张量创建操作( 其他操作):

    • tf.Variable: 创建可变张量,通常用于模型训练中的变量。
    • tf.placeholder: 用于定义模型的输入张量,具有动态形状。
  • 3. 张量的变换与操作

    3.1 类型改变

    通过使用tf.cast函数可以将张量的数据类型转换。例如,将浮点数张量转换为整数型:

    import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'import tensorflow as tfone = tf.ones([3, 4])cast_one = tf.cast(one, tf.int32)print(cast_one)

    3.2 形状改变

    张量可以进行静态形状或动态形状的改变。

    • 静态形状改变:使用tf.reshape函数进行张量形状的静态改变。这一步骤适用于已知形状的张量,且不改变其元素总数。
      tf.reshape(tensor, new_shape)
    • 动态形状改变:使用tf.reshape进行动态形状创建,适用于不确定大小的张量,但要确保张量元素总数不变。
      tf.reshape(tensor, new_shape)

    3.3 数学运算

    • 算术运算: 使用基本的加、减、乘、除操作。
      a = tf.constant(3.0)b = tf.constant(2.0)c = a + bprint(c)
    • 矩阵运算: 使用高级矩阵函数,如矩阵乘法。
      import numpy as npmatrix_a = np.array([[1, 2], [3, 4]])matrix_b = np.array([[5, 6], [7, 8]])tf.matmul(matrix_a, matrix_b)
    • reduce操作: 将张量按某一轴进行聚合。
      a = tf.constant([1, 2, 3, 4])b = tf.reduce_sum(a)print(b)
    • 序列操作: 如分割、重复操作等。

    这些操作为张量提供了丰富的运算能力,使其在深度学习和数据处理中极为灵活。

    转载地址:http://bnekk.baihongyu.com/

    你可能感兴趣的文章
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>
    ORM sqlachemy学习
    查看>>
    orm总结
    查看>>
    os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
    查看>>
    os.system 在 Python 中不起作用
    查看>>
    OSCACHE介绍
    查看>>
    SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
    查看>>
    OSChina 周五乱弹 ——吹牛扯淡的耽误你们学习进步了
    查看>>
    OSChina 周四乱弹 ——程序员为啥要买苹果手机啊?
    查看>>
    OSError: no library called “cairo-2“ was foundno library called “cairo“ was foundno library called
    查看>>
    OSG学习:几何体的操作(二)——交互事件、Delaunay三角网绘制
    查看>>
    OSG学习:几何对象的绘制(三)——几何元素的存储和几何体的绘制方法
    查看>>