mean

PyTorch的十七个损失函数

痞子三分冷 提交于 2019-12-05 21:56:18
本文截取自《PyTorch 模型训练实用教程》,获取全文pdf请点击: tensor-yu/PyTorch_Tutorial ​github.com 版权声明:本文为博主原创文章,转载请附上博文链接! 我们所说的优化,即优化网络权值使得损失函数值变小。但是,损失函数值变小是否能代表模型的分类/回归精度变高呢?那么多种损失函数,应该如何选择呢?请来了解PyTorch中给出的十七种损失函数吧。 1.L1loss 2.MSELoss 3.CrossEntropyLoss 4.NLLLoss 5.PoissonNLLLoss 6.KLDivLoss 7.BCELoss 8.BCEWithLogitsLoss 9.MarginRankingLoss 10.HingeEmbeddingLoss 11.MultiLabelMarginLoss 12.SmoothL1Loss 13.SoftMarginLoss 14.MultiLabelSoftMarginLoss 15.CosineEmbeddingLoss 16.MultiMarginLoss 17.TripletMarginLoss 请运行配套代码,代码中有详细解释,有手动计算,这些都有助于理解损失函数原理。 本小节配套代码: /Code/3_optimizer/3_1_lossFunction 1.L1loss class torch

socket服务端开发之测试使用threading和gevent框架

混江龙づ霸主 提交于 2019-12-05 20:43:30
socket服务端开发之测试使用threading和gevent框架 话题是测试下多线程和gevent在socket服务端的小包表现能力,测试的方法不太严谨,也没有用event loop + pool池的概念。不管是gevent和threading有pool的情况下,确实很省资源,但是固定的pool线程池容易在突发事件中被堵塞住。 另外提一句,劲量少用multiprocessing,因为他的进程开销有些大,当然如果单纯用multiprocessing做进程池里面worker进程,那还是个好选择,毕竟他是可以跑多核心的。 Hello , 另外请大家多关注下,我的个人博客 blog.xiaorui.cc 不管怎么说,还是有点属于自娱自乐的形态,有问题之处,请大家喷之 ! 话说,我们当时在搞一个回溯任务中心,说白了就是开发任务接口,通过mapreduce计算平均值,关于业务的逻辑我就不多写了,写出来,也只是浪费大家的思考。干脆点,每个连接都特意堵塞了0.5秒钟。 在大量的tcp短连接测试下,threading的开销越来越大,所以造成了在并发数加大的情况下,出现threading崩溃的情况 ! gevent是 libevent和协程的融合,一个线程里面都可以跑超多的协程! 利用libevent做io堵塞的调度 ,gevent体系下,同一时间只有一个任务在运行 ! 先来测试下多线程:

Element wise mean of multiple lists in R

三世轮回 提交于 2019-12-05 16:22:44
I have ten huge lists(each list has seven element but elements are huge) and I need to calculate the element wise mean of these lists. So if there are A1, A2, A3,..., A10 lists. I need to calculate : mean1 = mean(A1[[1]], A2[[1]], A3[[1]], ...,A10[[1]]) . . . mean7 = mean(A1[[7]], A2[[7]], A3[[7]], ....A10[[7]]) I have done it with for loop but I wanted to know if there is a better solution provided by R. Thank you in advance. Assuming your A s are lists of vectors: Anames <- paste0("A", 1:10) # for completeness for(A in Anames) assign(A, lapply(1:7, function(x) rnorm(1000))) sapply(1:7,

The as.numeric function changes the values in my dataframe [duplicate]

扶醉桌前 提交于 2019-12-05 16:12:58
This question already has answers here : How to convert a factor to integer\numeric without loss of information? (7 answers) Closed 5 years ago . I have a column containing speed measurements which I need to change to numeric so that I can use both the mean and sum functions. However, when I do convert them the values change substantially. Why is this? This is what my data look like at first: And here is the structure of the data frame: 'data.frame': 1899571 obs. of 20 variables: $ pcd : Factor w/ 1736958 levels "AB101AA","AB101AB",..: 1 2 3 4 5 6 6 7 7 8 $ pcdstatus : Factor w/ 5 levels

解释张量及TF的一些API

帅比萌擦擦* 提交于 2019-12-05 15:39:42
张量的定义   张量(Tensor)理论是数学的一个分支学科,在力学中有重要应用。张量这一术语起源于力学,它最初是用来表示弹性介质中各点应力状态的,后来张量理论发展成为力学和物理学的一个有力的数学工具。张量之所以重要,在于它可以满足一切物理定律必须与坐标系的选择无关的特性。 张量概念是矢量概念的推广,矢量是一阶张量。张量是一个可用来表示在一些矢量、标量和其他张量之间的线性关系的多线性函数(可以理解成是向量、矩阵以及更高维结构的统称)。   But we don’t have to restrict ourselves to linear algebra. As it turns out, we can move up the mathematical food chain a bit. It has long been known that there are bigger fish in the mathematical abstraction sea than just matrices. One such candidate is called a tensor. Tensors figure prominently in the mathematical underpinnings of general relativity and are fundamental to

Calculate a series of weighted means in R for groups with different weightings

*爱你&永不变心* 提交于 2019-12-05 15:36:52
I have the following dataset (simple version of my actual data), 'data', and would like to calculate weighted means for variables x1 and x2, using weightings w1 and w2 respectively, split up into two groups (groups determined by the variable n). data <- data.frame(n = c(1,1,1,2,2,2), x1 = c(4,5,4,7,5,5), x2 = c(7,10,9,NaN,11,12), w1 = c(0,1,1,1,1,1), w2 = c(1,1,1,0,0,1)) I'm trying to do it using with() but get an error when I run this: with(data, aggregate(x = list(x1=x1, x2=x2), by = list(n = n), FUN = weighted.mean, w = list(w1 = w1,w2 = w2))) On the otherhand, if weights aren't specified

Cumulative mean with conditionals

为君一笑 提交于 2019-12-05 12:52:58
New to R. Small rep of my df: PTS_TeamHome <- c(101,87,94,110,95) PTS_TeamAway <- c(95,89,105,111,121) TeamHome <- c("LAL", "HOU", "SAS", "MIA", "LAL") TeamAway <- c("IND", "LAL", "LAL", "HOU", "NOP") df <- data.frame(cbind(TeamHome, TeamAway,PTS_TeamHome,PTS_TeamAway)) df TeamHome TeamAway PTS_TeamHome PTS_TeamAway LAL IND 101 95 HOU LAL 87 89 SAS LAL 94 105 MIA HOU 110 111 LAL NOP 95 121 Imagine these are the first four games of a season with 1230 games. I want to calculate the cumulative points per game (mean) at any given time for the home team and the visiting team. The output would look

WGAN-GP精彩分析(附源代码)

匆匆过客 提交于 2019-12-05 10:54:02
转载自: https://www.e-learn.cn/content/qita/814071 from datetime import datetime import os import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from six.moves import xrange data = np.load('final37.npy') data = data[:,:,0:60] #显示原始数据图像 def Show_images(data,show_nums,save=False): index = 0 for n in range(show_nums): show_images = data[index:index+100] show_images = show_images.reshape(100,3,60,1) r,c = 10,10 fig,axs = plt.subplots(r,c) cnt = 0 for i in range(r): for j in range(c): xy = show_images[cnt] for k in range(len(xy)): x = xy[k][0:30] y = xy[k][30:60] if k == 0 : axs[i

WGAN-GP生成自己的数据

≡放荡痞女 提交于 2019-12-05 10:53:09
WGAN-GP生成MNIST 参考博客 点击打开链接 点击打开链接 33个epoch结果 #coding:utf-8 import os import numpy as np import scipy.misc import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #as mnist_data def conv2d(name, tensor,ksize, out_dim, stddev=0.01, stride=2, padding='SAME'): with tf.variable_scope(name): w = tf.get_variable('w', [ksize, ksize, tensor.get_shape()[-1],out_dim], dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=stddev)) var = tf.nn.conv2d(tensor,w,[1,stride, stride,1],padding=padding) b = tf.get_variable('b', [out_dim], 'float32',initializer=tf.constant

GAN实现(TensorFlow,MNIST数据集)

╄→尐↘猪︶ㄣ 提交于 2019-12-05 10:52:32
Generative Adversarial Nets in TensorFlow 生成对抗网络(简称GAN)是一种非常流行的神经网络。它由Ian Goodfellow等人在2014年NIPS论文中首次引入。这篇论文引发了对神经网络对抗训练的研究热潮。突然之间,GAN的许多改进都出现了:DCGAN,Sequence-GAN,LSTM-GAN等等。在2016年NIPS会议上,甚至有一整个专门针对GAN的研讨会! 请注意,该代码可在 https://github.com/wiseodd/generative-models 中找到。 首先,让我们回顾一下这篇论文的要点。之后,我们将一如既往地尝试使用TensorFlow和MNIST数据来实现GAN。 Generative Adversarial Nets 让我们举一个“造假币的罪犯”和“警察”之间的玫瑰关系的例子。在假币方面,罪犯的目标是什么?警察的目标是什么?我们列举一下: 为了成为一个成功的伪造犯,罪犯想欺骗警察,这样警察就不能分辨出假币与真钱的区别 为了成为正义的典范,警察希望尽可能地发现假币 在那里,我们看到产生了冲突。博弈论中的这种情况可以模拟为一个极小化极大游戏(minimax game)。这个过程被称为对抗过程。 生成对抗网(GAN)是对抗过程的一个特例,其组成部分(警察和罪犯)是神经网络。第一个网络生成数据