博客
关于我
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/

    你可能感兴趣的文章
    Nodejs简介以及Windows上安装Nodejs
    查看>>
    nodejs系列之express
    查看>>
    nodejs配置express服务器,运行自动打开浏览器
    查看>>
    Node入门之创建第一个HelloNode
    查看>>
    Node出错导致运行崩溃的解决方案
    查看>>
    node安装及配置之windows版
    查看>>
    Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
    查看>>
    NOIp2005 过河
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>