深度学习中的13种概率分布

1 概率分布概述

d3d991a320f84158872f0b73a7346cbe.png

  • 共轭意味着它有共轭分布的关系。

在贝叶斯概率论中,如果后验分布 p(θx)与先验概率分布 p(θ)在同一概率分布族中,则先验和后验称为共轭分布,先验称为似然函数的共轭先验。

  • 多分类表示随机方差大于 2。

  • n 次意味着我们也考虑了先验概率 p(x)。

2 分布概率与特征

2.1 均匀分布(连续)

均匀分布在 [a,b] 上具有相同的概率值,是简单概率分布。

示例代码:

import numpy as np
from matplotlib import pyplot as plt

def uniform(x, a, b):

    y = [1 / (b - a) if a <= val and val <= b
                    else 0 for val in x]

    return x, y, np.mean(y), np.std(y)

x = np.arange(-100, 100) # define range of x
for ls in [(-50, 50), (10, 20)]:
    a, b = ls[0], ls[1]
    x, y, u, s = uniform(x, a, b)
    plt.plot(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f$' % (u, s))

plt.legend()
plt.show()

运行代码显示:

b5e207c3f6b546f9981f9560f7a5b4d9.png

2.2 伯努利分布(离散)

  • 先验概率 p(x)不考虑伯努利分布。因此,如果我们对最大似然进行优化,那么我们很容易被过度拟合。

  • 利用二元交叉熵对二项分类进行分类。它的形式与伯努利分布的负对数相同。

示例代码:

import random
import numpy as np
from matplotlib import pyplot as plt

def bernoulli(p, k):
    return p if k else 1 - p

n_experiment = 100
p = 0.6
x = np.arange(n_experiment)
y = []
for _ in range(n_experiment):
    pick = bernoulli(p, k=bool(random.getrandbits(1)))
    y.append(pick)

u, s = np.mean(y), np.std(y)
plt.scatter(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f$' % (u, s))
plt.legend()
plt.show()

运行代码显示:

6638381a82cb4bd0b7204fb0360163b1.png

2.3 二项分布(离散)

  • 参数为 n 和 p 的二项分布是一系列 n 个独立实验中成功次数的离散概率分布。

  • 二项式分布是指通过指定要提前挑选的数量而考虑先验概率的分布。

示例代码:

import numpy as np
from matplotlib import pyplot as plt

import operator as op
from functools import reduce

def const(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer / denom

def binomial(n, p):
    q = 1 - p
    y = [const(n, k) * (p ** k) * (q ** (n-k)) for k in range(n)]
    return y, np.mean(y), np.std(y)

for ls in [(0.5, 20), (0.7, 40), (0.5, 40)]:
    p, n_experiment = ls[0], ls[1]
    x = np.arange(n_experiment)
    y, u, s = binomial(n_experiment, p)
    plt.scatter(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f$' % (u, s))

plt.legend()
plt.show()

运行代码显示:

30555ee71170430fae90af850f5123ec.png

2.4 多伯努利分布,分类分布(离散)

  • 多伯努利称为分类分布。

  • 交叉熵和采取负对数的多伯努利分布具有相同的形式。

示例代码:

import random
import numpy as np
from matplotlib import pyplot as plt

def categorical(p, k):
    return p[k]

n_experiment = 100
p = [0.2, 0.1, 0.7]
x = np.arange(n_experiment)
y = []
for _ in range(n_experiment):
    pick = categorical(p, k=random.randint(0, len(p) - 1))
    y.append(pick)

u, s = np.mean(y), np.std(y)
plt.scatter(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f$' % (u, s))
plt.legend()
plt.show()

运行代码显示:

7ee7365db9864858be3292a53422043b.png

2.5 多项式分布(离散)

多项式分布与分类分布的关系与伯努尔分布与二项分布的关系相同。

示例代码:

import numpy as np
from matplotlib import pyplot as plt

import operator as op
from functools import reduce

def factorial(n):
    return reduce(op.mul, range(1, n + 1), 1)

def const(n, a, b, c):
    """
        return n! / a! b! c!, where a+b+c == n
    """
    assert  a + b + c == n

    numer = factorial(n)
    denom = factorial(a) * factorial(b) * factorial(c)
    return numer / denom

def multinomial(n):
    """
    :param x : list, sum(x) should be `n`
    :param n : number of trial
    :param p: list, sum(p) should be `1`
    """
    # get all a,b,c where a+b+c == n, a<b<c
    ls = []
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            for k in range(j, n + 1):
                if i + j + k == n:
                    ls.append([i, j, k])

    y = [const(n, l[0], l[1], l[2]) for l in ls]
    x = np.arange(len(y))
    return x, y, np.mean(y), np.std(y)

for n_experiment in [20, 21, 22]:
    x, y, u, s = multinomial(n_experiment)
    plt.scatter(x, y, label=r'$trial=%d$' % (n_experiment))

plt.legend()
plt.show()

运行代码显示:

21b9679b43de462694db82542372a452.png

2.6 β分布(连续)

  • β分布与二项分布和伯努利分布共轭。

  • 利用共轭,利用已知的先验分布可以更容易地得到后验分布。

  • 当β分布满足特殊情况(α=1,β=1)时,均匀分布是相同的。

示例代码:

import numpy as np
from matplotlib import pyplot as plt

def gamma_function(n):
    cal = 1
    for i in range(2, n):
        cal *= i
    return cal

def beta(x, a, b):

    gamma = gamma_function(a + b) / \
            (gamma_function(a) * gamma_function(b))
    y = gamma * (x ** (a - 1)) * ((1 - x) ** (b - 1))
    return x, y, np.mean(y), np.std(y)

for ls in [(1, 3), (5, 1), (2, 2), (2, 5)]:
    a, b = ls[0], ls[1]

    # x in [0, 1], trial is 1/0.001 = 1000
    x = np.arange(0, 1, 0.001, dtype=np.float)
    x, y, u, s = beta(x, a=a, b=b)
    plt.plot(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f,'
                         r'\ \alpha=%d,\ \beta=%d$' % (u, s, a, b))
plt.legend()
plt.show()

运行代码显示:

3626bfcc60bc403e8de5a04e80d28254.png

2.7 Dirichlet 分布(连续)

  • dirichlet 分布与多项式分布是共轭的。

  • 如果 k=2,则为β分布。

示例代码:

from random import randint
import numpy as np
from matplotlib import pyplot as plt

def normalization(x, s):
    """
    :return: normalizated list, where sum(x) == s
    """
    return [(i * s) / sum(x) for i in x]

def sampling():
    return normalization([randint(1, 100),
            randint(1, 100), randint(1, 100)], s=1)

def gamma_function(n):
    cal = 1
    for i in range(2, n):
        cal *= i
    return cal

def beta_function(alpha):
    """
    :param alpha: list, len(alpha) is k
    :return:
    """
    numerator = 1
    for a in alpha:
        numerator *= gamma_function(a)
    denominator = gamma_function(sum(alpha))
    return numerator / denominator

def dirichlet(x, a, n):
    """
    :param x: list of [x[1,...,K], x[1,...,K], ...], shape is (n_trial, K)
    :param a: list of coefficient, a_i > 0
    :param n: number of trial
    :return:
    """
    c = (1 / beta_function(a))
    y = [c * (xn[0] ** (a[0] - 1)) * (xn[1] ** (a[1] - 1))
         * (xn[2] ** (a[2] - 1)) for xn in x]
    x = np.arange(n)
    return x, y, np.mean(y), np.std(y)

n_experiment = 1200
for ls in [(6, 2, 2), (3, 7, 5), (6, 2, 6), (2, 3, 4)]:
    alpha = list(ls)

    # random samping [x[1,...,K], x[1,...,K], ...], shape is (n_trial, K)
    # each sum of row should be one.
    x = [sampling() for _ in range(1, n_experiment + 1)]

    x, y, u, s = dirichlet(x, alpha, n=n_experiment)
    plt.plot(x, y, label=r'$\alpha=(%d,%d,%d)$' % (ls[0], ls[1], ls[2]))

plt.legend()
plt.show()

运行代码显示:

a2cb19e178b54e59884804f8d84c3334.png

2.8 伽马分布(连续)

  • 如果 gamma(a,1)/gamma(a,1)+gamma(b,1)与 beta(a,b)相同,则 gamma 分布为β分布。

  • 指数分布和卡方分布是伽马分布的特例。

代码示例:

import numpy as np
from matplotlib import pyplot as plt

def gamma_function(n):
    cal = 1
    for i in range(2, n):
        cal *= i
    return cal

def gamma(x, a, b):
    c = (b ** a) / gamma_function(a)
    y = c * (x ** (a - 1)) * np.exp(-b * x)
    return x, y, np.mean(y), np.std(y)

for ls in [(1, 1), (2, 1), (3, 1), (2, 2)]:
    a, b = ls[0], ls[1]

    x = np.arange(0, 20, 0.01, dtype=np.float)
    x, y, u, s = gamma(x, a=a, b=b)
    plt.plot(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f,'
                         r'\ \alpha=%d,\ \beta=%d$' % (u, s, a, b))
plt.legend()
plt.show()

运行代码显示:

1009001a4a754f21947db41db2d12c0b.png

2.9 指数分布(连续)

指数分布是 α 为 1 时 γ 分布的特例。

import numpy as np
from matplotlib import pyplot as plt

def exponential(x, lamb):
    y = lamb * np.exp(-lamb * x)
    return x, y, np.mean(y), np.std(y)

for lamb in [0.5, 1, 1.5]:

    x = np.arange(0, 20, 0.01, dtype=np.float)
    x, y, u, s = exponential(x, lamb=lamb)
    plt.plot(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f,'
                         r'\ \lambda=%d$' % (u, s, lamb))
plt.legend()
plt.show()

运行代码显示

c97ec2c0d4f44ade97e214e78cf70650.png

2.10 高斯分布(连续)

高斯分布是一种非常常见的连续概率分布。

示例代码:

import numpy as np
from matplotlib import pyplot as plt

def gaussian(x, n):
    u = x.mean()
    s = x.std()

    # divide [x.min(), x.max()] by n
    x = np.linspace(x.min(), x.max(), n)

    a = ((x - u) ** 2) / (2 * (s ** 2))
    y = 1 / (s * np.sqrt(2 * np.pi)) * np.exp(-a)

    return x, y, x.mean(), x.std()

x = np.arange(-100, 100) # define range of x
x, y, u, s = gaussian(x, 10000)

plt.plot(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f$' % (u, s))
plt.legend()
plt.show()

运行代码显示:

72168899350446a3b6e2748244a26d6f.png

2.11 标准正态分布(连续)

标准正态分布为特殊的高斯分布,平均值为 0,标准差为 1。

import numpy as np
from matplotlib import pyplot as plt

def normal(x, n):
    u = x.mean()
    s = x.std()

    # normalization
    x = (x - u) / s

    # divide [x.min(), x.max()] by n
    x = np.linspace(x.min(), x.max(), n)

    a = ((x - 0) ** 2) / (2 * (1 ** 2))
    y = 1 / (s * np.sqrt(2 * np.pi)) * np.exp(-a)

    return x, y, x.mean(), x.std()

x = np.arange(-100, 100) # define range of x
x, y, u, s = normal(x, 10000)

plt.plot(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f$' % (u, s))
plt.legend()
plt.show()

运行代码显示:

1d47e619ea3145dba3d6b45ef956ea93.png

2.12 卡方分布(连续)

  • k 自由度的卡方分布是 k 个独立标准正态随机变量的平方和的分布。

  • 卡方分布是 β 分布的特例

示例代码:

import numpy as np
from matplotlib import pyplot as plt

def gamma_function(n):
    cal = 1
    for i in range(2, n):
        cal *= i
    return cal

def chi_squared(x, k):

    c = 1 / (2 ** (k/2)) * gamma_function(k//2)
    y = c * (x ** (k/2 - 1)) * np.exp(-x /2)

    return x, y, np.mean(y), np.std(y)

for k in [2, 3, 4, 6]:
    x = np.arange(0, 10, 0.01, dtype=np.float)
    x, y, _, _ = chi_squared(x, k)
    plt.plot(x, y, label=r'$k=%d$' % (k))

plt.legend()
plt.show()

运行代码显示

45847e204210461a94099453422c31d2.png

2.13 t 分布(连续)

t 分布是对称的钟形分布,与正态分布类似,但尾部较重,这意味着它更容易产生远低于平均值的值。

示例代码:

import numpy as np
from matplotlib import pyplot as plt

def gamma_function(n):
    cal = 1
    for i in range(2, n):
        cal *= i
    return cal

def student_t(x, freedom, n):

    # divide [x.min(), x.max()] by n
    x = np.linspace(x.min(), x.max(), n)

    c = gamma_function((freedom + 1) // 2) \
        / np.sqrt(freedom * np.pi) * gamma_function(freedom // 2)
    y = c * (1 + x**2 / freedom) ** (-((freedom + 1) / 2))

    return x, y, np.mean(y), np.std(y)

for freedom in [1, 2, 5]:

    x = np.arange(-10, 10) # define range of x
    x, y, _, _ = student_t(x, freedom=freedom, n=10000)
    plt.plot(x, y, label=r'$v=%d$' % (freedom))

plt.legend()
plt.show()

运行代码显示

84ec7b8d6794491fa4b904b23840fcd9.png

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/242870.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Python实现多种图像锐化方法:拉普拉斯算子和Sobel算子

Python实现多种图像锐化方法&#xff1a;拉普拉斯算子和Sobel算子 图像和视频逐渐成为人们生活中信息获取的重要来源&#xff0c;而图像和视频在传输过程中有很多因素可能造成图像模糊&#xff0c;比如不正确的聚焦会产生离焦模糊&#xff0c;景物和照相机的相对运动会造成运动…

C# 编写Windows服务程序

1.什么是windows服务&#xff1f; Microsoft Windows 服务&#xff08;即&#xff0c;以前的 NT 服务&#xff09;使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序。这些服务可以在计算机启动时自动启动&#xff0c;可以暂停和重新启动而且不显示任何用…

基于自动化脚本批量上传依赖到nexus内网私服

前言 因为某些原因某些企业希望私服是不能连接外网的&#xff0c;所以需要某些开源依赖需要我们手动导入到nexus中&#xff0c;尽管nexus为我们提供了web页面。但是一个个手动导入显然是一个庞大的工程。 对此我们就不妨基于脚本的方式实现这一过程。 预期效果 笔者本地仓库…

IDEA之设置主题风格为eclipse风格

设置IDEA的主题风格为eclipse风格&#xff0c;步骤如下: 1.选择File->Settings 2.选择 Plugins 3.搜索 eclipse theme&#xff0c;注意是红框里的&#xff0c;点击 install 下载后就会自动设置这个主题 4.你也可以去修改主题&#xff0c;选择 Appearance&#xff0c;设置th…

deepstream-python安装

​ 安装deepstream-docker 在这边文章中deepstream-docker详细介绍了如何在Ubuntu下安装deepstream-docker&#xff0c;安装完成之后&#xff0c;为了快速入门deepstream&#xff0c;我们可以安装deepstream-python库&#xff0c;通过阅读相应的例子来快速搭建一个应用。 安…

认识loader和plugin

在 webpack 中&#xff0c;专注于处理 webpack 在编译过程中的某个特定的任务的功能模块&#xff0c;可以称为插件。它和 loader 有以下区别&#xff1a; 1loader 是一个转换器&#xff0c;将 A 文件进行编译成 B 文件&#xff0c;比如&#xff1a;将 A.less 转换为 A.css&…

IDEA之设置项目包的结构层级为eclipse默认样式

idea默认项目包的结构层级如下: 想修改成eclipse默认的那种样式&#xff0c;设置步骤如下: 1.点击下图中红框图标进行设置 2.选择 Tree Appearance&#xff0c;取消勾选 Compact Middle Packages 3.勾选红框里的两个选项&#xff0c;Flatten Packages 和 Hide Empty Middle Pa…

HTML插入视频和音频(详解)

&#x1f4cd;文章目录&#x1f4cd; &#x1f9c0;一&#xff0c;简介&#x1f9c0;二&#xff0c;视频(video)&#x1f367;1&#xff0c;普通的视频插入&#x1f367;2&#xff0c;在html5中嵌入视频网站视频 &#x1f9c0;三&#xff0c;音频(audio) &#x1f9c0;一&#…

50mA、24V、超低 IQ、低压降稳压器

一、Description The TPS715 low-dropout (LDO) voltage regulators offer the benefits of high input voltage, low-dropout voltage, low-power operation, and miniaturized packaging. The devices, which operate over an input range of 2.5 V to 24 V, are stable wit…

Wordle 游戏实现 - 使用 C++ Qt

标题&#xff1a;Wordle 游戏实现 - 使用 C Qt 摘要&#xff1a; Wordle 是一款文字猜词游戏&#xff0c;玩家需要根据给定的单词猜出正确的答案&#xff0c;并在限定的次数内完成。本文介绍了使用 C 和 Qt 框架实现 Wordle 游戏的基本思路和部分代码示例。 引言&#xff1a;…

jmeter简单压测kafka

前言 这也是一个笔记&#xff0c;就是计划用jmeter做性能测试&#xff0c;但是这里是只要将数据放到kafka的topic里&#xff0c;后面查看下游业务处理能力。 一、方案 因为只要实现数据放到kafka&#xff0c;参考了下博友的方案&#xff0c;可行。 二、方案验证 详细过程就不…

CNN 卷积神经网络之 DenseNet 网络的分类统一项目(包含自定义数据集的获取)

1. DenseNet 网络介绍 本章实现的项目是DenseNet 网络对花数据集的五分类&#xff0c;下载链接&#xff1a; 基于迁移学习的 DenseNet 图像分类项目 DenseNet 网络是在 ResNet 网络上的改进&#xff0c;大概的网络结构如下&#xff1a; 1.1 卷积的简单介绍 图像识别任务主要…

计算机速成课Crash Course - 10. 早期的编程方式

今天继续计算机速成课Crash Course的系列讲解。 更多技术文章&#xff0c;关注公众号 “摸鱼IT” 锁定 -上午11点 - &#xff0c;感谢大家关注、转发、点赞&#xff01; 10. 早期的编程方式 前几集我们把重点放在计算机的原理&#xff0c;怎么从内存读写数据&#xff0c;执行…

js基础:函数、对象、WebAPIs-DOM

一、函数和对象 1、函数概述 &#x1f916;chatgpt&#xff1a;什么是函数&#xff1f;为什么要有函数&#xff1f; 函数是一种可重复使用的代码块&#xff0c;它们可以接受输入&#xff08;参数&#xff09;、执行特定的任务&#xff0c;并返回结果。 JavaScript中函数是非常…

鸿蒙OS应用开发之按钮组件(2)

前面学习了简单的按钮添加到程序里,并且使用了简单的布局排列来放置。其实按钮还有很多种形式,会在不同的场合来使用。 默认的按钮外形,跟前面例子的程序是一样的: 包含着图片的按钮: 不同外形的按钮:

Python编程进阶:轻松掌握多线程和多进程

大家好&#xff0c;今天我们将讨论如何利用Python执行多线程和多进程任务。它们提供了在单个进程或多个进程之间执行并发操作的方法&#xff0c;并行和并发执行可以提高系统的速度和效率。在讨论多线程和多进程的基础知识之后&#xff0c;我们还将讨论使用Python库实现它们的实…

利用poi实现将数据库表字段信息导出到word中

研发文档对于开发人员来说都不陌生了&#xff0c;而研发文档里重要的一部分就是表结构设计&#xff0c;需要我们在word建个表格把我们数据库中的表字段信息填进去&#xff0c;表多的话靠我们手动去填非常累人&#xff01;&#xff01;&#xff01; 因此作为开发人员可不可以写…

计算机网络应用层(期末、考研)

计算机网络总复习链接&#x1f517; 目录 DNS域名服务器域名解析过程分类递归查询&#xff08;给根域名服务器造成的负载过大&#xff0c;实际中几乎不用&#xff09;迭代查询 域名缓存&#xff08;了解即可&#xff09;完整域名解析过程采用UDP服务 FTP控制连接与数据连接 电…

Flutter Dart FFI Pointer<Uint8>类型如何转成数组或String

前言 继上一次发布的 Flutter 直接调用so动态库&#xff0c;或调用C/C源文件内函数 内容&#xff0c;最终我选择了第二种方式&#xff0c;直接把整个 Native C 的项目源代码放进了 Flutter 工程里编译&#xff08;放在iOS的目录是因为它不支持自定义源码路径&#xff0c;Andro…

Linux免密实现文件拷贝(建立机器之间的SSH密钥认证)

背景&#xff1a; 在之前的工作中&#xff0c;我需要在我的shell脚本中实现将机器A的文件拷贝至机器B&#xff0c;然后去执行一系列的操作。由于我将我想要执行的动作完全写入了shell脚本中&#xff0c;并且不想每次执行时都去输入密码&#xff0c;因此这里&#xff0c;我们需要…
最新文章