Zamiast:
$$ A x = b$$
rozwiązujemy
$$ A^T A x = A^T b.$$
Inaczej mówiąc, niech błąd:
$$ r=b-A x$$
leży w lewym jądrze operatora A:
$$A^T r =A^T( b-Ax) = A^T b-A^TAx = 0.$$
$$ A^T A x = A^T b.$$
Można też zarządać znikania gradientu kwadratu odchylenia:
$$ \frac{\partial}{\partial x_k} (A_{ij} x_j - b_i) (A_{il} x_l - b_i) = 0$$$$ A_{ij} \delta_{jk} (A_{il} x_l - b_i) + A_{il} \delta_{lk} (A_{ij} x_j - b_i) =0 $$ $$ A^TAx - A^T b + A^TAx - A^T b =0 $$ $$ A^TAx - A^T b =0 $$
In [1]:
%matplotlib notebook
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
import numpy as np
import matplotlib.pyplot as plt
In [2]:
learning_rate = 0.01
training_epochs = 1000
display_step = 50
In [3]:
train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]
Macierz $A$ dla regresji liniowej wynosi:
In [4]:
import numpy as np
M = np.vstack([np.ones_like(train_X),train_X]).T
M
Out[4]:
In [5]:
print (np.dot(M.T,M))
print(np.dot(M.T,train_Y))
Współczynniki dokładnie będą wynosiły:
In [6]:
c = np.linalg.solve(np.dot(M.T,M),np.dot(M.T,train_Y))
c
Out[6]:
In [7]:
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, c[1] * train_X + c[0], label='Fitted line')
plt.legend()
Out[7]:
In [8]:
plt.close()
Nie zakładamy, że mamy problem regresji liniowej.
Używamy: https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/optimize.html
In [9]:
from scipy.optimize import minimize
In [10]:
def cost(c,x=train_X,y=train_Y):
return sum( (c[0]+x_*c[1]-y_)**2 for (x_,y_) in zip(x,y) )
In [11]:
cost([1,2])
Out[11]:
In [12]:
res = minimize(cost, [1,1], method='nelder-mead', options={'xtol': 1e-8, 'disp': True})
In [13]:
res.x
Out[13]:
In [ ]:
In [ ]:
In [14]:
x = np.linspace(-2,2,77)
y = np.linspace(-2,2,77)
X,Y = np.meshgrid(x,y)
In [15]:
cost([X,Y]).shape
Out[15]:
In [16]:
plt.contourf( X,Y,np.log(cost([X,Y])),cmap='gray')
Out[16]:
In [17]:
plt.plot(res.x[0],res.x[1],'o')
Out[17]:
In [18]:
np.min(cost([X,Y]))
Out[18]:
In [19]:
px=[]
py=[]
for i in range(20):
res = minimize(cost, [1,1], options={ 'maxiter':i})
px.append(res.x[0])
py.append(res.x[1])
print(res.x)
In [20]:
plt.plot(px,py,'ro-')
Out[20]:
In [ ]:
In [21]:
import sympy
from sympy.abc import x,y
sympy.init_printing(use_latex='mathjax')
In [22]:
f_symb = cost([x,y]).expand()
In [23]:
f_symb.diff(x)
Out[23]:
In [24]:
F = sympy.lambdify((x,y),f_symb,np)
Fx = sympy.lambdify((x,y),f_symb.diff(x),np)
Fy = sympy.lambdify((x,y),f_symb.diff(y),np)
F(1,1),cost([1,1])
Out[24]:
In [25]:
x0,y0 = -1,1
In [ ]:
In [26]:
h = 0.01/(2*17)
for i in range(500):
plt.plot(x0,y0,'go')
#print(i,x0,y0)
x0 += -h * Fx(x0,y0)
y0 += -h * Fy(x0,y0)
In [ ]:
In [27]:
# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")
# Set model weights
W = tf.Variable(1.0, name="weight")
b = tf.Variable(1.0, name="bias")
In [28]:
# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)
In [29]:
# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
In [30]:
# Initializing the variables
init = tf.global_variables_initializer()
In [31]:
# TEST
with tf.Session() as sess:
sess.run(init)
sess.run(tf.assign(W,1.0))
sess.run(tf.assign(b,2.0))
print(sess.run(b),sess.run(cost, feed_dict={X: train_X, Y: train_Y}))
In [32]:
# Launch the graph
x_tf_lst = []
y_tf_lst = []
with tf.Session() as sess:
sess.run(init)
# Fit all training data
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={X: x, Y: y})
#Display logs per epoch step
if (epoch+1) % display_step == 0:
c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
print ("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
"W=", sess.run(W), "b=", sess.run(b))
x_tf_lst.append(sess.run(b))
y_tf_lst.append(sess.run(W))
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
In [33]:
plt.plot(x_tf_lst,y_tf_lst,'yo')
Out[33]:
In [ ]:
In [ ]: