创建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的更多相关文章

  1. pytorch——不用包模拟简单线性预测,数据类型,创建tensor,索引与切片

    常见的学习种类 线性回归,最简单的y=wx+b型的,就像是调节音量大小.逻辑回归,是否问题.分类问题,是猫是狗是猪 最简单的线性回归y=wx+b 目的:给定大量的(x,y)坐标点,通过机器学习来找出最 ...

  2. pytorch 创建tensor的几种方法

    tensor默认是不求梯度的,对应的requires_grad是False. 1.指定数值初始化 import torch #创建一个tensor,其中shape为[2] tensor=torch.T ...

  3. 吴裕雄--天生自然TensorFlow2教程:创建Tensor

    import numpy as np import tensorflow as tf tf.convert_to_tensor(np.ones([2, 3])) tf.convert_to_tenso ...

  4. pytorch 中的数据类型,tensor的创建

    pytorch中的数据类型 import torch a=torch.randn(2,3) b=a.type() print(b) #检验是否是该数据类型 print(isinstance(a,tor ...

  5. pytorch(02)tensor的概念以及创建

    二.张量的简介与创建 2.1张量的概念 张量的概念:Tensor 张量是一个多维数组,它是标量.向量.矩阵的高维拓展 Tensor与Variable Variable是torch.autograd(t ...

  6. Tensor基本操作

    Tensor(张量) 1.Tensor,又名张量,从工程角度来说,可简单地认为它就是一个数组,且支持高效的科学计算.它可以是一个数(标量).一维数组(向量).二维数组(矩阵)或更高维的数组(高阶数组) ...

  7. 深度学习框架PyTorch一书的学习-第三章-Tensor和autograd-1-Tensor

    参考https://github.com/chenyuntc/pytorch-book/tree/v1.0 希望大家直接到上面的网址去查看代码,下面是本人的笔记 Tensor Tensor可以是一个数 ...

  8. 『PyTorch』第五弹_深入理解Tensor对象_上:初始化以及尺寸调整

    一.创建Tensor 特殊方法: t.arange(1,6,2)t.linspace(1,10,3)t.randn(2,3) # 标准分布,*size t.randperm(5) # 随机排序,从0到 ...

  9. Pytorch Tensor 常用操作

    https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor, ...

随机推荐

  1. XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)

    1.XML 格式规范: ① 必须有一个根元素 ② 不可有空格.不可以数字或.开头.大小写敏感 ③ 不可交叉嵌套 ④ 属性双引号(浏览器自动修正成双引号了) ⑤ 特殊符号要使用实体 ⑥ 注释和HTML一 ...

  2. htm 与 html 的区别

    htm 与 html 的区别 前者是超文本标记(Hypertext Markup) 后者是超文本标记语言(Hypertext Markup Language) 可以说 htm = html 同时,这两 ...

  3. [POI2008]激光发射器SZK

    Description 多边形相邻边垂直,边长为整数,边平行坐标轴.要在多边形的点上放一些激光发射器和接收器.满足下列要求: 1发射器和接收器不能放置在同一点: 2发射器发出激光可以沿壁反射,最终到达 ...

  4. HDU 1524

    思路: 算出来每个点的sg值,然后对于每个询问xor一下 //By SiriusRen #include <cstdio> #include <vector> using na ...

  5. 题解报告:hdu 1176 免费馅饼(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小 ...

  6. Struts2 表单提交与execute()方法的结合使用

    1.创建web项目,添加struts2支持的类库,在web.xml中配置struts2过滤器. 2.创建名为UserAction的Action对象,并在其中编写execute()方法,代码如下所示: ...

  7. 自动化中Java面试题

    1.面向对象的特征有哪些方面?答:面向对象的特征主要有以下几个方面:- 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注这些 ...

  8. 一个简单的jsp+servlet登录界面的总结

    这个登录界面我是用eclipse+tomcat7来实现的(网上比较多都是用myeclipse来做的) 1.首先是关于servlet部署的问题 首先你的servlet类要写在WEB-INF的Class文 ...

  9. CentOS系统里如何正确取消或者延长屏幕保护自动锁屏功能(图文详解)

    不多说,直接上干货! 对于我这里想说的是,分别从CentOS6.X  和  CentOS7.X来谈及. 1. 问题:默认启动屏幕保护 问题描述: CentOS系统在用户闲置一段时间(默认为5分钟)后, ...

  10. ASP.Net TextBox只读时不能通过后台赋值取值

    给页面的TextBox设置ReadOnly="True"时,在后台代码中不能赋值取值,下边几种方法可以避免: 1.不设置ReadOnly,设置onfocus=this.blur() ...