mean

动手学深度学习PyTorch版——Task06学习笔记

北战南征 提交于 2020-02-23 22:57:02
批量归一化和残差网络 批量归一化 从零开始 import time import torch from torch import nn , optim import torch . nn . functional as F import torchvision import sys sys . path . append ( "/home/kesci/input/" ) import d2lzh1981 as d2l device = torch . device ( 'cuda' if torch . cuda . is_available ( ) else 'cpu' ) def batch_norm ( is_training , X , gamma , beta , moving_mean , moving_var , eps , momentum ) : # 判断当前模式是训练模式还是预测模式 if not is_training : # 如果是在预测模式下,直接使用传入的移动平均所得的均值和方差 X_hat = ( X - moving_mean ) / torch . sqrt ( moving_var + eps ) else : assert len ( X . shape ) in ( 2 , 4 ) if len ( X . shape ) == 2 : #

pytorch的linspace, rand, randn, normal

﹥>﹥吖頭↗ 提交于 2020-02-22 13:25:19
torch.linspace torch.linspace(start, end, steps) returns a one-dimensional tensor of equally spaced points between [start, end]。steps默认值是100。 torch.rand torch.rand(size) returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1). torch.randn torch.randn(size) retu rns a tensor filled with random numbers from a normal distribution with mean `0` and variance `1` (also called the standard normal distribution). torch.normal torch.normal returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given. The

tensorflow学习笔记 + 程序 (四)tensorboard可视化

爷,独闯天下 提交于 2020-02-15 22:01:47
1 tensorboard网络结构 下文中所用的部分数据集链接(百度网盘): 链接:https://pan.baidu.com/s/1_Y0rWLj9wuJefzT9JME5Ug 提取码:6fho 先来说说流程图 1.1 不用命名空间 实际上可以直接在会话中添加 : writer = tf . summary . FileWriter ( 'logs/' , sess . graph ) # 扔初始化变量后面就行 另外,关于这个函数 : log是事件文件所在的目录(这里就是当前主目录下),第二个参数是事件文件要记录的图(这里是tensorflow默认的图)。 1.1.1 程序 import tensorflow as tf from tensorflow . examples . tutorials . mnist import input_data # 载入数据集 mnist = input_data . read_data_sets ( "MNIST_data" , one_hot = True ) # 定义变量_每个批次的大小 batch_size = 100 # 计算一共多少批次 n_batch = mnist . train . num_examples // batch_size # 整除 # 命名空间 # 定义placeholder x = tf .

Pytorch框架学习(10)——损失函数

爷,独闯天下 提交于 2020-02-14 00:42:53
文章目录 1. 损失函数概念 2. 交叉熵损失函数 3. NLL/BCE/BCEWITHLogits Loss 1. 损失函数概念 损失函数:衡量模型输出与真实标签的差异 损失函数(Loss Function): L o s s = f ( y ^ , y ) Loss = f(\hat{y}, y) L o s s = f ( y ^ ​ , y ) 代价函数(Cost Function): L o s s = 1 N ∑ i N f ( y i ^ , y i ) Loss = \frac{1}{N}\sum^{N}_{i} f(\hat{y_i}, y_i) L o s s = N 1 ​ i ∑ N ​ f ( y i ​ ^ ​ , y i ​ ) 目标函数(Objective Function): O b j = C o s t + R e g u l a r i z a t i o n ( 正 则 项 ) Obj = Cost + Regularization(正则项) O b j = C o s t + R e g u l a r i z a t i o n ( 正 则 项 ) 2. 交叉熵损失函数 1.nn.CrossEntropyLoss 功能:nn.LogSoftmax()与nn.NLLLoss()结合,进行交叉熵计算 主要参数: weight

简单的手写数字识别

谁说我不能喝 提交于 2020-02-12 16:58:08
个人笔记 感谢指正 手写数字识别 参考链接:https://blog.csdn.net/qq_32241189/article/details/80450741 1.导入包 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data 2.导入数据集 mnist = input_data.read_data_sets('MNIST_data',one_hot=True) #read_data_sets函数是专门下载MNIST数据集的,注意这里注意这里是one_hot,即标签不是一个值而是一个向量 #数据集分为train,validation,test三个数据集: 1.返回数据集train样本数 mnist.train.num_examples 2.返回数据集validation样本数 mnist.validation.num_examples 3.返回数据集test样本数 mnist.test.num_examples 4.使用mnist.train.images返回train数据集中的所有图片的像素值 5.使用mnist.train.labels返回train数据集中的所有图片的标签 6.使用mnist.train.next_batch()将数据输入神经网络 3.定义批次的大小

tensorflow中 tf.reduce_mean函数

痞子三分冷 提交于 2020-02-12 03:53:11
tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。 reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None) 第一个参数input_tensor: 输入的待降维的tensor; 第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值; 第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度; 第四个参数name: 操作的名称; 第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用; 以一个维度是2,形状是[2,3]的tensor举例: import tensorflow as tf x = [[1,2,3], [1,2,3]] xx = tf.cast(x,tf.float32) mean_all = tf.reduce_mean(xx, keep_dims=False) mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False) mean_1 = tf.reduce_mean(xx, axis

ncnn之六:ncnn量化(post-training quantization)三部曲 - ncnnoptimize

﹥>﹥吖頭↗ 提交于 2020-02-11 13:13:16
1 NetOptimize 定义类 NetOptimize 用于优化网络结构 class NetOptimize : public ncnn :: Net { public : // 0=fp32 1=fp16 int storage_type ; public : int fuse_batchnorm_scale ( ) ; int fuse_convolution_batchnorm ( ) ; int fuse_convolutiondepthwise_batchnorm ( ) ; int fuse_deconvolution_batchnorm ( ) ; int fuse_deconvolutiondepthwise_batchnorm ( ) ; int fuse_innerproduct_batchnorm ( ) ; int fuse_innerproduct_dropout ( ) ; int fuse_convolution_activation ( ) ; int fuse_convolutiondepthwise_activation ( ) ; int fuse_deconvolution_activation ( ) ; int fuse_deconvolutiondepthwise_activation ( ) ; int fuse

4.模型评估之ROC和AUC

我们两清 提交于 2020-02-11 06:46:06
基本概念 ROC全称是“受试者工作特征”(Receiver Operating Characteristic)曲线。ROC曲线的纵轴是“真正率”(True Positive Rate, TPR),横轴是“假正例率”(False Positive Rate, FPR)。 TPR = TP / ( TP + FN ) FPR = FP / ( TN + FP ) 进行学习器的比较时,若一个学习器的ROC曲线被另一个学习器的曲线完全“包住”,则可以断言后者的性能优于前者;若两个学习器的ROC曲线发生交叉,则难以一般性地断言两者孰优孰劣。此时较为合理的判据是比较ROC曲面下的面积,即AUC(Area Under ROC Curve)。 示例演示 我们看看sklearn的官方例子: Receiver Operating Characteristic (ROC) with cross validation 。 """ ============================================================= Receiver Operating Characteristic (ROC) with cross validation =============================================================

利用dataset.columns为表格加表头

吃可爱长大的小学妹 提交于 2020-02-11 01:22:00
import pandas as pd import numpy as np dataset = pd . read_csv ( 'wdbc.csv' , header = None ) columns = [ 'mean radius' , 'mean texture' , 'mean perimeter' , 'mean area' , 'mean smoothness' , 'mean compactness' , 'mean concavity' , 'mean concave points' , 'mean symmetry' , 'mean fractal dimension' , 'radius error' , 'texture error' , 'perimeter error' , 'area error' , 'smoothness error' , 'compactness error' , 'concavity error' , 'concave points error' , 'symmetry error' , 'fractal dimension error' , 'worst radius' , 'worst texture' , 'worst perimeter' , 'worst area' , 'worst smoothness' ,

机器学习实例(六)美国波士顿地区房价预测

非 Y 不嫁゛ 提交于 2020-02-11 00:00:36
回归问题预测的目标是连续变量 数据描述 # 从sklearn.datasets导入波士顿房价数据读取器 from sklearn . datasets import load_boston # 从读取房价数据存储在变量boston中 boston = load_boston # 输出数据描述 boston . DESCR Number of Instances: 506 Number of Attributes: 13 numeric/categorical predictive.Median Value (attribute 14) is usually the target. Missing Attribute Values: None 由上述可知,该数据集共有506条美国波士顿地区房价的数据,每条数据包括对指定房屋的13项数值型特征描述和目标房价(平均值)。另外,该数据中没有缺失的属性/特征值 数据处理 from sklearn . model_selection import train_test_split import numpy as np X = boston . data y = boston . target # 随机采样25%的数据构建测试样本,其余作为训练样本 X_train , X_test , y_train , y_test = train_test