tensor

Bert 代码详细解读——modeling.py

一笑奈何 提交于 2019-11-28 08:14:44
在官方的bert-github上, git clone https://github.com/google-research/bert.git 主要的文件内容如下图: 主要包括7个主要的python文件,小编要坚持把这7个解读清楚呀! 首先解读的是modeling.py文件,是bert实现的核心代码,主要包括2个类和17个函数, 如下所示: 一、类 1.class BertConfig(object): class BertConfig(object): """Configuration for `BertModel`.""" def __init__(self, vocab_size,“词表中共有多少个词” hidden_size=768,#词嵌入的维度,也是编码层和池化层的维度 num_hidden_layers=12,#transformer隐藏层数个数 num_attention_heads=12,#在encoder层中的注意头个数 intermediate_size=3072,#encoder中间隐藏层神经元数,如feed-forward layer hidden_act="gelu",#encoder和pooler的激活函数 hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position

list,tensor,numpy相互转化

六眼飞鱼酱① 提交于 2019-11-28 07:15:16
1.1 list 转 numpy ndarray = np . array ( list ) 1.2 numpy 转 list list = ndarray . tolist ( ) 2.1 list 转 torch . Tensor tensor = torch . Tensor ( list ) 2.2 torch . Tensor 转 list 先转numpy,后转 list list = tensor . numpy ( ) . tolist ( ) 3.1 torch . Tensor 转 numpy ndarray = tensor . numpy ( ) * gpu上的tensor不能直接转为numpy ndarray = tensor . cpu ( ) . numpy ( ) 3.2 numpy 转 torch . Tensor tensor = torch . from_numpy ( ndarray ) 来源: https://blog.csdn.net/qq_43790749/article/details/100031241

test

混江龙づ霸主 提交于 2019-11-28 06:30:47
目录 1. TensorFlow简介 2. TensorFLow编程模型、计算机制 3. 模型保存与恢复(两种方法:ckpt和PB) 保存模型 4. tensorflow中的计算图(Graph) 5. 会话Session 6. Tensor 7. 变量的创建 8. 变量赋值或更新assign() 9. TensorFlow之矩阵基本运算 1. TensorFlow简介 TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。TensorFlow 最初由Google大脑小组(隶属于Google机器智能研究机构)的研究员和工程师们开发出来,用于机器学习和深度神经网络方面的研究, 但这个系统的通用性使其也可广泛用于其他计算领域。它是谷歌基于DistBelief进行研发的第二代人工智能学习系统。2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源。 其命名来源于本身的原理,Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算。Tensorflow运行过程就是张量从图的一端流动到另一端的计算过程。张量从图中流过的直观图像是其取名为“TensorFlow”的原因。TensorFlow的开发过程重点在于构建执行流图。 TensorFlow 是一个编程系统, 使 用图来表示计算任务 。图中的

Python之TensorFlow的基本介绍-1

烈酒焚心 提交于 2019-11-28 05:11:25
  一、TensorFlow™是一个基于 数据流编程 (dataflow programming)的符号数学系统,被广泛应用于各类 机器学习 (machine learning)算法的编程实现,其前身是 谷歌 的神经网络算法库DistBelief。   TensorFlow: 神经网络(深度) 图像:卷积神经网络 自然语言处理:循环神经网络 特点: 引入各种计算设备(CPU/GPU/TPU),以及能够很好的运行在移动端。 合理的C++使用界面,易用的Python使用界面来构造个执行你的graphs,可以直接写Python/C++程序 采用数据流图(data flow graphs),用于数值计算的开源库,能够灵活的组装图,执行图 谷歌支持,希望成为通用语言 前后端系统: 前端系统:定义程序图的机构 后端系统:运算图结构 会话: 1、运算图的结构 2、分配资源计算 3、掌握资源(变量的资源,队列,线程)   二、前面我们已经介绍了TensorFlow在人工智能领域所能做的事情了,下面会主要介绍TensorFlow的一些常用概念。   1)张量(tensor):   tensor(张量): 一个类型化的N维数组 三部分: 名字、形状、数据类型 阶:和数组的维度类似 属性: graph:张量的默认图 op:张量的操作名 name:张量的字符串描述 shape:张量的形状

PyTorch入门笔记一

我的未来我决定 提交于 2019-11-27 23:48:44
张量 引入pytorch,生成一个随机的5x3张量 >>> from __future__ import print_function >>> import torch >>> x = torch.rand(5, 3) >>> print(x) tensor([[0.5555, 0.7301, 0.5655], [0.9998, 0.1754, 0.7808], [0.5512, 0.8162, 0.6148], [0.8618, 0.3293, 0.6236], [0.2787, 0.0943, 0.2074]]) 声明一个5x3的张量,张量中所有元素初始化为0 >>> x = torch.zeros(5, 3, dtype=torch.long) 从数据直接构造张量,这里的数据一般是python数组 >>> x = torch.tensor([5.5, 3]) >>> print(x) tensor([5.5000, 3.0000]) 从一个已有的tensor上类似创建新的张量,新、旧张量的形状和数据类型相同,除非对dtype进行了覆盖声明 >>> x = x.new_ones(5, 3, dtype=torch.double) >>> print(x) tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]

tf.shape() get wrong shape in tensorflow

六眼飞鱼酱① 提交于 2019-11-27 17:30:01
I define a tensor like this: x = tf.get_variable("x", [100]) But when I try to print shape of tensor : print( tf.shape(x) ) I get Tensor("Shape:0", shape=(1,), dtype=int32) , why the result of output should not be shape=(100) tf.shape(input, name=None) returns a 1-D integer tensor representing the shape of input. You're looking for: x.get_shape() that returns the TensorShape of the x variable. Update: I wrote an article to clarify the dynamic/static shapes in Tensorflow because of this answer: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/

ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer`

。_饼干妹妹 提交于 2019-11-27 17:23:08
问题 I'm building a model in Keras using some tensorflow function (reduce_sum and l2_normalize) in the last layer while encountered this problem. I have searched for a solution but all of it related to "Keras tensor". Here is my code: import tensorflow as tf; from tensorflow.python.keras import backend as K vgg16_model = VGG16(weights = 'imagenet', include_top = False, input_shape = input_shape); fire8 = extract_layer_from_model(vgg16_model, layer_name = 'block4_pool'); pool8 = MaxPooling2D((3,3),

Debugging keras tensor values

二次信任 提交于 2019-11-27 17:13:17
问题 I am implementing own keras loss function. How can I access tensor values? What I've tried def loss_fn(y_true, y_pred): print y_true It prints Tensor("target:0", shape=(?, ?), dtype=float32) Is there any keras function to access y_true values? 回答1: Keras' backend has print_tensor which enables you to do this. You can use it this way: import keras.backend as K def loss_fn(y_true, y_pred): y_true = K.print_tensor(y_true, message='y_true = ') y_pred = K.print_tensor(y_pred, message='y_pred = ')

[Pytorch]Pytorch中tensor常用语法

旧街凉风 提交于 2019-11-27 15:40:45
原文地址: https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中 建立随机数Tensor的多种方法 的区别。 这次我把常用的 Tensor的数学运算 总结到这里,以防自己在使用PyTorch做实验时,忘记这些方法应该传什么参数。 总结的方法包括: Tensor求和以及按索引求和: torch.sum() torch.Tensor.indexadd() Tensor元素乘积 :torch.prod(input) 对Tensor求均值、方差、极值: torch.mean() torch.var() torch.max() torch.min() 最后还有在NLP领域经常用到的: 求Tensor的平方根倒数、线性插值、双曲正切 torch.rsqrt(input) torch.lerp(star,end,weight) torch.tanh(input, out=None) 张量数学运算 元素求和 torch.sum(input) → float 返回输入向量input中所有元素的和。 参数: input (Tensor) - 输入张量 例子: a = torch.randn(1, 3) a 1.5796 0.4102 -0.2885 [torch.FloatTensor of size 1x3] torch.sum(a) 1

Pytorch中Tensor的类型转换

这一生的挚爱 提交于 2019-11-27 15:40:26
Pytorch中的Tensor常用的类型转换函数(inplace操作): (1)数据类型转换   在Tensor后加 .long(), .int(), .float(), .double()等即可,也可以用.to()函数进行转换,所有的Tensor类型可参考 https://pytorch.org/docs/stable/tensors.html (2)数据存储位置转换   CPU张量 ----> GPU张量,使用data.cuda()   GPU张量 ----> CPU张量,使用data.cpu() (3)与numpy数据类型转换   Tensor---->Numpy 使用 data.numpy(),data为Tensor变量   Numpy ----> Tensor 使用 torch.from_numpy(data),data为numpy变量 (4)与Python数据类型转换   Tensor ----> 单个Python数据,使用data.item(),data为Tensor变量且只能为包含单个数据   Tensor ----> Python list,使用data.tolist(),data为Tensor变量,返回shape相同的可嵌套的list (5)剥离出一个tensor参与计算,但不参与求导   Tensor后加 .detach()   官方解释为: