张量学习

张量(Tensor)学习
张量基本概念:表示多维数组(列表)阶数:张量的维数
维数0: 标量Scalar s=1 2 3 shape=0
维数1: 向量Vector v=[1,2,3] shape=(3)
维数2: 矩阵Matrix m=[[1,2,3],[4,5,6],[7,8,9]] shape=(3,3)
维数3: 张量Tensor t=[[[1,1],[1,1]],[[2,2],[2,2]]] shape=(2,2,2)
维数n:[[[…]]]n个
张量数据类型
tf.int tf.float…
如 tf.int32 tf.float64
tf.bool
如tf.constant([True,False])
tf.string
如 tf.constant(“hello python”)
张量的属性
dtype:查看张量的数据类型
shape:查看张量的维度

a=tf.constant([4,4],dtype=tf.int64)
print(a.shape,a.dtype)
>>>(2,) <dtype: 'int64'>

创建张量tensor
tf.constant(张量内容,dtype=指定类型)

import tensorflow as tf
a=tf.constant([4,4],dtype=tf.int64)
print(a)
>>>tf.Tensor([4 4], shape=(2,), dtype=int64)
>

特殊张量
a=tf.ones(维度)
b=tf.zeros(维度)
c=tf.fill(维度,指定数字)

import tensorflow as tf
a=tf.ones(4)
b=tf.zeros((3,2))
c=tf.fill((1,2,2),4)
print(a,b,c)
>>>tf.Tensor([1. 1. 1. 1.], shape=(4,), dtype=float32) tf.Tensor(
[[0. 0.]
[0. 0.]
[0. 0.]], shape=(3, 2), dtype=float32) tf.Tensor(
[[[4 4]
  [4 4]]], shape=(1, 2, 2), dtype=int32)

axis轴概念
在这里插入图片描述
在二维张量中以axis=0为竖轴(对行操作),axis=1横轴(对列操作)
如不指定axis则所有元素参加运算
tf.cast(张量名,dtype=数据类型)将张量强制转换为指定类型
tf.reduce_mim(张量名)求最大值
tf.reduce_max(张量名)求最小值
tf.reduce_mean(张量名,axis=操作轴)求平均数
tf.reduce_sum(张量名,axis=操作轴)如果指定axis其结果为一维张量存放各轴的和
tf.constant()常量
tf.Variable()变量,把数据标记为可训练,用于代训练参数
如tf.Variable(tf.random_normal((2,2),stddev=1,mean=0)创建了一个二行二列的标准正态分布
Tensor运算
四则运算
tf.add() tf.subtract() tf.multiply() tf.didvide()
平方,次方,开方
tf.square() tf.pow() tf.sqrt()
矩阵乘法
tf.matmul()
Tensor数据
tf.data.Dataset.from_tensor_slices(特征x,标签y)
Numpy与Tensorflow格式都适用
分布类
正态分布:tf.random.normal(维度,均值,标准差)
tf.random.truncated_normal((2,2),mean=0,stddev=1)#数据向0更加集中,控制在2倍标准差内,生成均值为0,标准差为1的标准正态分布

均匀分布随机数[min,max) 这里是左开右闭
tf.random.uniform([8],minval=0,maxval=1)
Tensor数据
tf.data.Dataset.from_tensor_slices(特征x,标签y)
将特征与标签一一匹配
Numpy与Tensorflow格式都适用

import tensorflow as tf
features=tf.constant([12,23,10,17])#创建特征x
labels=tf.constant([0,1,1,0])#创建标签y
dataset=tf.data.Dataset.from_tensor_slices((features,labels))
print(dataset)
for element in dataset:
    print(element)
>>>
<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>
(<tf.Tensor: id=9, shape=(), dtype=int32, numpy=12>, <tf.Tensor: id=10, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=11, shape=(), dtype=int32, numpy=23>, <tf.Tensor: id=12, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=13, shape=(), dtype=int32, numpy=10>, <tf.Tensor: id=14, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=15, shape=(), dtype=int32, numpy=17>, <tf.Tensor: id=16, shape=(), dtype=int32, numpy=0>)

Numpy转换为Tensor类型

import tensorflow as tf
import numpy as np
a=np.arange(0,5)
b=tf.convert_to_tensor(a,dtype=tf.int64)
print(b)
>>>tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值