生成隨機數語法速查
阿新 • • 發佈:2018-12-05
import tensorflow as tf
w1=tf.Variable(tf.random_uniform([2,2],-1,1))
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(w1))
輸出得
[[ 0.94702697 -0.19546461]
[-0.69034481 -0.10538507]]
n_features=7 w2=tf.Variable(tf.random_normal((n_features,1),mean=0.0,stddev=1.0),name='weights') with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) print(sess.run(w2))
輸出:
[[-0.6331901 ]
[ 2.41542029]
[-0.83030796]
[-1.55228913]
[-0.98885322]
[-0.63248575]
[ 0.26187277]]
def feature_normalize(data):
mu = np.mean(data,axis=0)
std = np.std(data,axis=0)
return (data - mu)/std
其中,np.std()的運算為
std = sqrt(mean(abs(x - x.mean())**2))
如果沒有指定axis,則是全部元素計算。
注意這裡涉及的向量運算是基於每個元素的運算。
std是標準差不是方差。
import numpy as np
ind = np.random.permutation(20)
print(ind)
輸出
[11 15 4 1 6 12 8 17 18 14 9 5 7 0 13 10 19 3 16 2]
import numpy as np import matplotlib.pyplot as plt s = np.random.poisson(5) print(s) s2 = np.random.poisson(5, 10000) count, bins, ignored = plt.hist(s2, 14, density=True) plt.show()
輸出:4