numpy.random.RandomState函数用法
-
numpy.random.RandomState()是一个伪随机数生成器。那么伪随机数是什么呢?
-
伪随机数是用确定性的算法计算出来的似来自[0,1]均匀分布的随机数序列。并不真正的随机,但具有类似于随机数的统计特征,如均匀性、独立性等
这样输出一样
rng = np.random.RandomState(0)
rng.rand(4)
Out[377]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
rng = np.random.RandomState(0)
rng.rand(4)
Out[379]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
这样输出不一样,要用rng生成
np.random.RandomState(0)
Out[397]: <mtrand.RandomState at 0xddaa288>
np.random.rand(4)
Out[398]: array([0.62395295, 0.1156184 , 0.31728548, 0.41482621])
np.random.RandomState(0)
Out[399]: <mtrand.RandomState at 0xddaac38>
np.random.rand(4)
Out[400]: array([0.86630916, 0.25045537, 0.48303426, 0.98555979])
np.random.seed()
np.random.seed(0)
np.random.rand(4,3)
Out[362]:
array([[0.5488135 , 0.71518937, 0.60276338],
[0.54488318, 0.4236548 , 0.64589411],
[0.43758721, 0.891773 , 0.96366276],
[0.38344152, 0.79172504, 0.52889492]])
np.random.seed(0)
np.random.rand(4,3)
Out[364]:
array([[0.5488135 , 0.71518937, 0.60276338],
[0.54488318, 0.4236548 , 0.64589411],
[0.43758721, 0.891773 , 0.96366276],
[0.38344152, 0.79172504, 0.52889492]])
random.randn()和random.rand()
- numpy.random.randn(d0,d1,…,dn)是从标准正态分布中返回一个或多个样本值。
- numpy.random.rand(d0,d1,…,dn) 的随机样本位于[0,1) 中
- 两个函数中两个参数是代表生成的矩阵的维度
import numpy as np
In [22]: data = np.random.randn(3,4)
In [23]: data
Out[23]:
array([[-0.34341494, -0.01541249, -0.18014056, -1.30215008],
[ 0.82040243, -0.92564691, 0.79424176, -0.10651544],
[-0.18457542, 0.87839392, -1.72359517, 1.24179385]])
In [25]: data2 = np.random.rand(3,4)
In [26]: data2
Out[26]:
array([[0.09158799, 0.53545735, 0.58871176, 0.80192998],
[0.28538348, 0.72147261, 0.16966679, 0.43919518],
[0.04111255, 0.86852787, 0.33768262, 0.60136455]])
tensorflow中实现正则化
loss(w) = tf.contrib.layers.l1_regularizer(REGULARIZER)(w)#L1正则化
loss(w) = tf.contrib.layers.l2_regularizer(REGULARIZER)(w)#L2正则化
tf.add_to_collection('losses',tf.contrib.layers.l1_regularizer(REGULARIZER)(w))
#把内容加到集合对应位置
loss = cem + tf.add_n(tf.get_collection('losses'))#cem为损失函数,add_n为列表元素相加
- 实例
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 9 09:59:25 2019
@author: zzw
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
batch_size = 30
seed = 2
rdm = np.random.RandomState(seed)
X = rdm.randn(300,2)
Y_ = [int(x0*x0+x1*x1 <2) for (x0,x1) in X]
Y_c = [['red' if y else 'blue'] for y in Y_]
X = np.vstack(X).reshape(-1,2)
Y_ = np.vstack(Y_).reshape(-1,1)
plt.scatter(X[:,0],X[:,1],c = np.squeeze(Y_c))
plt.show()
def get_weight(shape,regularizer):
w = tf.Variable(tf.random_normal(shape),dtype = tf.float32)
tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(regularizer)(w))
return w
def get_bias(shape):
b = tf.Variable(tf.constant(0.01,shape = shape))
return b
x = tf.placeholder(tf.float32,[None,2])
y_ = tf.placeholder(tf.float32,[None,1])
w1 = get_weight([2,11],0.01)
b1 = get_bias([11])
y1 = tf.nn.relu(tf.matmul(x,w1)+b1)
w2 = get_weight([11,1],0.01)
b2 = get_bias([1])
y = tf.matmul(y1,w2)+b2
loss_mse = tf.reduce_mean(tf.square(y-y_))
loss_total = loss_mse + tf.add_n(tf.get_collection('losses'))
train_step = tf.train.AdamOptimizer(0.0001).minimize(loss_total)
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
STEPS = 40000
for i in range(STEPS):
start = (i*batch_size)%300
end = start + batch_size
sess.run(train_step,feed_dict = {x:X[start:end],y_:Y_[start:end]})
if i %2000 == 0:
loss_mse_v = sess.run(loss_mse,feed_dict = {x:X,y_:Y_})
print('after %d steps ,loss is: %f'%(i,loss_mse_v))
xx,yy = np.mgrid[-3:3:0.01,-3:3:0.01]
grid = np.c_[xx.ravel(),yy.ravel()]
probs = sess.run(y,feed_dict={x:grid})
probs = probs.reshape(xx.shape)
plt.scatter(X[:,0],X[:,1],c = np.squeeze(Y_c))
plt.contour(xx,yy,probs,levels = [0.5])
plt.show()
matplotlib安装
- pip install matplotlib.pyplot会报错,用pip install matplotlib
np.stack() ,hstack(),vstack()
- stack()
- vstack() 以方括号为单位垂直方向叠加
import numpy as np
a=[1,2,3]
b=[4,5,6]
print(np.vstack((a,b)))
输出:
[[1 2 3]
[4 5 6]]
import numpy as np
a=[[1],[2],[3]]
b=[[1],[2],[3]]
c=[[1],[2],[3]]
d=[[1],[2],[3]]
print(np.vstack((a,b,c,d)))
输出:
[[1]
[2]
[3]
[1]
[2]
[3]
[1]
[2]
[3]
[1]
[2]
[3]]
- hstack()以方括号为单位水平方向叠加
import numpy as np
a=[1,2,3]
b=[4,5,6]
print(np.hstack((a,b)))
输出:[1 2 3 4 5 6 ]
import numpy as np
a=[[1],[2],[3]]
b=[[1],[2],[3]]
c=[[1],[2],[3]]
d=[[1],[2],[3]]
print(np.hstack((a,b,c,d)))
输出:
[[1 1 1 1]
[2 2 2 2]
[3 3 3 3]]
np.squeeze()
- 从数组的形状中删除单维条目,即把shape中为1的维度去掉
np.squeeze(x)
Out[30]: array([0, 1, 2])
x = np.array([[[0], [1], [2]]])
x.shape
Out[32]: (1, 3, 1)
x1 = np.squeeze(x)
x1
Out[34]: array([0, 1, 2])
x1.shape
Out[35]: (3,)
全局变量和局部变量
def spam():
print(eggs)
eggs = 'spam'
eggs = 'global'
spam()
会出现错误,因为在函数中有针对eggs的赋值,因此认为eggs是局部变量。