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
import numpy as np
mask=np.random.randint(3, size=(256, 256))
生成256×256整数标签<3。