TensorFlow基础2(张量)

记录TensorFlow听课笔记



一,创建张量

在这里插入图片描述

1.1创建张量对象

在这里插入图片描述
numpy创建浮点数数组时,默认的浮点型是64位浮点数。 当使用NumPy数组创建张量时,TensorFlow会接受数组 元素的数据类型,使用64位浮点数保存数据。
在这里插入图片描述

import tensorflow as tf
matrix1 = tf.constant([[3, 3]])               #使用python列表创建张量
matrix2 = tf.constant([[2],[2]])
print("结果:")
print(matrix1)
print(matrix2)

1.2在创建张量时,指定元素的数据类型

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.3改变张量中元素的数据类型

在进行数据类型转换时,一般是将低精度的数据类型向高精度转换, 否则可能发生数据溢出,得到错误的结果。

import tensorflow as tf
import numpy as np
a=tf.constant(np.array([1,2]))
b=tf.cast(a,dtype=tf.float32)
print(b.dtype)

在这里插入图片描述

1.4将数组/列表/数字/布尔型/字符串变为张量

在这里插入图片描述

1.5判断是否为张量

在这里插入图片描述

import tensorflow as tf
import numpy as np
na=np.arange(12).reshape(3,4)
ta=tf.convert_to_tensor(na)
print(type(na))
print(type(ta))

1.6创建全0张量和全1/元素值都相同/随机数张量-正态分布/截断正态分布/均匀分布张量

创建全1和全0张量
tf.zeros(shape,dtype=tf.float32) tf.ones(shape,dtype=tf.float32)
创建元素值都相同的张量 tf.fill(dims=,value=) 可以用tf.constant(value=,shape=[])达到同样效果
创建随机数张量-正态分布tf.random.normal(shape,mean,stddev,dtype)
截断正态分布tf.random.truncated_normal(shape,mean,stddev,dtype) 不可能出现[-2,2]以外的点
创建均匀分布张量tf.random.uniform(shape,minval,maxval,dtype)
随机打乱函数tf.random.shuffle()
创建序列tf.range(start,limit,delta=1,dtype) 起始 终止 步长

import tensorflow as tf
import numpy as np
a=tf.zeros([2,2],dtype=tf.int32)
b=tf.ones([2,2],dtype=tf.int32)
c=tf.fill(dims=[2,2],value=9)
d=tf.constant(value=9,shape=[2,2])
e=tf.random.normal([2,2])
f=tf.random.normal([3,3,3],mean=0.0,stddev=2.0)
g=tf.random.uniform(shape=(3,3),minval=0,maxval=10,dtype='int32')
h=tf.constant([[1,2],[3,4],[5,6]])
i=tf.random.shuffle(x)                      #只按行打乱 在数据中只打乱样本顺序 而属性顺序不变
j=np.arange(5)
k=tf.random.shuffle(j)                      #打乱一维
l=tf.range(1,10,2)                          #1-9奇数数列

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二,Tensor对象的属性——ndim、shape、dtype

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三,维度变换

3.1改变张量的形状

import tensorflow as tf
import numpy as np
a=tf.range(24)
b=tf.reshape(a,[2,3,4])                       #一维变三维
c=tf.constant(np.arange(24).reshape(2,3,4))   #使用numpy然后转换成张量
d=tf.reshape(b,[4,-1])                        #-1自动推算第二维度的值

在这里插入图片描述
在这里插入图片描述
多维张量的轴:张量的维度
在这里插入图片描述

3.2张量增加维度,删除维度长度

import tensorflow as tf
import numpy as np
#二维
t=tf.constant([1,2])
print(t.shape)                                
t1=tf.expand_dims(t,1)
print(t1.shape)                               
t2=tf.expand_dims(t,0)                        #在零轴上增加维度
#三维
a=tf.ramge(24)
b=tf.reshape(a,[2,3,4])
print(b,shape)
b1=tf.expand_dims(b,0)
b2=tf.expand_dims(b,1)
b3=tf.expand_dims(b,2)
b4=tf.expand_dims(b,3)
print(b1,shape)
print(b2,shape)
print(b3,shape)
print(b4,shape)
tf.shape(tf.squeeze(b,[2,4]))

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.3交换维度

交换维度tf.transpose(a,perm) 转置
perm参数可以调换轴的位置也可以实现转置

import tensorflow as tf
import numpy as np
#二维
x=tf.constant([[1,2,3],[4,5,6]])
y1=tf.transpose(x)
y2=tf.transpose(x,perm=[1,0])      #与上一句相同 将2x3转换成3x2
#三维
a=tf.range(24)
b=tf.reshape(a,[2,3,4])
c=tf.transpose(b,(1,0,2))
print(b)
print(c)

在这里插入图片描述
在这里插入图片描述

3.4拼接和分割

拼接张量 tf.concat(tensors,axis) tensors是要拼接的张量表
张量分割 tf.split(value,num_or_size_splits,axis=0)

import tensorflow as tf
import numpy as np
t1=[[1,2,3],[4,5,6]]
t2=[[7,8,9],[10,11,12]]
a=tf.concat([t1,t2],0)                 #4x3
b=tf.concat([t1,t2],1)                 #2x6

在这里插入图片描述
在这里插入图片描述
value:要分割的张量
num_or_size_splits:分割方法
axis=0:轴

import tensorflow as tf
import numpy as np
x=tf.range(24)
x1=tf.reshape(x,[4,6])
x2=tf.split(x,2,0)                     #4x6 ->分割两份 按轴0方向 2x6 2x6
x3=tf.split(x,[1,2,1],0)               #4x6 ->1x6 2x6 1x6
x4=tf.split(x,[2,4],1)                 #4x6 ->4x2 4x4

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.5堆叠和分解

张量堆叠 tf.stack(values,axis)
分解张量tf.unstack( values, axis )
在这里插入图片描述
在这里插入图片描述

import tensorflow as tf
import numpy as np
x=tf.constant([1,2,3])
y=tf.constant([4,5,6])
z1=tf.stack((x,y),axis=0)               #2x3
z2=tf.stack((x,y),axis=1)              #3x2
#张量分解
o1=tf.unstack(z1,axis=0)
o2=tf.unstack(z2,axis=0)

四,部分采样

4.1索引和切片

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import tensorflow as tf
import numpy as np
#一维切片
a=tf.range(10)
b=a[::2]                    #0-9的偶数 从0开始9结束步长2
c=a[::-1]                   #逆序取出所有
d=a[::-2]                   #逆序步长为2

手写数字数据集MNIST(60000,28,28)
mnist[0,:,:] 第1张图片
mnist[0,:,:]为了更加简洁,两个冒号可以简写为一个冒号
mnist[0:10,:,:] 前10张图片
mnist[0:20,0:28:2,:] 前20张图片的所有行隔行采样
mnist[:,0:28:2,0:28:2] 所有图片隔行隔离列采样

4.2数据提取

数据提取:根据索引,抽取出没有规律的、特定的数据
gather(params,indices):用一个索引列表,将给定张量中,对应索引值的元素提出
params:输入张量
indices:索引列表

#一维
import tensorflow as tf
import numpy as np
a=tf.range(5)
b=tf.gather(a,indices=[0,2,3])        #将索引值为0 2 3 的元素
#多维采样gather(params,axis,indices) 指定轴 可实现对0->1->列采样 只能同时对一个维度进行索引
x=tf.range(24)
y=tf.reshape(a,[4,5])
z1=tf.gather(y,axis=0,indices=[0,2,3])  #在4x5中的023行进行提取采样
z2=tf.gather(y,axis=1,indices=[0,2,3])  #对列索引
#同时对多个维度索引 tf.gather_nd()
tf.gather_nd(lena,[[0,0],[1,1],[2,3]])
tf.gather_nd(mnist,[[0],[2],[3]])

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五,使用GPU

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值