tensor

What are symbolic tensors, and why do they throw “use `steps_per_epoch` argument” error?

和自甴很熟 提交于 2020-01-25 04:06:07
问题 Note: I already solved my issue, but I'm posting the question in case others have it too and because I don't understand how I solved it. I was building a Named Entity Classifier (sequence labelling model) in Keras with Tensorflow backend. When I tried to fit the model, I got this error (which, amazingly, returns only 4 Google results): "If your data is in the form of symbolic tensors, you should specify the `steps_per_epoch` argument (instead of the batch_size argument, because symbolic

np.dot product between two 3D matrices along specified axis

强颜欢笑 提交于 2020-01-25 01:04:20
问题 I have two 3D matrices: a = np.random.normal(size=[3,2,5]) b = np.random.normal(size=[5,2,3]) I want the dot product of each slice along 2 and 0 axes respectively: c = np.zeros([3,3,5]) # c.size is 45 c[:,:,0] = a[:,:,0].dot(b[0,:,:]) c[:,:,1] = a[:,:,1].dot(b[1,:,:]) ... I would like to do that using np.tensordot (for efficiency and speed) I have tried: c = np.tensordot(a, b, axes=[2,0]) but I get a 4D array with 36 elements (instead of 45). c.shape, c.size = ((3L, 2L, 2L, 3L), 36). I have

pytoch总结—TENSOR 和NUMPY相互转换

ⅰ亾dé卋堺 提交于 2020-01-25 00:39:23
我们很容易用 numpy() 和 from_numpy() 将 Tensor 和NumPy中的数组相互转换。但是需要注意的⼀一 点是: 这两个函数所产⽣生的的 Tensor 和NumPy中的数组共享相同的内存(所以他们之间的转换很 快),改变其中一个时另⼀个也会改变!!! 还有一个常用的将NumPy中的array转换成 Tensor 的方法就是 torch.tensor() , 需要注意的 是,此方法总是会进行行数据拷贝(就会消耗更多的时间和空间),所以返回的 Tensor 和原来的数 据不再共享内存。 Tensor 转NumPy a = torch.ones(5) b = a.numpy() print(a, b) NumPy数组转 Tensor import numpy as np a = np.ones(5) b = torch.from_numpy(a) print(a, b) 所有在CPU上的 Tensor (除了 CharTensor )都⽀持与NumPy数组相互转换。 此外上面提到还有一个常⽤的方法就是直接用 torch.tensor() 将NumPy数组转换成 Tensor ,需要 注意的是该方法总是会进行行数据拷贝,返回的 Tensor 和原来的数据不再共享内存。 来源: CSDN 作者: TJMtaotao 链接: https://blog.csdn.net

Element by element tensor multiplication in python

泪湿孤枕 提交于 2020-01-24 13:17:27
问题 I am trying to solve a problem in computational algebra using python. Basically given two sets, say A={a,b} and B={e} , I need to compute the element by element tensor products and get a final set say C={a\tensor{e},b\tensor{e}} containing these products of elements. I can do an element by element multiplication using arrays with numbers but I can't do an element by element tensor multiplication of letters instead of numbers. 回答1: Not sure if I understood correctly, this below code multiplies

indexing in tensorflow slower than gather

房东的猫 提交于 2020-01-24 11:05:26
问题 I am trying to index into a tensor to get a slice or single element from 1d tensors. I find that there is significant performance difference when using the numpy way of indexing [:] and slice vs tf.gather (almost 30-40% ). Also I observe that tf.gather has significant overhead when used on scalars (looping over unstacked tensor) as opposed to tensor . Is this a known issue ? example code (inefficient) : for node_idxs in graph.nodes(): node_indice_list = tf.unstack(node_idxs) result = [] for

torch.nn.functional.binary_cross_entropy_with_logits

独自空忆成欢 提交于 2020-01-24 00:16:41
torch.nn.functional.binary_cross_entropy_with_logits(input, target, weight=None, size_average=None, reduce=None, reduction=‘mean’, pos_weight=None) 参数说明 input 神经网络预测结果(未经过sigmoid),任意形状的tensor target 标签值,与input形状相同 weight 权重值,可用于mask的作用, 具体作用下面讲解,形状同input size_average 弃用,见reduction参数 reduce 弃用,见reduction参数 reduction 指定对输出结果的操作,可以设定为 none mean sum ; none将不对结果进行任何处理,mean对结果求均值, sum对结果求和, 默认是mean 函数功能 对神经网络的输出结果进行sigmoid操作,然后求交叉熵,当参数reduction=mean时其实际效果和下面的程序相同; weight参数实际是对交叉熵结果的权重 x = torch . sigmoid ( pred ) # pred是神经网络的输出 result = - torch . mean ( ( label * torch . log ( x ) + ( 1 - label ) *

tensorflow张量排序

孤街醉人 提交于 2020-01-23 23:30:40
本篇记录一下TensorFlow中张量的排序方法 tf.sort和tf.argsort # 声明tensor a是由1到5打乱顺序组成的 a = tf.random.shuffle(tf.range(5)) # 打印排序后的tensor print(tf.sort(a,direction='DESCENDING').numpy()) # 打印从大到小排序后,数字对应原来的索引 print(tf.argsort(a,direction='DESCENDING').numpy()) index = tf.argsort(a,direction='DESCENDING') # 按照索引序列取值 print(tf.gather(a,index)) # 返回最大的两个值信息 res = tf.math.top_k(a,2) # indices返回索引 print(res.indices) # values返回值 print(res.values) 计算准确率实例: # 定义模型输出预测概率 prob = tf.constant([[0.1,0.2,0.7],[0.2,0.7,0.1]]) # 定义y标签 target = tf.constant([2,0]) # 求top3的索引 k_b = tf.math.top_k(prob,3).indices # 将矩阵进行转置,即把top-1

What is a dimensional range of [-1,0] in Pytorch?

寵の児 提交于 2020-01-23 11:49:26
问题 So I'm struggling to understand some terminology about collections in Pytorch. I keep running into the same kinds of errors about the range of my tensors being incorrect, and when I try to Google for a solution often the explanations are further confusing. Here is an example: m = torch.nn.LogSoftmax(dim=1) input = torch.tensor([0.3300, 0.3937, -0.3113, -0.2880]) output = m(input) I don't see anything wrong with the above code, and I've defined my LogSoftmax to accept a 1 dimensional input. So

Pytorch框架学习(4)——autograd与逻辑回归

倾然丶 夕夏残阳落幕 提交于 2020-01-23 02:32:44
autograd与逻辑回归 文章目录 autograd与逻辑回归 1. torch.autograd自动求导系统 2. 逻辑回归 1. torch.autograd自动求导系统 torch.autograd.backward 功能:自动求取梯度 tensors:用于求导的张量,如loss retain_graph:保存计算图 create_graph:创建导数计算图,用于高阶求导 grad_tensors:多梯度权重(当有多个loss需要计算梯度时,需要设置各个loss之间的权重比例) w = torch . tensor ( [ 1 . ] , requires_grad = True ) x = torch . tensor ( [ 2 . ] , requires_grad = True ) a = torch . add ( w , x ) b = torch . add ( w , 1 ) y0 = torch . mul ( a , b ) y1 = torch . add ( a , b ) loss = torch . cat ( [ y0 , y1 ] , dim = 0 ) grad_tensors = torch . tensor ( [ 1 . , 2 . ] ) loss . backward ( gradient = grad_tensors ) #

01_使用 Java API 进行预测

折月煮酒 提交于 2020-01-22 05:10:34
TensorFlow for Java WARNING : The TensorFlow Java API is not currently covered by the TensorFlow API stability guarantees . 目前,TensorFlow Java API 不在 TensorFlow API 稳定性保证的范围内。 For using TensorFlow on Android refer instead to TensorFlow Lite . 关于在Android上使用TensorFlow,请参考TensorFlow Lite。 使用 Java API 进行预测 private static Session loadSession() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Graph graph = new Graph(); //创建图结构 InputStream is = getStreamFromPb("car_model.pb"); //加载本地 pb 文件到内存 byte[] graphBytes = new byte[0]; try { graphBytes = IOUtils.toByteArray(is); } catch