提交 3eb2781b authored 作者: Olivier Breuleux's avatar Olivier Breuleux

bla

上级 51b37947
...@@ -131,53 +131,69 @@ import grad ...@@ -131,53 +131,69 @@ import grad
############################ ############################
# def dataset_1hot(x, targ, n): def dataset_1hot(x, targ, n):
# """Return an looping iterator over 1-hot vectors """Return an looping iterator over 1-hot vectors
# This function is a generator for the integers range(n) that works by This function is a generator for the integers range(n) that works by
# side-effect on the numpy ndarray mat. side-effect on the numpy ndarray mat.
# On each iteration, mat is set (in-place) to the next element of an infinite On each iteration, mat is set (in-place) to the next element of an infinite
# sequence of 1-hot vectors. sequence of 1-hot vectors.
# """ """
# assert targ.size == 1 assert targ.size == 1
# for i in xrange(n): for i in xrange(n):
# idx = i % x.shape[1] idx = i % x.shape[1]
# x[:] = 0 x[:] = 0
# x[0,idx] = 1 x[0,idx] = 1
# targ[0] = idx targ[0] = idx
# yield i yield i
# class sigmoid(core.omega_op): class sigmoid(core.omega_op):
# def impl(x): def impl(x):
# return 1.0 / (1.0 + numpy.exp(-x)) return 1.0 / (1.0 + numpy.exp(-x))
# def grad(x, gz): def grad(x, gz):
# return gz * sigmoid(x) * (1 - sigmoid(x)) return gz * sigmoid(x) * (1 - sigmoid(x))
# 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))
# #print x.data, w.data x = numpy.zeros((1, 10))
w = numpy.random.rand(10, 15)
# def autoassociator(w, x): #print x.data, w.data
# forward = sigmoid(core.dot(sigmoid(core.dot(x, w)), w.T))
# rec_error = core.sum(core.sqr(x - forward)) import inspect
# w -= 0.1 * grad.grad(rec_error, w)
# return w, rec_error def omega_compile(f):
args, varargs, kwargs, defaults = inspect.getargspec(f)
assert not varargs
assert not kwargs
def ret(*args):
outputs = core.build(f, *args)
return compile.prog(args, outputs)
return ret
@omega_compile
def autoassociator(w, x):
forward = sigmoid(core.dot(sigmoid(core.dot(x, w)), w.T))
rec_error = core.sum(core.sqr(x - forward))
w -= 0.1 * grad.grad(rec_error, w)
return w, rec_error
# w2, rec_error = core.build(autoassociator, w, x) # w2, rec_error = core.build(autoassociator, w, x)
# f = compile.to_func([w, x], [w2, rec_error]) # #f = compile.to_func([w, x], [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):
# 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
# print "done!" print "done!"
# print w.data print w.data
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论