def test_backward(self): A = Square() B = Exp() C = Square() x = Variable(np.array(0.5)) a = A(x) b = B(a) y = C(b) y.grad = np.array(1.0) b.grad = C.backward(y.grad) a.grad = B.backward(b.grad) x.grad = A.backward(a.grad) print(x.grad)
def soy_showcase(self): # sanmartin sanmartin = Variable('soy/afascl') rosario = Variable('soy/bcr') chicago = Variable('soy/chicago') # foorecast soy sanmartin regression = TimeRegression(self.date_list, self.day, [rosario]) fx, _, rmse = regression.pattern() price = fx(regression.future_x) filename = draw(regression, 'graph_soy_rosario.png') self.tweet(('Estimación Soja Rosario para el' ' %s: AR$ %.f (RMSE: AR$ %.f)') % (self.day.strftime('%d-%m-%Y'), price, rmse), filename) regression = TimeRegression(self.date_list, self.day, [chicago]) fx, _, rmse = regression.pattern() price = fx(regression.future_x) filename = draw(regression, 'graph_soy_chicago.png') self.tweet(('Estimación Soja Chicago para el' ' %s: U$D %.f (RMSE: U$D %.f)') % (self.day.strftime('%d-%m-%Y'), price, rmse), filename) regression = TimeRegression(self.date_list, self.day, [sanmartin]) fx, _, rmse = regression.pattern() price = fx(regression.future_x) filename = draw(regression, 'graph_soy_sanmartin.png') self.tweet(('Estimación Soja puerto San Martín con descarga para el' ' %s: AR$ %.f (RMSE: AR$ %.f)') % (self.day.strftime('%d-%m-%Y'), price, rmse), filename) regression = VariableRegression(self.date_list, self.day, [chicago, sanmartin]) fx, _, rmse = regression.pattern() filename = draw(regression, 'graph_soy_related.png') price = fx(regression.future_x) x, y, dt = regression.data pearson = regression.pearson_correlation() m_dt = max(dt) m_dt = int_to_date(m_dt) if isinstance(m_dt, int) else m_dt try: self.tweet(('Correlación Soja Chicago con pto. San Martín hasta el' ' %s: AR$ %.f (RMSE: AR$ %.f, Pearson: %.2f%%)') % (m_dt.strftime('%d-%m-%Y'), price, rmse, pearson), filename) except AttributeError: pass self.tweet( 'El código puede ser descargado desde https://github.com/limiear/soyprice.', [])
def test_composite_function_differential(self): def f(x): A = Square() B = Exp() C = Square() return C(B(A(x))) x = Variable(np.array(0.5)) dy = numerical_diff(f, x) print(dy)
def test_composite(self): A = Square() B = Exp() C = Square() x = Variable(np.array(0.5)) a = A(x) b = B(a) y = C(b) print(y.data)
def test_function_chain_backward(self): A = Square() B = Exp() C = Square() x = Variable(np.array(0.5)) a = A(x) b = B(a) y = C(b) y.grad = np.array(1.0) C = y.creator b = C._input b.grad = C.backward(y.grad) B = b.creator a = B._input a.grad = B.backward(b.grad) A = a.creator x = A._input x.grad = A.backward(a.grad) print(x.grad)
def dollar_showcase(self): dollars = Variable('dollar/blue') regression = TimeRegression(self.date_list, self.day, [dollars]) fx, _, rmse = regression.pattern() price = fx(regression.future_x) filename = draw(regression, 'graph_dollar.png') try: rmse = int(rmse) print self.day, price, rmse, filename self.tweet(('Estimación Dollar Blue para el %s: AR$ %.2f ' '(RMSE: AR$ %.2f)') % (self.day.strftime('%d-%m-%Y'), price, rmse), filename) except ValueError: pass
def test_function_chain(self): A = Square() B = Exp() C = Square() x = Variable(np.array(0.5)) a = A(x) b = B(a) y = C(b) self.assertTrue(y.creator == C) self.assertTrue(y.creator._input == b) self.assertTrue(y.creator._input.creator == B) self.assertTrue(y.creator._input.creator._input == a) self.assertTrue(y.creator._input.creator._input.creator == A) self.assertTrue(y.creator._input.creator._input.creator._input == x)
def test_differential(self): f = Square() x = Variable(np.array(2.0)) dy = numerical_diff(f, x) print(dy)
def test_initialize(self): data = np.array(1.0) x = Variable(data) self.assertEqual(x.data, data)
def test_function(self): x = Variable(np.array(10)) f = Square() y = f(x) self.assertTrue(isinstance(y, Variable)) print(y.data)
from core import Variable from operation import * if __name__ == "__main__": a = Variable(1.0) b = add(a, a) c = square(a) d = add(a, b) e = add(c, d) e.backward() print(e.data) print(a.grad) a.clearGrad() print(a.grad)
from core import Variable from operation import * a = Variable(2) b = square(a) c = square(b) d = square(c) e = square(d) e.backward() print(a.grad)
from core import Variable from operation import * if __name__ == "__main__": a = Variable(1.0) b = add(a,a) b.backward() print(b.data) print(a.grad)