提交 40f0270a authored 作者: Olivier Breuleux's avatar Olivier Breuleux

merged

import gof import gof
from gof import current_mode, set_mode, build_mode, eval_mode, pop_mode, UNCOMPUTED, UNDEFINED, PythonR from gof import current_mode, set_mode, build_mode, eval_mode, build_eval_mode, pop_mode, UNCOMPUTED, UNDEFINED, PythonR
import numpy import numpy
...@@ -165,6 +165,7 @@ class NumpyR(gof.PythonR): ...@@ -165,6 +165,7 @@ class NumpyR(gof.PythonR):
self.data = value self.data = value
else: else:
self.data = numpy.array(value) self.data = numpy.array(value)
self.up_to_date = True
def __add__(self, y): return add(self, y) def __add__(self, y): return add(self, y)
def __radd__(self, x): return add(x, self) def __radd__(self, x): return add(x, self)
...@@ -208,14 +209,13 @@ zeros = wrap_producer(numpy.zeros) ...@@ -208,14 +209,13 @@ zeros = wrap_producer(numpy.zeros)
ones = wrap_producer(numpy.ones) ones = wrap_producer(numpy.ones)
# Wrapper to ensure that all inputs to the function impl have the same size (foils numpy's broadcasting) # Wrapper to ensure that all inputs to the function impl have the same size (foils numpy's broadcasting)
def assert_same_shapes(impl): def assert_same_shapes(impl):
def ret(x, *rest): def ret(x, *rest):
shape = x.shape shape = x.shape
for other in rest: for other in rest:
if other.shape != shape: if other.shape != shape:
raise TypeError("The dimensions of the inputs do not match.") raise ValueError("The dimensions of the inputs do not match.")
return impl(x, *rest) return impl(x, *rest)
return ret return ret
...@@ -223,7 +223,7 @@ def assert_same_shapes(impl): ...@@ -223,7 +223,7 @@ def assert_same_shapes(impl):
def tensor_scalar_op(impl): def tensor_scalar_op(impl):
def ret(x, a): def ret(x, a):
if a.shape: if a.shape:
raise TypeError("The second argument to %s must be a scalar." % impl) raise ValueError("The second argument to %s must be a scalar." % impl)
return impl(x, a) return impl(x, a)
return ret return ret
...@@ -240,7 +240,6 @@ class add_elemwise(proto_add_elemwise): ...@@ -240,7 +240,6 @@ class add_elemwise(proto_add_elemwise):
class iadd_elemwise(proto_add_elemwise, inplace): class iadd_elemwise(proto_add_elemwise, inplace):
impl = assert_same_shapes(numpy.ndarray.__iadd__) impl = assert_same_shapes(numpy.ndarray.__iadd__)
class proto_add_scalar(omega_op): class proto_add_scalar(omega_op):
def grad(x, a, gz): def grad(x, a, gz):
return gz, sum(gz) return gz, sum(gz)
...@@ -355,7 +354,6 @@ class div_elemwise(proto_div_elemwise): ...@@ -355,7 +354,6 @@ class div_elemwise(proto_div_elemwise):
class idiv_elemwise(proto_div_elemwise, inplace): class idiv_elemwise(proto_div_elemwise, inplace):
impl = assert_same_shapes(numpy.ndarray.__idiv__) impl = assert_same_shapes(numpy.ndarray.__idiv__)
def div_scalar_r(x, a): def div_scalar_r(x, a):
return scale(x, inv_elemwise(a)) return scale(x, inv_elemwise(a))
......
...@@ -156,8 +156,10 @@ class sigmoid(core.omega_op): ...@@ -156,8 +156,10 @@ class sigmoid(core.omega_op):
numpy.random.seed(1) numpy.random.seed(1)
core.build_eval_mode()
x = core.zeros((1, 10)) x = core.zeros((1, 10))
w = core.input(numpy.random.rand(10, 15)) w = core.input(numpy.random.rand(10, 15))
core.pop_mode()
# x = numpy.zeros((1, 10)) # x = numpy.zeros((1, 10))
# w = numpy.random.rand(10, 15) # w = numpy.random.rand(10, 15)
...@@ -188,6 +190,8 @@ f = compile.to_func([w, x], [w2, rec_error]) ...@@ -188,6 +190,8 @@ f = compile.to_func([w, x], [w2, rec_error])
#f = compile.single(w2, rec_error) #f = compile.single(w2, rec_error)
for i in dataset_1hot(x.data, numpy.ndarray((1, )), 10000): for i in dataset_1hot(x.data, numpy.ndarray((1, )), 10000):
# w.up_to_date = True
# x.up_to_date = True
w2, rec_error = f(w.data, x.data) w2, rec_error = f(w.data, x.data)
if not(i % 1000): if not(i % 1000):
print rec_error print rec_error
...@@ -223,24 +227,25 @@ print w.data ...@@ -223,24 +227,25 @@ print w.data
############################ ############################
x = core.ones((2, 2)) x = core.ones((2, 2))
y = core.zeros((1, 1)) y = core.zeros((1, 1))
#print "?", gof.graph.ops([], [x + y]) #print "?", gof.graph.ops([], [x + y])
print x # x + x
# print "1", gof.eval_env#.ops()
x + x # y + y
print "1", gof.eval_env#.ops() # print "2", gof.eval_env#.ops()
y + y # x + x
print "2", gof.eval_env#.ops() # print "3", gof.eval_env#.ops()
x + x
print "3", gof.eval_env#.ops()
x += (x + x) core.build_eval_mode()
x = core.ones((2, 2))
y = core.ones((2, 2)) * 2
x += y.T
# z = core.iadd(x, y)
# core.iadd(x, y)
print x print x
core.pop_mode()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论