写在前面:使用Python的一些小问题
查看Python安装路径方法:使用命令py -0p
1. 矢量化
import time
import numpy as np
x1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]
x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]
tic = time.process_time()
###矩阵乘法 如果是向量相乘 结果是内奇数;矩阵相乘结果按照矩阵乘法计算
dot = np.dot(x1,x2)
toc = time.process_time()
print("dot = " + str(dot) +"\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")
tic = time.process_time()
###计算向量的外积
outer = np.outer(x1,x2)
toc = time.process_time()
print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")
2. L1 和 L2 损失函数
L1损失函数:
import numpy as np
def L1(yhat,y):
loss = np.sum(np.abs(y-yhat))
return loss
yhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1, 0, 0, 1, 1])
print("L1 = " + str(L1(yhat,y)))
L2损失函数:
import numpy as np
def L2(yhat,y):
###numpy.power()用于数组元素求n次方求和
loss = np.sum(np.power((y-yhat),2))
return loss
yhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1, 0, 0, 1, 1])
print("L2 = " + str(L2(yhat,y)))
3. python中的zero用法
用法:zeros(shape, dtype=float, order=‘C’)
返回:返回来一个给定形状和类型的用0填充的数组;
4.初始化参数
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
def initialize_with_zeros(dim):
w = np.zeros((dim,1))
b = 0
### 为真的时候运行
assert(w.shape == (dim,1))
assert(isinstance(b, float) or isinstance(b, int))
return w, b
dim = 2
w, b = initialize_with_zeros(dim)
print ("w = " + str(w))
print ("b = " + str(b))
5.正向传播
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
def sigmoid(z):
"""
Compute the sigmoid of z
Arguments:
z -- A scalar or numpy array of any size.
Return:
s -- sigmoid(z)
"""
### START CODE HERE ### (≈ 1 line of code)
s = 1.0/(1+np.exp(-z))
### END CODE HERE ###
return s
def propagate(w,b,X,Y):
m = X.shape[1]
###表示矩阵的转置
A = sigmoid(np.dot(w.T,X)+b)
cost = -(1.0/m)*np.sum(Y*np.log(A)+(1-Y)*np.log(1-A))
dw = (1.0/m)*np.dot(X,(A-Y).T)
db = (1.0/m)*np.sum(A-Y)
assert(dw.shape == w.shape)
assert(db.dtype == float)
# 从数组的形状中删除单维条目,即把shape中为1的维度去掉
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = {"dw": dw,
"db": db}
return grads, cost
w, b, X, Y = np.array([[1.],[2.]]), 2., np.array([[1.,2.,-1.],[3.,4.,-3.2]]), np.array([[1,0,1]])
grads, cost = propagate(w, b, X, Y)
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))
6 python实现梯度下降
import random
import matplotlib.pyplot as plt
#x轴对应的点
_x = [i/100 for i in range(100)]
#y轴对应的点
_y = [3*e+4+random.random() for e in _x]
# c for b in a
# 首先for b in a 很好理解:我们从a里面取出每一个迭代对象b。
# 然后我们对这个b进行c操作,并且以c操作后的形式输出一个列表。
plt.show()
w = random.random()
b = random.random()
plt.ion()
for i in range(30):
for x,y in zip(_x,_y):
z = x*w+b
o = z-y
loss = o**2
dw = -2*o*x
db = -2*o
w = w + 0.1*dw
b = b+0.1*db
print(w,b,loss)
# 清除其他的活動軸
plt.cla()
plt.plot(_x,_y,".")
v = [w*e+b for e in _x]
plt.plot(_x,v)
plt.pause(0.01)
plt.ioff()
plt.show()