tensor

tensorflow|tf.train.slice_input_producer|tf.train.Coordinator|tf.train.start_queue_runners

二次信任 提交于 2019-12-02 02:29:22
#### ''' tf.train.slice_input_producer :定义样本放入文件名队列的方式[迭代次数,是否乱序],但此时文件名队列还没有真正写入数据 slice_input_producer(tensor_list, num_epochs=None, shuffle=True, seed=None,capacity=32, shared_name=None, name=None) tensor_list:如[images,labels] = [['img1','image2','imag3','img4','img5','img6'],[1,2,3,4,5,6]] num_epochs:可选参数,迭代次数 num_epochs=None 无限次遍历tensor列表 num_epochs=N 生成器只能遍历列表N次 shuffle:shuffle=True 乱序样本 shuffle=False需要在批处理时使用tf.train.shuffle_batch函数打乱样本 seed:随机数种子 在shuffle=True 时使用 capacity:设置tensor列表的容量 shared_name:可选参数,如果设置一个‘shared_name’,则在不同的上下文环境(Session)中可以通过这个名字共享生成的tensor name:设置操作名称 ''' import

PyTorch实战:Chapter-1(PyTorch介绍和入门)

白昼怎懂夜的黑 提交于 2019-12-02 01:18:35
PyTorch简介 为什么要用PyTorch? 在讲PyTorch的优点前,先讲现在用的最广的TensorFlow。 TensorFlow提供了一套深度学习从定义到部署的工具链,非常强大齐全的一套软件包,很适合工程使用,但也正是为了工程使用, TensorFlow部署模型是基于静态计算图设计的,计算图需要提前定义好计算流程,这与传统的程序语言(host language)不同。这样做可让同一计算图平行运行在多个GPU上,加快和优化模型计算。但这样设计限制了TensorFlow的灵活性。 我个人理解静态图和铺下水管道很像,TensorFlow需要将所有管道都铺好后(模型定义完成),通过Session向系统申请注水(数据),水流经过管道才能获取对应管道的值,这样的架构不利于Debug,因为定义模型的时候是没有数据流动,无法查看数据变换过程。这导致了Python提供的很多操作运算都不好使。 PyTorch支持动态计算图,能够提供线上操作,这在定义网络上操作更简单,静态图需要一个个操作声明,而动态图可以调用各种函数。并且因为是动态图,在改变网络的某一层或者改变一些变量,例如动态修改学习率,对模型做fine-tune操作,都更简单方便。 PyTorch非常适合调用新模型,用于设计自己的架构,测试新想法~ 当然我学习PyTorch的一个非常关键的原因: 许多新的模型是使用PyTorch实现的~

PyTorch-GAN-master中的wgan的理解

岁酱吖の 提交于 2019-12-02 01:12:43
PyTorch-GAN-master中wgan代码的解读 这是我第一次在csdn上写博客,因为前段时间开始接触到GAN,并且在一篇论文中看到wgan这样一个新奇的东西,因为从来没有写过GAN的网络,而且之前都是用pytorch在写网络,就在github上找到了PyTorch-GAN-master这个系列,里面恰好有一个wgan的网络,我在看过之后,就想自己能否尝试一下根据这个写一篇blog,当然,权当一篇笔记,供自己以后忘了的时候看看。处于当作笔记的目的,这里面写的主要是我作为一个新手想弄清楚的一些东西 PyTorch-GAN的链接 各个阶段的数据的shape 因为我也只是一个新手,在之前写网络的时候,其实最纠结的就是网络结构中的参数,和数据的shape,因为至少将这些弄清楚了,写对了,整个才能够运行,关于到Tensor的shape,主要就是网络的输入,forward,输出。 forward 输入 输出 (可能表达得不是很专业) 代码中的wgan算是一个最简单的wgan,因为就只有一个生成器和一个鉴别器,并且都是线性层,所用的数据集是mnist的手写数字数据集,也就是一张张28*28的图片,网络中默认的batch_size是64。 其中Generator网络部分代码如下: class Generator ( nn . Module ) : def __init__ ( self )

How to find the top k values in a 2-D tensor in tensorflow

你离开我真会死。 提交于 2019-12-02 00:47:55
Is there a way to find the top k values in a 2-D tensor in Tensorflow? I can use tf.nn.top_k for a 1-D tensor but it cannot work with a 2-D tensor. I have a 2-D tensor with unknown size, is there a way to find the top k values and their indices? Thanks a lot. You can reshape your matrix to a 1-D tensor before tf.nn.top_k() , then compute the 2-D indices from the 1-D ones: x = tf.random_uniform((3, 4)) x_shape = tf.shape(x) k = 3 top_values, top_indices = tf.nn.top_k(tf.reshape(x, (-1,)), k) top_indices = tf.stack(((top_indices // x_shape[1]), (top_indices % x_shape[1])), -1) with tf.Session()

TensorFlow变量Variable

落爺英雄遲暮 提交于 2019-12-01 18:41:50
变量通过 tf.Variable 类进行操作,可以通过运行op改变它的值。 与 tf.Tensor 对象不同,tf.Variable 存在于单个 session.run 调用的上下文之外。 创建变量 创建变量的最佳方式是调用 tf.get_variable 函数。 这个函数的本意是获取已存在的变量,因而必须指定变量名称。当变量不存在时,将会创建一个。 my_variable = tf.get_variable("my_variable", [1,2,3])# 指定dtype和initializermy_int_variable = tf.get_variable("my_int_variable", [1,2,3], dtype=tf.int32, initializer=tf.zeros_initializer)# 初始化为Tensor的值,不能指定形状my_ten_variable = tf.get_variable("other_var", dtype=tf.int32, initializer=tf.constant([23,42])) 变量集合 默认情况下,每个 tf.Variable 都放置在以下两个集合中: tf.GraphKeys.GLOBAL_VARIABLES:可在多个设备间共享 tf.GraphKeys.TRAINABLE_VARIABLES

Are there any computational efficiency differences between nn.functional() Vs nn.sequential() in PyTorch

自作多情 提交于 2019-12-01 17:57:45
The following is a Feed-forward network using the nn.functional() module in PyTorch import torch.nn.functional as F class newNetwork(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 128) self.fc2 = nn.Linear(128, 64) self.fc3 = nn.Linear(64,10) def forward(self,x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.softmax(self.fc3(x)) return x model = newNetwork() model The following is the same Feed-forward using nn.sequential() module to essentially build the same thing. What is the difference between the two and when would i use one instead of the other?

tensorboard_embedding

时间秒杀一切 提交于 2019-12-01 16:42:41
from tensorboardX import SummaryWriter import torchvision writer=SummaryWriter(log_dir="embedding") mnist=torchvision.datasets.MNIST("mnist",download=True) writer.add_embedding( mat=mnist.train_data.reshape((-1,28*28))[:100,:], metadata=mnist.train_labelS[:100], label_img=mnist.train_data[:100,:,:].reshape((-1,1,28,28)).float()/255, global_step=0 )    add_embedding (mat , metadata = None , label_img = None , global_step = None , tag = 'default' , metadata_header = None ) mat (torch.Tensor or numpy.array): 一个矩阵,每行代表特征空间的一个数据点 metadata (list or torch.Tensor or numpy.array, optional): 一个一维列表,mat

AttributeError: 'Tensor' object has no attribute 'numpy'

左心房为你撑大大i 提交于 2019-12-01 16:16:13
How can I fix this error I downloaded this code from GitHub. predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].numpy() throws the error AttributeError: 'Tensor' object has no attribute 'numpy' Please help me fix this! I used: sess = tf.Session() with sess.as_default(): predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval() And i get this error. Someone help me i just want it to work why is this so hard? D:\Python>python TextGenOut.py File "TextGenOut.py", line 72 predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval() ^