diff options
author | Edoardo Pasca <edo.paskino@gmail.com> | 2019-06-05 09:58:00 +0100 |
---|---|---|
committer | Edoardo Pasca <edo.paskino@gmail.com> | 2019-06-05 09:58:00 +0100 |
commit | 935361ba734c7a2ecae8835d5f6959d32f4c7403 (patch) | |
tree | 1ed582c72f66363b6b045aae40b5193306e8f8c1 | |
parent | e5d9351e87c92276677a0b26141049205e215860 (diff) | |
download | framework-935361ba734c7a2ecae8835d5f6959d32f4c7403.tar.gz framework-935361ba734c7a2ecae8835d5f6959d32f4c7403.tar.bz2 framework-935361ba734c7a2ecae8835d5f6959d32f4c7403.tar.xz framework-935361ba734c7a2ecae8835d5f6959d32f4c7403.zip |
fixed test
-rwxr-xr-x | Wrappers/Python/ccpi/framework/VectorData.py | 2 | ||||
-rwxr-xr-x | Wrappers/Python/ccpi/framework/framework.py | 3 | ||||
-rwxr-xr-x | Wrappers/Python/ccpi/optimisation/algorithms/FISTA.py | 10 | ||||
-rwxr-xr-x | Wrappers/Python/wip/fix_test.py | 39 |
4 files changed, 35 insertions, 19 deletions
diff --git a/Wrappers/Python/ccpi/framework/VectorData.py b/Wrappers/Python/ccpi/framework/VectorData.py index fdce3a5..d0fed10 100755 --- a/Wrappers/Python/ccpi/framework/VectorData.py +++ b/Wrappers/Python/ccpi/framework/VectorData.py @@ -54,5 +54,5 @@ class VectorData(DataContainer): out = array else: raise ValueError('Incompatible size: expecting {} got {}'.format((self.length,), array.shape)) - deep_copy = False + deep_copy = True super(VectorData, self).__init__(out, deep_copy, None) diff --git a/Wrappers/Python/ccpi/framework/framework.py b/Wrappers/Python/ccpi/framework/framework.py index b972be6..f91343c 100755 --- a/Wrappers/Python/ccpi/framework/framework.py +++ b/Wrappers/Python/ccpi/framework/framework.py @@ -329,7 +329,7 @@ class DataContainer(object): self.dimension_labels = {} self.geometry = None # Only relevant for AcquisitionData and ImageData - if dimension_labels is not None and \ + if dimension_labels != {} and \ len (dimension_labels) == self.number_of_dimensions: for i in range(self.number_of_dimensions): self.dimension_labels[i] = dimension_labels[i] @@ -632,6 +632,7 @@ class DataContainer(object): def pixel_wise_binary(self, pwop, x2, *args, **kwargs): out = kwargs.get('out', None) + if out is None: if isinstance(x2, (int, float, complex)): out = pwop(self.as_array() , x2 , *args, **kwargs ) diff --git a/Wrappers/Python/ccpi/optimisation/algorithms/FISTA.py b/Wrappers/Python/ccpi/optimisation/algorithms/FISTA.py index db4e8b7..4754d9c 100755 --- a/Wrappers/Python/ccpi/optimisation/algorithms/FISTA.py +++ b/Wrappers/Python/ccpi/optimisation/algorithms/FISTA.py @@ -62,28 +62,18 @@ class FISTA(Algorithm): def update(self): self.f.gradient(self.y, out=self.u) - print ('update, self.u' , self.u.as_array()) self.u.__imul__( -self.invL ) self.u.__iadd__( self.y ) - print ('update, self.u' , self.u.as_array()) - # x = g.prox(u,invL) - print ('update, self.x pre prox' , self.x.as_array()) self.g.proximal(self.u, self.invL, out=self.x) - print ('update, self.x post prox' , self.x.as_array()) self.t = 0.5*(1 + numpy.sqrt(1 + 4*(self.t_old**2))) self.x.subtract(self.x_old, out=self.y) - print ('update, self.y' , self.y.as_array()) - self.y.__imul__ ((self.t_old-1)/self.t) - print ('update, self.x' , self.x.as_array()) self.y.__iadd__( self.x ) - print ('update, self.y' , self.y.as_array()) self.x_old.fill(self.x) - print ('update, self.x_old' , self.x_old.as_array()) self.t_old = self.t def update_objective(self): diff --git a/Wrappers/Python/wip/fix_test.py b/Wrappers/Python/wip/fix_test.py index 7b19910..094f571 100755 --- a/Wrappers/Python/wip/fix_test.py +++ b/Wrappers/Python/wip/fix_test.py @@ -60,11 +60,11 @@ class Norm1(Function): opt = {'memopt': True} # Problem data. -m = 3 -n = 3 +m = 30 +n = 30 np.random.seed(1) -#Amat = np.asarray( np.random.randn(m, n), dtype=numpy.float32) -Amat = np.asarray(np.eye(m), dtype=np.float32) * 2 +Amat = np.asarray( np.random.randn(m, n), dtype=numpy.float32) +#Amat = np.asarray(np.eye(m), dtype=np.float32) * 2 A = LinearOperatorMatrix(Amat) bmat = np.asarray( np.random.randn(m), dtype=numpy.float32) bmat *= 0 @@ -92,8 +92,15 @@ g0 = ZeroFunction() #f.L = 30.003 x_init = vgx.allocate(VectorGeometry.RANDOM, dtype=numpy.float32) +a = VectorData(x_init.as_array(), deep_copy=True) + +assert id(x_init.as_array()) != id(a.as_array()) + +#%% f.L = LinearOperator.PowerMethod(A, 25, x_init)[0] * 2 print ('f.L', f.L) +rate = (1 / f.L) / 3 +f.L *= 6 # Initial guess #x_init = DataContainer(np.zeros((n, 1))) @@ -102,6 +109,7 @@ print ('b', b.as_array()) # Create 1-norm object instance g1_new = lam * L1Norm() g1 = Norm1(lam) + g1 = ZeroFunction() #g1(x_init) x = g1.prox(x_init, 1/f.L ) @@ -112,6 +120,16 @@ print ("g1.proximal ", x.as_array()) x = g1_new.proximal(x_init, 0.03 ) print ("g1.proximal ", x.as_array()) +x1 = vgx.allocate(VectorGeometry.RANDOM, dtype=numpy.float32) +pippo = vgx.allocate() + +print ("x_init", x_init.as_array()) +print ("x1", x1.as_array()) +a = x_init.subtract(x1, out=pippo) + +print ("pippo", pippo.as_array()) +print ("x_init", x_init.as_array()) +print ("x1", x1.as_array()) # Combine with least squares and solve using generic FISTA implementation @@ -120,8 +138,14 @@ def callback(it, objective, solution): print (it, objective, solution.as_array()) fa = FISTA(x_init=x_init, f=f, g=g1) -fa.max_iteration = 10 -fa.run(3, callback = callback, verbose=False) +fa.max_iteration = 1000 +fa.update_objective_interval = int( fa.max_iteration / 10 ) +fa.run(fa.max_iteration, callback = None, verbose=True) + +gd = GradientDescent(x_init=x_init, objective_function=f, rate = rate ) +gd.max_iteration = 100000 +gd.update_objective_interval = 10000 +gd.run(gd.max_iteration, callback = None, verbose=True) # Print for comparison @@ -130,4 +154,5 @@ print(fa.get_output().as_array()) print(fa.get_last_objective()) print (A.direct(fa.get_output()).as_array()) -print (b.as_array())
\ No newline at end of file +print (b.as_array()) +print (A.direct(gd.get_output()).as_array()) |