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

本文共 1756 字,大约阅读时间需要 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 os
      os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
      import tensorflow as tf
      zeros = 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 os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    import tensorflow as tf
    one = 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 + b
      print(c)
    • 矩阵运算: 使用高级矩阵函数,如矩阵乘法。
      import numpy as np
      matrix_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/

    你可能感兴趣的文章
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>