这是我尝试仅使用numpy和线性代数执行线性回归的尝试:
def linear_function(w , x , b):
return np.dot(w , x) + b
x = np.array([[1, 1,1],[0, 0,0]])
y = np.array([0,1])
w = np.random.uniform(-1,1,(1 , 3))
print(w)
learning_rate = .0001
xT = x.T
yT = y.T
for i in range(30000):
h_of_x = linear_function(w , xT , 1)
loss = h_of_x - yT
if i % 10000 == 0:
print(loss , w)
w = w + np.multiply(-learning_rate , loss)
linear_function(w , x , 1)
这会导致错误:
ValueError Traceback (most recent call last)
in ()
24 if i % 10000 == 0:
25 print(loss , w)
---> 26 w = w + np.multiply(-learning_rate , loss)
27
28 linear_function(w , x , 1)
ValueError: operands could not be broadcast together with shapes (1,3) (1,2)
这似乎可以减少训练集的维数:
import numpy as np
def linear_function(w , x , b):
return np.dot(w , x) + b
x = np.array([[1, 1],[0, 0]])
y = np.array([0,1])
w = np.random.uniform(-1,1,(1 , 2))
print(w)
learning_rate = .0001
xT = x.T
yT = y.T
for i in range(30000):
h_of_x = linear_function(w , xT , 1)
loss = h_of_x - yT
if i % 10000 == 0:
print(loss , w)
w = w + np.multiply(-learning_rate , loss)
linear_function(w , x , 1)
print(linear_function(w , x[0] , 1))
print(linear_function(w , x[1] , 1))
哪个返回:
[[ 0.68255806 -0.49717912]]
[[ 1.18537894 0. ]] [[ 0.68255806 -0.49717912]]
[[ 0.43605474 0. ]] [[-0.06676614 -0.49717912]]
[[ 0.16040755 0. ]] [[-0.34241333 -0.49717912]]
[ 0.05900769]
[ 1.]
[0.05900769]& [1.]接近培训示例,因此看来此实现是正确的.引发错误的实现有什么问题?我还没有实现2->的维度扩展. 3正确吗?