【知识点】np.random.randn()

目录

一、基本知识

1. np.random.randn()

2. 正态分布

二、具体实例

三、项目应用


一、基本知识

1. np.random.randn()

numpy.random.randn(d0,d1,…,dn)

(1) randn函数返回一个或一组样本,具有标准正态分布。

(2) dn表格每个维度

(3) 返回值为指定维度的array

2. 正态分布

标准正态分布介绍

(1)标准正态分布—-standard normal distribution

(2)标准正态分布又称为u分布,是以0为均值、以1为标准差的正态分布,记为N(0,1)。


二、具体实例

代码:

import numpy as np

x = np.random.randn(2, 4)
b = np.random.randn(3, 2, 4)
print(f'x is {x}')
print()
print(f'b is {b}')

结果:

x is [[ 0.91998142  1.35255229  0.61680609 -0.90104357]
 [-1.1394227   0.16248028  1.31023715 -0.35412562]]

b is [[[-0.9556731  -0.99616581 -0.19022034 -0.83921712]
  [-0.98856744 -0.77866624 -0.36667162 -1.69963599]]

 [[-1.31725326 -0.22468372 -1.93575552  0.28267799]
  [-0.75844531  0.42178096 -0.63655538 -1.88670984]]

 [[ 0.09319706 -0.36167653 -0.74885749 -0.60621931]
  [-1.05559154  0.09121703 -0.85993236 -0.20603581]]]

三、项目应用

代码:

import numpy as np
import matplotlib.pyplot as plt


def sigmoid(x):
    return 1 / (1 + np.exp(-x))


def ReLU(x):
    return np.maximum(0, x)


def tanh(x):
    return np.tanh(x)
    
input_data = np.random.randn(1000, 100)  # 1000个数据
node_num = 100  # 各隐藏层的节点(神经元)数
hidden_layer_size = 5  # 隐藏层有5层
activations = {}  # 激活值的结果保存在这里

x = input_data

for i in range(hidden_layer_size):
    if i != 0:
        x = activations[i-1]  
        #当i=0时,activations是空的,所以不执行该语句
        #当i=1时,activations有上一轮输出的z,此时让x=上一轮的z

    # 改变初始值进行实验!
    w = np.random.randn(node_num, node_num) * 1
    # w = np.random.randn(node_num, node_num) * 0.01
    # w = np.random.randn(node_num, node_num) * np.sqrt(1.0 / node_num)
    # w = np.random.randn(node_num, node_num) * np.sqrt(2.0 / node_num)


    a = np.dot(x, w)


    # 将激活函数的种类也改变,来进行实验!
    z = sigmoid(a)
    # z = ReLU(a)
    # z = tanh(a)

    activations[i] = z

# 绘制直方图
for i, a in activations.items():
    plt.subplot(1, len(activations), i+1)
    plt.title(str(i+1) + "-layer")
    if i != 0: plt.yticks([], [])
    # plt.xlim(0.1, 1)
    # plt.ylim(0, 7000)
    plt.hist(a.flatten(), 30, range=(0,1))
plt.show()

主要看这部分:

(1)*1表示标准差为1的高斯函数

(2)*0.01表示标准差为0.01的高斯函数 

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rachel MuZy

你的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值