Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 Ind
 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 

7 Category of Lenses
 7.1 Definition
 7.2 Constructors
 7.3 Attributes
 7.4 Operations
 7.5 Optimizers
 7.6 Supported CAP Operations
 7.7 Examples
 7.8 GAP Categories

7 Category of Lenses

7.1 Definition

Let \(C\) be a cartesian category. The category of lenses \(\mathbf{Lenses}(C)\) has:

7.2 Constructors

7.2-1 CategoryOfLenses
‣ CategoryOfLenses( C )( operation )

Returns: a category

Construct the category of lenses over the category C. A lens is a pair of morphisms: a "get" morphism and a "put" morphism, which together model bidirectional data flow used in automatic differentiation.

7.2-2 ObjectConstructor
‣ ObjectConstructor( Lens, obj_list )( operation )

Returns: an object in the category of lenses

Construct an object in the category of lenses given a pair of objects.

7.2-3 MorphismConstructor
‣ MorphismConstructor( Lens, source_obj, morphism_list, target_obj )( operation )

Returns: a morphism in the category of lenses

Construct a morphism in the category of lenses given a pair of morphisms: a "get" morphism and a "put" morphism.

7.3 Attributes

7.3-1 UnderlyingPairOfObjects
‣ UnderlyingPairOfObjects( obj )( attribute )

Returns: a pair of objects

Returns the underlying pair of objects \((S_1, S_2)\) for an object in the category of lenses. This operation is a synonym for ObjectDatum.

7.3-2 UnderlyingPairOfMorphisms
‣ UnderlyingPairOfMorphisms( f )( attribute )

Returns: a pair of morphisms

Returns the underlying pair of morphisms for a morphism in the category of lenses. This operation is a synonym for MorphismDatum.

7.3-3 GetMorphism
‣ GetMorphism( f )( attribute )

Returns: a morphism

Returns the "get" morphism of a lens. For a lens \(f: (S_1, S_2) \to (T_1, T_2)\), the output is the get morphism \(S_1 \to T_1\).

7.3-4 PutMorphism
‣ PutMorphism( f )( attribute )

Returns: a morphism

Returns the "put" morphism of a lens. For a lens \(f: (S_1, S_2) \to (T_1, T_2)\), the output is the put morphism \(S_1 \times T_2 \to S_2\).

7.4 Operations

7.4-1 ReverseDifferentialLensFunctor
‣ ReverseDifferentialLensFunctor( Smooth, Lenses )( operation )

Returns: a functor

Embedding functor from the category Smooth of smooth maps into its category of lenses Lenses. An object \(\mathbb{R}^m\) in Smooth is mapped to the lens \((\mathbb{R}^m, \mathbb{R}^m)\), and a morphism \(f: \mathbb{R}^m \to \mathbb{R}^n\) is mapped to the lens with get morphism \(f: \mathbb{R}^m \to \mathbb{R}^n\) and put morphism \(Rf: \mathbb{R}^m \times \mathbb{R}^n \to \mathbb{R}^m\) given by \((u,v) \mapsto vJ_f(u)\), where \(J_f(u) \in \mathbb{R}^{n \times m}\) is the Jacobian matrix of \(f\) evaluated at \(u \in \mathbb{R}^m\). This functor might be defined for any cartesian reverse-differentiable category, but the category of smooth maps is the only such category currently implemented in this package. For example, if \(f: \mathbb{R}^2 \to \mathbb{R}\) is defined by \(f(x_1, x_2) = (x_1^3 + x_2^2)\), the the corresponding \(Rf:\mathbb{R}^2 \times \mathbb{R} \to \mathbb{R}^2\) is given by \(Rf((x_1, x_2), y) = y(3x_1^2, 2x_2) = (3x_1^2y, 2x_2y)\).

gap> Smooth := SkeletalCategoryOfSmoothMaps( );
SkeletalSmoothMaps
gap> Lenses := CategoryOfLenses( Smooth );
CategoryOfLenses( SkeletalSmoothMaps )
gap> R := ReverseDifferentialLensFunctor( Smooth, Lenses );
Embedding functor into category of lenses
gap> SourceOfFunctor( R );
SkeletalSmoothMaps
gap> RangeOfFunctor( R );
CategoryOfLenses( SkeletalSmoothMaps )
gap> f := DirectProductFunctorial( [ Smooth.Power(3), Smooth.Power(2) ] );
ℝ^2 -> ℝ^2
gap> Display( f );
ℝ^2 -> ℝ^2

‣ x1 ^ 3
‣ x2 ^ 2
gap> f := PreCompose( f, Smooth.Sum(2) );
ℝ^2 -> ℝ^1
gap> Display( f );
ℝ^2 -> ℝ^1

‣ x1 ^ 3 + x2 ^ 2
gap> Rf := ApplyFunctor( R, f );;
gap> Display( Rf );
(ℝ^2, ℝ^2) -> (ℝ^1, ℝ^1) defined by:

Get Morphism:
------------
ℝ^2 -> ℝ^1

‣ x1 ^ 3 + x2 ^ 2

Put Morphism:
------------
ℝ^3 -> ℝ^2

‣ x3 * (1 * (3 * x1 ^ 2) + 0)
‣ x3 * (0 + 1 * (2 * x2 ^ 1))

7.5 Optimizers

gap> Smooth := SkeletalCategoryOfSmoothMaps( );
SkeletalSmoothMaps
gap> Lenses := CategoryOfLenses( Smooth );
CategoryOfLenses( SkeletalSmoothMaps )
gap> optimizer := Lenses.GradientDescentOptimizer( :learning_rate := 0.01 )( 2 );
(ℝ^2, ℝ^2) -> (ℝ^2, ℝ^2) defined by:

Get Morphism:
------------
ℝ^2 -> ℝ^2

Put Morphism:
------------
ℝ^4 -> ℝ^2
gap> dummy_input := [ "theta_1", "theta_2", "g1", "g2" ];;
gap> dummy_input := CreateContextualVariables( dummy_input );
[ theta_1, theta_2, g1, g2 ]
gap> Display( optimizer : dummy_input := dummy_input );
(ℝ^2, ℝ^2) -> (ℝ^2, ℝ^2) defined by:

Get Morphism:
------------
ℝ^2 -> ℝ^2

‣ theta_1
‣ theta_2
Put Morphism:
------------
ℝ^4 -> ℝ^2

‣ theta_1 + 0.01 * g1
‣ theta_2 + 0.01 * g2
gap> optimizer := Lenses.GradientDescentWithMomentumOptimizer(
>             :learning_rate := 0.01, momentum := 0.9 )( 2 );
(ℝ^4, ℝ^4) -> (ℝ^2, ℝ^2) defined by:

Get Morphism:
------------
ℝ^4 -> ℝ^2

Put Morphism:
------------
ℝ^6 -> ℝ^4
gap> dummy_input := [ "s1", "s2", "theta_1", "theta_2", "g1", "g2" ];;
gap> dummy_input := CreateContextualVariables( dummy_input );
[ s1, s2, theta_1, theta_2, g1, g2 ]
gap> Display( optimizer : dummy_input := dummy_input );
(ℝ^4, ℝ^4) -> (ℝ^2, ℝ^2) defined by:

Get Morphism:
------------
ℝ^4 -> ℝ^2

‣ theta_1
‣ theta_2
Put Morphism:
------------
ℝ^6 -> ℝ^4

‣ 0.9 * s1 + 0.01 * g1
‣ 0.9 * s2 + 0.01 * g2
‣ theta_1 + (0.9 * s1 + 0.01 * g1)
‣ theta_2 + (0.9 * s2 + 0.01 * g2)
gap> optimizer := Lenses.AdagradOptimizer( :learning_rate := 0.01 )( 2 );
(ℝ^4, ℝ^4) -> (ℝ^2, ℝ^2) defined by:

Get Morphism:
------------
ℝ^4 -> ℝ^2

Put Morphism:
------------
ℝ^6 -> ℝ^4
gap> Display( optimizer : dummy_input := dummy_input );
(ℝ^4, ℝ^4) -> (ℝ^2, ℝ^2) defined by:

Get Morphism:
------------
ℝ^4 -> ℝ^2

‣ theta_1
‣ theta_2
Put Morphism:
------------
ℝ^6 -> ℝ^4

‣ s1 + g1 ^ 2
‣ s2 + g2 ^ 2
‣ theta_1 + 0.01 * g1 / (1.e-07 + Sqrt( s1 + g1 ^ 2 ))
‣ theta_2 + 0.01 * g2 / (1.e-07 + Sqrt( s2 + g2 ^ 2 ))
gap> optimizer := Lenses.AdamOptimizer(
>           :learning_rate := 0.01, beta_1 := 0.9, beta_2 := 0.999 )( 2 );
(ℝ^7, ℝ^7) -> (ℝ^2, ℝ^2) defined by:

Get Morphism:
------------
ℝ^7 -> ℝ^2
Put Morphism:
------------
ℝ^9 -> ℝ^7
gap> dummy_input :=
>   [ "t", "m1", "m2", "v1", "v2", "theta_1", "theta_2", "g1", "g2" ];;
gap> dummy_input := CreateContextualVariables( dummy_input );
[ t, m1, m2, v1, v2, theta_1, theta_2, g1, g2 ]
gap> Display( optimizer : dummy_input := dummy_input );
(ℝ^7, ℝ^7) -> (ℝ^2, ℝ^2) defined by:

Get Morphism:
------------
ℝ^7 -> ℝ^2

‣ theta_1
‣ theta_2
Put Morphism:
------------
ℝ^9 -> ℝ^7

‣ t + 1
‣ 0.9 * m1 + 0.1 * g1
‣ 0.9 * m2 + 0.1 * g2
‣ 0.999 * v1 + 0.001 * g1 ^ 2
‣ 0.999 * v2 + 0.001 * g2 ^ 2
‣ theta_1 + 0.01 /
 (1 - 0.999 ^ t) *
    ((0.9 * m1 + 0.1 * g1) /
        (1.e-07 + Sqrt( (0.999 * v1 + 0.001 * g1 ^ 2) / (1 - 0.999 ^ t) )))
‣ theta_2 + 0.01 /
 (1 - 0.999 ^ t) *
    ((0.9 * m2 + 0.1 * g2) /
        (1.e-07 + Sqrt( (0.999 * v2 + 0.001 * g2 ^ 2) / (1 - 0.999 ^ t) )))

7.6 Supported CAP Operations

7.6-1 Category of Lenses of Smooth Maps

The following CAP operations are supported:

7.7 Examples

gap> Smooth := SkeletalCategoryOfSmoothMaps( );
SkeletalSmoothMaps
gap> Lenses := CategoryOfLenses( Smooth );
CategoryOfLenses( SkeletalSmoothMaps )
gap> A := ObjectConstructor( Lenses, [ Smooth.( 1 ), Smooth.( 2 ) ] );
(ℝ^1, ℝ^2)
gap> IsWellDefined( A );
true
gap> CapCategory( A );
CategoryOfLenses( SkeletalSmoothMaps )
gap> A_datum := ObjectDatum( A );
[ ℝ^1, ℝ^2 ]
gap> CapCategory( A_datum[1] );
SkeletalSmoothMaps
gap> B := ObjectConstructor( Lenses, [ Smooth.( 3 ), Smooth.( 4 ) ] );
(ℝ^3, ℝ^4)
gap> get := RandomMorphism( Smooth.( 1 ), Smooth.( 3 ), 5 );
ℝ^1 -> ℝ^3
gap> put := RandomMorphism( Smooth.( 1 + 4 ), Smooth.( 2 ), 5 );
ℝ^5 -> ℝ^2
gap> f := MorphismConstructor( Lenses, A, [ get, put ], B );
(ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:

Get Morphism:
------------
ℝ^1 -> ℝ^3

Put Morphism:
------------
ℝ^5 -> ℝ^2
gap> MorphismDatum( f );
[ ℝ^1 -> ℝ^3, ℝ^5 -> ℝ^2 ]
gap> IsWellDefined( f );
true
gap> Display( f );
(ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:

Get Morphism:
------------
ℝ^1 -> ℝ^3

‣ 0.766 * x1 ^ 4 + 0.234
‣ 1. * x1 ^ 4 + 0.388
‣ 0.459 * x1 ^ 4 + 0.278

Put Morphism:
------------
ℝ^5 -> ℝ^2

‣ 0.677 * x1 ^ 5 + 0.19 * x2 ^ 4 + 0.659 * x3 ^ 4
+ 0.859 * x4 ^ 5 + 0.28 * x5 ^ 1 + 0.216
‣ 0.37 * x1 ^ 5 + 0.571 * x2 ^ 4 + 0.835 * x3 ^ 4
+ 0.773 * x4 ^ 5 + 0.469 * x5 ^ 1 + 0.159
gap> id_A := IdentityMorphism( Lenses, A );
(ℝ^1, ℝ^2) -> (ℝ^1, ℝ^2) defined by:

Get Morphism:
------------
ℝ^1 -> ℝ^1

Put Morphism:
------------
ℝ^3 -> ℝ^2
gap> Display( id_A );
(ℝ^1, ℝ^2) -> (ℝ^1, ℝ^2) defined by:

Get Morphism:
------------
ℝ^1 -> ℝ^1

‣ x1

Put Morphism:
------------
ℝ^3 -> ℝ^2

‣ x2
‣ x3
gap> IsCongruentForMorphisms( PreCompose( id_A, f ), f );
true
gap> TensorUnit( Lenses );
(ℝ^0, ℝ^0)
gap> TensorProductOnObjects( A, B );
(ℝ^4, ℝ^6)
gap> f1 := RandomMorphism( A, B, 5 );
(ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:

Get Morphism:
------------
ℝ^1 -> ℝ^3

Put Morphism:
------------
ℝ^5 -> ℝ^2
gap> f2 := RandomMorphism( A, B, 5 );
(ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:

Get Morphism:
------------
ℝ^1 -> ℝ^3

Put Morphism:
------------
ℝ^5 -> ℝ^2
gap> f3 := RandomMorphism( A, B, 5 );
(ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:

Get Morphism:
------------
ℝ^1 -> ℝ^3

Put Morphism:
------------
ℝ^5 -> ℝ^2
gap> f1_f2 := TensorProductOnMorphisms( Lenses, f1, f2 );
(ℝ^2, ℝ^4) -> (ℝ^6, ℝ^8) defined by:

Get Morphism:
------------
ℝ^2 -> ℝ^6

Put Morphism:
------------
ℝ^10 -> ℝ^4
gap> f2_f3 := TensorProductOnMorphisms( Lenses, f2, f3 );
(ℝ^2, ℝ^4) -> (ℝ^6, ℝ^8) defined by:

Get Morphism:
------------
ℝ^2 -> ℝ^6

Put Morphism:
------------
ℝ^10 -> ℝ^4
gap> t1 := TensorProductOnMorphisms( Lenses, f1_f2, f3 );
(ℝ^3, ℝ^6) -> (ℝ^9, ℝ^12) defined by:

Get Morphism:
------------
ℝ^3 -> ℝ^9

Put Morphism:
------------
ℝ^15 -> ℝ^6
gap> t2 := TensorProductOnMorphisms( Lenses, f1, f2_f3 );
(ℝ^3, ℝ^6) -> (ℝ^9, ℝ^12) defined by:

Get Morphism:
------------
ℝ^3 -> ℝ^9

Put Morphism:
------------
ℝ^15 -> ℝ^6
gap> IsCongruentForMorphisms( t1, t2 );
true
gap> Display( Braiding( A, B ) );
(ℝ^4, ℝ^6) -> (ℝ^4, ℝ^6) defined by:

Get Morphism:
------------
ℝ^4 -> ℝ^4

‣ x2
‣ x3
‣ x4
‣ x1

Put Morphism:
------------
ℝ^10 -> ℝ^6

‣ x9
‣ x10
‣ x5
‣ x6
‣ x7
‣ x8
gap> Display( PreCompose( Braiding( A, B ), BraidingInverse( A, B ) ) );
(ℝ^4, ℝ^6) -> (ℝ^4, ℝ^6) defined by:

Get Morphism:
------------
ℝ^4 -> ℝ^4

‣ x1
‣ x2
‣ x3
‣ x4

Put Morphism:
------------
ℝ^10 -> ℝ^6

‣ x5
‣ x6
‣ x7
‣ x8
‣ x9
‣ x10

7.8 GAP Categories

7.8-1 IsCategoryOfLenses
‣ IsCategoryOfLenses( arg )( filter )

Returns: true or false

The GAP category of a category of lenses.

7.8-2 IsObjectInCategoryOfLenses
‣ IsObjectInCategoryOfLenses( arg )( filter )

Returns: true or false

The GAP category of objects in a category of lenses.

7.8-3 IsMorphismInCategoryOfLenses
‣ IsMorphismInCategoryOfLenses( arg )( filter )

Returns: true or false

The GAP category of morphisms in a category of lenses.

 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 
Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 Ind

generated by GAPDoc2HTML