tensorflow2.0---笔记1 tensor基本操作

这篇博客介绍了TensorFlow 2.0中张量(tensor)的基本操作,包括创建、类型转换、索引与切片、维度变换和数学运算。内容涵盖tf.constant、tf.Variable、tf.reshape、广播机制、矩阵乘法以及element-wise和dim-wise运算。此外,还讨论了tensor到numpy数组的转换和随机数生成。
摘要由CSDN通过智能技术生成

什么是tensor

常见的数据载体(data container):

  1. list(可存放不同类型数据);
  2. np.array(科学计算库,数值运算,但不支持自动求导);
  3. tf.Tensor(也可视为一个与numpy类似的科学计算库)。
    TensorFlow使用张量(Tensor)作为数据的基本单位。tensor在概念上类似于多维数组,可以用它来描述数学中的标量(0维数组)、向量(一维数组)、矩阵(2维数组)等各种量。

tensor的属性

张量的三个重要属性是其形状(存放的数据)类型。可以通过shapedtype属性和 numpy() 方法获得。

属性/方法 描述
a.shape
a.dtype 存放的数据类型
a.numpy() 获取a中存放的值
a.ndim 获得a的维度

tf.is_tensor(a) 可以判断a是否为tensor(返回布尔型)。

创建tensor

  1. 用 tf.constant()、tf.Variable() 创建tensor
a = tf.constant(10) # 标量
print(a)  # tf.Tensor(10, shape=(), dtype=int32)

b = tf.constant([2.], dtype=tf.double)  # 加上中括号就为一维向量
print(b)  # tf.Tensor([2.], shape=(1,), dtype=float64)
print(b.dtype)  # <dtype: 'float64'>

c = tf.constant([True, False])
print(c)  # tf.Tensor([ True False], shape=(2,), dtype=bool)

d = tf.constant("hello world")
print(d)  # tf.Tensor(b'hello world', shape=(), dtype=string)

tf.Variable专为神经网络中可训练的参数设置(有trainable属性且为True)

a = tf.Variable(initial_value=1)
print(a)  # <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=1>
print(a.dtype)  # <dtype: 'int32'>
print(a.numpy())  # 1

b = tf.range(5)
print(b.trainable)  # 报错,无此属性 AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'trainable'
c = tf.Variable(a)  
print(c)  # <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=1>
print(c.trainable)  # True
  1. tf.range()、tf.linspace(start, end, number)
a = tf.range(5)
print(a)  # tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int32)
print(a.numpy())  # [0 1 2 3 4]
  1. 使用tf.convert_to_tensot() 将numpy或list转换为tensor
import tensorflow as tf
import numpy as np

a = np.arange(5)
b = tf.convert_to_tensor(a) 

c = tf.convert_to_tensor(a, dtype=tf.int32) 

print(b.dtype)  # <dtype: 'int32'>
print(c.dtype)  # <dtype: 'int32'>
import tensorflow as tf

a = tf.convert_to_tensor([1, 5, 8])
print(a)  # tf.Tensor([1 5 8], shape=(3,), dtype=int32)
  1. tf.zeros()tf.zeros_like()tf.ones()tf.ones_like()
    注:tf.zeros()、tf.ones() 括号中接收的是数组形状
a = tf.zeros([])
print(a.shape)  # ()  即,创建了一个标量

b = tf.zeros([1])
print(b.shape)  # (1,)  创建一维向量

c = tf.zeros([3, 6]) 
print(c.shape)
a = tf.range(5)
b = tf.zeros_like(a)
print(b.numpy())  # [0 0 0 0 0]
  1. tf.fill() 创建值相同的tensor
a = tf.fill([4, 6], 8)
print(a.numpy())

在这里插入图片描述
6. 创建正态分布、均匀分布随机初始化的tensor
(1)正态分布

a = tf.random.normal([2, 3], mean=0, stddev=2)  # stddev为标准差
print(a)

在这里插入图片描述
截断正态分布,避免数据落在sigmoid函数两端导致的梯度消失问题(如参数初始化时)
在这里插入图片描述

a = tf.random.truncated_normal([2, 6], mean=0, stddev=2)
print(a)

函数注释:The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.
大于两倍标准差的数值被舍弃。
(2)均匀分布

a = tf.random.uniform([3, 6], minval=1, maxval=6)  # 还可指定dtype
print(a)   

在这里插入图片描述
7. Random Permutation

ind = tf.range(10)
ind = tf.random.shuffle(ind)

a = tf.random.normal([10, 784])
b = tf.random.uniform([10], maxval=10, dtype=tf.int32)

a = tf.gather(a, ind)
b = tf.gather(b, ind)
创建方法 说明
tf.range(n) 创建tensor,值为从0~n-1的向量
tf.constant() 创建常量tensor
tf.Variable() 创建变量tensor,可训练(trainable属性为True)
tf.convert_to_tensor(numpy数组或list) 将numpy数组或list转换为tensor
tf.zeros( [shape] )、tf.ones( [shape] ) 创建形状为shape的全为0/1的tensor
tf.zeros_like(b)、tf.ones_like(b) 创建与b形状相同的全为0/1的tensor
tf.fill([shape], num) 创建一个值全为num的shape形tensor
tf.random.normal( [shape], mean=, steddv = ) 正态分布
tf.random.truncated_normal([shape], mean=, steddv = ) 截断正态分布
tf.random.uniform([shape],minval=, maxvla =, dtype = ) 均匀分布

tensor数据类型转换

tf.int、tf.double、tf.float 之间相互转换( tf.cast() )

a = np.arange(5)
b = tf.convert_to_tensor(a, dtype=tf.int32)

c = tf.cast(b, tf.float32)
print(c.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值