tensor

how do I assign a value to a tensor in order to prevent this error: TypeError: 'RefVariable' object does not support item assignment?

匆匆过客 提交于 2019-12-02 11:10:50
问题 I have a function that has one two inputs and both of them are tensor. I need to do some processes on these inputs and then used the output in my network. one of these inputs is the output of the convolution layer and I do not know how do I solve this error! I put the function below: the below code implements this equation, G , C(u) and C(v) are numbers. enter image description here def DCT(a, b): for u in range(8): for v in range(8): for x in range(8): for y in range(8): b[u,v] = b[u, v] + 0

什么是pytorch?

怎甘沉沦 提交于 2019-12-02 11:04:14
pytorch是一个基于python的科学计算工具 这个一个可以最大化利用GPU能力的NumPy的替代品 这是一个深度学习的平台,可以提供最大的自由度已经计算速度 张量(Tensors) Pytorch 的一大作用就是可以代替 Numpy 库,所以首先介绍 Tensors ,也就是张量,它相当于 Numpy 的多维数组(ndarrays)。两者的区别就是 Tensors 可以应用到 GPU 上加快计算速度。 首先导入必须的库,主要是 torch from __future__ import print_function import torch 声明和定义 首先是对 Tensors 的声明和定义方法,分别有以下几种: torch.empty() : 声明一个未初始化的矩阵。 x = torch . empty ( 5 , 3 ) print ( x ) output: tensor ( [ [ 5.0850e+31 , 7.5338e+28 , 1.3556e-19 ] , [ 1.8530e+22 , 7.0976e+22 , 2.2087e+03 ] , [ 2.9386e+29 , 7.1104e-04 , 1.3556e-19 ] , [ 1.8567e-01 , 1.9069e-19 , 7.5553e+28 ] , [ 5.2839e-11 , 1.7589e+22 ,

pytorch常用函数

不羁的心 提交于 2019-12-02 10:43:24
总体介绍 https://pytorch.org/docs/stable/torch.html https://pytorch.apachecn.org/docs/1.2/torch.html nn 实现了神经网络中大多数的损失函数,例如 nn.MSELoss 用来计算均方误差, nn.CrossEntropyLoss 用来计算交叉熵损失。 nn.Module 是 nn 中最重要的类,可把它看成是一个网络的封装,包含网络各层定义以及forward方法,调用forward(input)方法,可返回前向传播的结果。定义网络时,需要继承 nn.Module ,并实现它的forward方法,把网络中具有可学习参数的层放在构造函数 __init__ 中。如果某一层(如ReLU)不具有可学习的参数,则既可以放在构造函数中,也可以不放,但建议不放在其中,而在forward中使用 nn.functional 代替。 import torch . nn as nn import torch . nn . functional as F torch.optim 中实现了深度学习中绝大多数的优化方法,例如RMSProp、Adam、SGD等,更便于使用,因此大多数时候并不需要手动写上述代码。 import torch . optim as optim torchvision 实现了常用的图像数据加载功能

How to convert Tensor to ndarray (tensor with adversarial images inside)

巧了我就是萌 提交于 2019-12-02 09:04:38
问题 NOTE: I already have tried solutions from different SO questions with no success, details follow. I'm studying cleverhans Pyhton tutorials, focusing on this code (keras model case). I have a base keras knowledge but I've just started with Tensorflow (total newbie). I'm trying to visualize the adversial images generated in this piece of code (quote from the linked cleverhans sources): # Initialize the Fast Gradient Sign Method (FGSM) attack object and graph fgsm = FastGradientMethod(wrap, sess

Tensorflow: using an input-pipeline (.csv) as a dictionary for training

ⅰ亾dé卋堺 提交于 2019-12-02 08:52:08
问题 I'm trying to train a model on a .csv dataset (5008 columns, 533 rows). I'm using a textreader to parse the data into two tensors, one holding the data to train on [example] and one holding the correct labels [label]: def read_my_file_format(filename_queue): reader = tf.TextLineReader() key, record_string = reader.read(filename_queue) record_defaults = [[0.5] for row in range(5008)] #Left out most of the columns for obvious reasons col1, col2, col3, ..., col5008 = tf.decode_csv(record_string,

Tensorflow Cpp API调用训练好的模型

一曲冷凌霜 提交于 2019-12-02 05:08:53
之前的笔记有一篇完成了对tensorflow的编译,然后简单写了个测试程度,所以,应该是可以用了的,然后上篇简单写了个基于Keras的手写数字识别的的模型需要你连了一下,那么现在就试试用tensorflow的 C++ API调用训练好的模型测试下。这里推荐一个 GitHub上找到的项目 ,用的C++调用tensorflow API的。 首先,前面把模型保存为了.h5文件,但是,很遗憾,我查到的资料都是用C++加载的.pb文件,所以我还是得把模型保存为.pb文件或者将.h5文件转存为.pb文件,然后上帝说有光就有了光,我说要有个保存模型为.pb文件的,上帝就给了一个代码: from keras.models import load_model import tensorflow as tf from keras import backend as K from tensorflow.python.framework import graph_io def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True): from tensorflow.python.framework.graph_util import convert_variables_to_constants

Tensorflow: using an input-pipeline (.csv) as a dictionary for training

人盡茶涼 提交于 2019-12-02 04:34:34
I'm trying to train a model on a .csv dataset (5008 columns, 533 rows). I'm using a textreader to parse the data into two tensors, one holding the data to train on [example] and one holding the correct labels [label]: def read_my_file_format(filename_queue): reader = tf.TextLineReader() key, record_string = reader.read(filename_queue) record_defaults = [[0.5] for row in range(5008)] #Left out most of the columns for obvious reasons col1, col2, col3, ..., col5008 = tf.decode_csv(record_string, record_defaults=record_defaults) example = tf.stack([col1, col2, col3, ..., col5007]) label = col5008

pytorch 模块

时光怂恿深爱的人放手 提交于 2019-12-02 03:47:37
pytorch的中文手册:https://github.com/zergtant/pytorch-handbook 一、定义/初始化张量Define tensors tensor,即“张量”。实际上跟numpy数组、向量、矩阵的格式基本一样。但是是专门针对GPU来设计的,可以运行在GPU上来加快计算效率。 PyTorch中定义tensor,就跟numpy定义矩阵、向量差不多,例如定义一个5×3的tensor,每一项都是0的张量: x = torch.zeros(5,3) 如果想查看某个tensor的 形状 的话,使用: z.size() ,或者 z.shape ,但是前者更常用。 下面列举一些 常用 的定义tensor的方法: 常数初始化: torch.empty(size) 返回形状为size的空tensor torch.zeros(size) 全部是0的tensor torch.zeros_like(input) 返回跟input的tensor一个size的全零tensor torch.ones(size) 全部是1的tensor torch.ones_like(input) 返回跟input的tensor一个size的全一tensor torch.arange(start=0, end, step=1) 返回一个从start到end的序列,可以只输入一个end参数

how do I assign a value to a tensor in order to prevent this error: TypeError: 'RefVariable' object does not support item assignment?

半世苍凉 提交于 2019-12-02 02:47:36
I have a function that has one two inputs and both of them are tensor. I need to do some processes on these inputs and then used the output in my network. one of these inputs is the output of the convolution layer and I do not know how do I solve this error! I put the function below: the below code implements this equation, G , C(u) and C(v) are numbers. enter image description here def DCT(a, b): for u in range(8): for v in range(8): for x in range(8): for y in range(8): b[u,v] = b[u, v] + 0.25 * C(u) * C(v) * a[x, y]* np.cos((2 * x+1) * (u) * np.pi / 16) * np.cos((2 * y+1) * (v) * np.pi / 16