创建Tensor
创建Tensor
* from numpy, list
* zeros, ones, fill
* random # if big dimension, random initial
* constant
* Application
numpy, list
numpy
import numpy as np
import tensorflow as tf
tf.convert_to_tensor(np.ones([2, 3]))
<tf.Tensor: id=0, shape=(2, 3), dtype=float64, numpy=
array([[1., 1., 1.],
[1., 1., 1.]])>
tf.convert_to_tensor(np.zeros([2, 3]))
<tf.Tensor: id=2, shape=(2, 3), dtype=float64, numpy=
array([[0., 0., 0.],
[0., 0., 0.]])>
list
tf.convert_to_tensor([1, 2])
<tf.Tensor: id=4, shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>
tf.convert_to_tensor([1, 2.])
<tf.Tensor: id=6, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>
tf.convert_to_tensor([[1], [2.]])
<tf.Tensor: id=8, shape=(2, 1), dtype=float32, numpy=
array([[1.],
[2.]], dtype=float32)>
zeros, ones, fill
zeros
tf.zeros([])
<tf.Tensor: id=10, shape=(), dtype=float32, numpy=0.0>
tf.zeros([1])
<tf.Tensor: id=14, shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>
tf.zeros([2, 2])
<tf.Tensor: id=18, shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
[0., 0.]], dtype=float32)>
tf.zeros([2, 3, 3])
<tf.Tensor: id=22, shape=(2, 3, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]], dtype=float32)>
a = tf.constant([0])
tf.zeros_like(a) # 等同于tf.zeros(a.shape)
<tf.Tensor: id=25, shape=(1,), dtype=int32, numpy=array([0], dtype=int32)>
ones
tf.ones(1)
<tf.Tensor: id=29, shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>
tf.ones([])
<tf.Tensor: id=31, shape=(), dtype=float32, numpy=1.0>
tf.ones([2])
<tf.Tensor: id=35, shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>
tf.ones([2, 3])
<tf.Tensor: id=39, shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>
a = tf.constant([0])
tf.ones_like(a) # # 等同于tf.ones(a.shape)
<tf.Tensor: id=44, shape=(1,), dtype=int32, numpy=array([1], dtype=int32)>
fill
tf.fill([2, 2], 0)
<tf.Tensor: id=48, shape=(2, 2), dtype=int32, numpy=
array([[0, 0],
[0, 0]], dtype=int32)>
tf.fill([2, 2], 0)
<tf.Tensor: id=52, shape=(2, 2), dtype=int32, numpy=
array([[0, 0],
[0, 0]], dtype=int32)>
tf.fill([2, 2], 1)
<tf.Tensor: id=56, shape=(2, 2), dtype=int32, numpy=
array([[1, 1],
[1, 1]], dtype=int32)>
tf.fill([2, 2], 9)
<tf.Tensor: id=60, shape=(2, 2), dtype=int32, numpy=
array([[9, 9],
[9, 9]], dtype=int32)>
random
# 正态分布,均值为1,方差为1
tf.random.normal([2, 2], mean=1, stddev=1)
<tf.Tensor: id=67, shape=(2, 2), dtype=float32, numpy=
array([[1.0804566, 0.9318387],
[1.0620257, 0.6907253]], dtype=float32)>
tf.random.normal([2, 2])
<tf.Tensor: id=74, shape=(2, 2), dtype=float32, numpy=
array([[-0.06452972, 0.05704789],
[ 0.82857376, 0.71619517]], dtype=float32)>
# 截断的正态分布,
tf.random.truncated_normal([2, 2], mean=0, stddev=1)
<tf.Tensor: id=81, shape=(2, 2), dtype=float32, numpy=
array([[ 0.19161457, -0.820383 ],
[ 0.43668088, -0.3798696 ]], dtype=float32)>
如下图所示为截断正态分布,截掉红色部分,对新的正态分布重新采样。因为sigmoid的和新的正态分布不冲突的地方的区域,对于sigmoid函数来说是近似于平滑的直线,梯度为0,因此会有梯度消失。
# 均匀分布
tf.random.uniform([2, 2], minval=0, maxval=1)
<tf.Tensor: id=89, shape=(2, 2), dtype=float32, numpy=
array([[0.01481438, 0.15071952],
[0.5599004 , 0.59821343]], dtype=float32)>
tf.random.uniform([2, 2], minval=0, maxval=100, dtype=tf.int32)
<tf.Tensor: id=102, shape=(2, 2), dtype=int32, numpy=
array([[51, 9],
[10, 14]], dtype=int32)>
打乱idx后,a和b的索引不变
idx = tf.range(10)
idx = tf.random.shuffle(idx)
idx
<tf.Tensor: id=113, shape=(10,), dtype=int32, numpy=array([0, 8, 4, 9, 6, 7, 5, 2, 1, 3], dtype=int32)>
a = tf.random.normal([10, 784])
b = tf.random.uniform([10], maxval=10, dtype=tf.int32)
b
<tf.Tensor: id=134, shape=(10,), dtype=int32, numpy=array([1, 8, 1, 2, 4, 6, 2, 7, 4, 5], dtype=int32)>
a = tf.gather(a, idx)
b = tf.gather(b, idx)
b
<tf.Tensor: id=147, shape=(10,), dtype=int32, numpy=array([1, 8, 2, 2, 6, 1, 7, 4, 4, 5], dtype=int32)>
constant
tf.constant(1)
<tf.Tensor: id=149, shape=(), dtype=int32, numpy=1>
tf.constant([1])
<tf.Tensor: id=151, shape=(1,), dtype=int32, numpy=array([1], dtype=int32)>
tf.constant([1, 2.])
<tf.Tensor: id=153, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>
tf.constant([[1, 2], [3., 2]])
<tf.Tensor: id=156, shape=(2, 2), dtype=float32, numpy=
array([[1., 2.],
[3., 2.]], dtype=float32)>
loss计算
无bias的loss
out = tf.random.uniform([4, 10])
out
<tf.Tensor: id=171, shape=(4, 10), dtype=float32, numpy=
array([[0.67733276, 0.2267617 , 0.21761227, 0.28679788, 0.68864655,
0.21349418, 0.5646602 , 0.8294822 , 0.22094071, 0.20246148],
[0.7940483 , 0.86402774, 0.78399694, 0.80085063, 0.01357341,
0.11889946, 0.89162886, 0.755934 , 0.8058628 , 0.40188062],
[0.115659 , 0.30951428, 0.39866602, 0.5358803 , 0.9163326 ,
0.47557557, 0.9397205 , 0.3110628 , 0.49839914, 0.34321547],
[0.5563061 , 0.78829396, 0.52705276, 0.29077685, 0.35033226,
0.9630101 , 0.338771 , 0.6301584 , 0.7393383 , 0.7073529 ]],
dtype=float32)>
y = tf.range(4)
y = tf.one_hot(y, depth=10)
y
<tf.Tensor: id=188, shape=(4, 10), dtype=float32, numpy=
array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]], dtype=float32)>
loss = tf.keras.losses.mse(y, out)
loss
<tf.Tensor: id=195, shape=(4,), dtype=float32, numpy=array([0.19016443, 0.4096697 , 0.31698173, 0.43206215], dtype=float32)>
loss = tf.reduce_mean(loss)
loss
<tf.Tensor: id=200, shape=(), dtype=float32, numpy=0.3372195>
创建Tensor的更多相关文章
- pytorch——不用包模拟简单线性预测,数据类型,创建tensor,索引与切片
常见的学习种类 线性回归,最简单的y=wx+b型的,就像是调节音量大小.逻辑回归,是否问题.分类问题,是猫是狗是猪 最简单的线性回归y=wx+b 目的:给定大量的(x,y)坐标点,通过机器学习来找出最 ...
- pytorch 创建tensor的几种方法
tensor默认是不求梯度的,对应的requires_grad是False. 1.指定数值初始化 import torch #创建一个tensor,其中shape为[2] tensor=torch.T ...
- 吴裕雄--天生自然TensorFlow2教程:创建Tensor
import numpy as np import tensorflow as tf tf.convert_to_tensor(np.ones([2, 3])) tf.convert_to_tenso ...
- pytorch 中的数据类型,tensor的创建
pytorch中的数据类型 import torch a=torch.randn(2,3) b=a.type() print(b) #检验是否是该数据类型 print(isinstance(a,tor ...
- pytorch(02)tensor的概念以及创建
二.张量的简介与创建 2.1张量的概念 张量的概念:Tensor 张量是一个多维数组,它是标量.向量.矩阵的高维拓展 Tensor与Variable Variable是torch.autograd(t ...
- Tensor基本操作
Tensor(张量) 1.Tensor,又名张量,从工程角度来说,可简单地认为它就是一个数组,且支持高效的科学计算.它可以是一个数(标量).一维数组(向量).二维数组(矩阵)或更高维的数组(高阶数组) ...
- 深度学习框架PyTorch一书的学习-第三章-Tensor和autograd-1-Tensor
参考https://github.com/chenyuntc/pytorch-book/tree/v1.0 希望大家直接到上面的网址去查看代码,下面是本人的笔记 Tensor Tensor可以是一个数 ...
- 『PyTorch』第五弹_深入理解Tensor对象_上:初始化以及尺寸调整
一.创建Tensor 特殊方法: t.arange(1,6,2)t.linspace(1,10,3)t.randn(2,3) # 标准分布,*size t.randperm(5) # 随机排序,从0到 ...
- Pytorch Tensor 常用操作
https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor, ...
随机推荐
- bzoj 4078: [Wf2014]Metal Processing Plant【二分+2-SAT+枚举+并查集】
枚举从大到小s1,二分s2(越大越有可能符合),2-SAT判断,ans取min 思路倒是挺简单的,就是二分的时候出了比较诡异的问题,只能二分s2的值,不能在数组上二分... 有个优化,就是当不是二分图 ...
- AutoCAD2012启动错误 1308 源文件未找到
启动AutoCAD2012时,弹出错误1308,如图所示: 是何原因? 对CAD的运行有何影响?
- 51nod 1874 字符串排序
1874 字符串排序 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 定义一个字符串的无序度为所有位置后面的字母比该位置的字母小的总数之和.比如&q ...
- ES6的新方法实现数组去重
ES6里新添加了两个很好用的东西,set和Array.from. set是一种新的数据结构,它可以接收一个数组或者是类数组对象,自动去重其中的重复项目. 在这我们可以看见,重复的项目已经被去掉了,包括 ...
- 【先定一个小目标】Postgresql允许远程访问配置修改
1.解决不能连接远程postgresql: postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下: 1.postgresql.conf 将该文件中的l ...
- Codeforces Round #230 (Div. 1)
A: 题意:给你一个半径为n的圆 求最少阻塞多少个点 才能使所以圆内及圆上的点 都不与外边的点相连 相连是距离为1 只算整数点 这题定住x,y依次递减 判断一下是否4-connect 这个意思就是 ...
- 给定一个整数 n,返回 n! 结果尾数中零的数量。
示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120, 尾数中有 1 个零. 代码部分 class Solution ...
- [转]在 Azure 云服务上设计大规模服务的最佳实践
本文转自:http://technet.microsoft.com/zh-cn/magazine/jj717232.aspx 英文版:http://msdn.microsoft.com/library ...
- 白话容器namespace
进入正题之前是例行装X环节: 过年7天吃的,花了45天又回来了. 近年来容器大火,也正是因为容器,生生灭掉了一个IT岗位!哥也是被生生的逼上了邪路. 那究竟什么是容器呢? 就三个字:它就是个进程!(多 ...
- avd manager或sdk manager无法打开
最近开始搞安卓,使用AS启动项目时老是报各种错误,而网上这方面的资料很多都解决不了.只能边实验边做. 定位到avd manager或sdk manager无法打开,网上找了很多资料,都不能解决,知道看 ...