人工智能基础-tensorflow篇

本文介绍了Tensorflow中创建tensor的不同方法,如constant、numpy转换、zeros、ones等,并详细讲解了查看tensor属性、进行元素级运算、矩阵乘法以及求平均、最大值等统计运算的操作。
摘要由CSDN通过智能技术生成

tensorflow基础篇 一

1. 创建tensor
  • 使用constant创建

    a = tf.constant(3.9)
    b = tf.constant([1, 2],[3, 4])
    c = tf.constant("Hello world!")
    
  • 使用numpy或list创建

    a = tf.convert_to_tensor(np.ones([3, 5])
    b = tf.convert_to_tensor([1, 2, 3, 4, 5])
    
  • 使用zeros、ones和fill等创建

    a = tf.zeros([2, 3])
    b = tf.ones([]4, 5)
    c = tf.fill([2, 3, 4], 12)
    
  • 使用随机数初始化

    a = tf.random.normal([3, 3], mean=0, stddev=1)
    b = tf.random.uniform([2, 2], minval=0, maxval=1)
    
2. 关于tensor的一些属性
  • 查看tensor的数据类型

    a.numpy
    
  • 查看tensor的维度

    a.ndim
    
  • 查看tensor的形状

    a.shape
    
  • 查看是否为tensor

    tf.is_tensor(a)
    
3. tensor的运算
  • 读取元素、切片

    a[2][3][1]
    a[:, :, :, -2:]
    a[1, ...]
    
  • 维度变换

    a = tf.reshape(b, [2, 16])
    
  • 数学计算

    # 逐元素运算
    a + b
    a - b
    a * b
    a \ b
    a % b
    a \\ b
    a ** 3
    a = tf.pow(b)
    a = tf.square(b)
    a = tf.sqrt(b)
    a = tf.exp(b)
    a = tf.math.log(b)
    
    # 矩阵乘
    a @ b
    tf.matmul(a, b)
    
    # 求平均、最大、最小和求和
    tf.reduce_mean(a)
    tf.reduce_min(a)
    tf.reduce_max(a)
    tf.reduce_sum(a)
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值