Ich möchte Gradientabstieg verwenden, um Gleichungssatz zu lösen, aber ich habe jedes Mal ein falsches Ergebnis, also überprüfe ich meinen Code und schreibe eine numme Ausgabe, in dieser Ausgabe biete ich expliziten Verlustgradienten an und kann ein korrektes Ergebnis erhalten .GradientDescentOptimizer hat ein falsches Ergebnis
Also ich verstehe nicht, warum GradientDescentOptimizer nicht funktionieren kann.
hier ist mein Code ohne tf:
import numpy as np
class SolveEquation:
def __init__(self, rate: float, loss_threshold: float=0.0001, max_epochs: int=1000):
self.__rate = rate
self.__loss_threshold = loss_threshold
self.__max_epochs = max_epochs
self.__x = None
def solve(self, coefficients, b):
_a = np.array(coefficients)
_b = np.array(b).reshape([len(b), 1])
_x = np.zeros([_a.shape[1], 1])
for epoch in range(self.__max_epochs):
grad_loss = np.matmul(np.transpose(_a), np.matmul(_a, _x) - _b)
_x -= self.__rate * grad_loss
if epoch % 10 == 0:
loss = np.mean(np.square(np.subtract(np.matmul(_a, _x), _b)))
print('loss = {:.8f}'.format(loss))
if loss < self.__loss_threshold:
break
return _x
s = SolveEquation(0.1, max_epochs=1)
print(s.solve([[1, 2], [1, 3]], [3, 4]))
Und hier ist mein Code mit tf:
import tensorflow as tf
import numpy as np
class TFSolveEquation:
def __init__(self, rate: float, loss_threshold: float=0.0001, max_epochs: int=1000):
self.__rate = rate
self.__loss_threshold = tf.constant(loss_threshold)
self.__max_epochs = max_epochs
self.__session = tf.Session()
self.__x = None
def __del__(self):
try:
self.__session.close()
finally:
pass
def solve(self, coefficients, b):
coefficients_data = np.array(coefficients)
b_data = np.array(b)
_a = tf.placeholder(tf.float32)
_b = tf.placeholder(tf.float32)
_x = tf.Variable(tf.zeros([coefficients_data.shape[1], 1]))
loss = tf.reduce_mean(tf.square(tf.matmul(_a, _x) - _b))
optimizer = tf.train.GradientDescentOptimizer(self.__rate)
model = optimizer.minimize(loss)
self.__session.run(tf.global_variables_initializer())
for epoch in range(self.__max_epochs):
self.__session.run(model, {_a: coefficients_data, _b: b_data})
if epoch % 10 == 0:
if self.__session.run(loss < self.__loss_threshold, {_a: coefficients_data, _b: b_data}):
break
return self.__session.run(_x)
s = TFSolveEquation(0.1, max_epochs=1)
print(s.solve([[1, 2], [1, 3]], [3, 4]))
ich diese 2-Codes mit sehr einfachen Gleichung Satz testen:
x_1 + 2 * x_2 = 3
x_1 + 3 * x_3 = 4
loss = 1/2 * || Ax - b ||^2
Init x_1 = 0, x_2 = 0, rate = 0.1
Verwenden Gradientenabfallsaktualisierung So am 1. Rechen-, das Delta x = (0,7, 1,8)
Aber leider mit tf meinem Code die ohne tf
delta x =
[[ 0.69999999]
[ 1.75 ]]
Und mein Code geben geben die
Absolut Code ohne tf ist richtig, aber warum tf comput Gradient weniger als 0,05 dann richtige Ergebnis? Ich denke, das ist der Grund, dass mein Code ohne tf den Gleichungssatz lösen kann, aber tf Ausgabe kann Gleichungssatz zur Zeit nicht lösen.
Kann mir jemand sagen, warum tf geben einen ansteigenden Gradiant? Dank
Meine Plattform ist Win10 + tensorflow-gpu v1.0