Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions protoflow/applications/glvq.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class _GLVQ(Network):
layer used by GLVQ-like models including GMLVQ and LVQMLN.

"""

def compile(self,
loss=None,
squashing="sigmoid_beta",
Expand Down Expand Up @@ -59,13 +60,15 @@ class GLVQ(_GLVQ):
prototype_initializer (str) : Method to use to set the initial
prototype locations. (default: "mean")
"""

def __init__(self,
nclasses,
input_dim,
prototypes_per_class=1,
prototype_initializer="zeros",
trainable_prototypes=True,
prototypes_dtype="float32",
prototype_constraint=None,
distance_fn=squared_euclidean_distance,
**kwargs):
super().__init__(**kwargs)
Expand All @@ -75,6 +78,7 @@ def __init__(self,
prototypes_per_class=prototypes_per_class,
prototype_initializer=prototype_initializer,
trainable_prototypes=trainable_prototypes,
prototype_constraint=prototype_constraint,
dtype=prototypes_dtype,
)
self.distance_fn = distance_fn
Expand Down
10 changes: 9 additions & 1 deletion protoflow/layers/prototypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

from protoflow.modules import initializers

from tensorflow.python.keras import constraints


class _Prototypes(tf.keras.layers.Layer):
"""Base class for Prototype layers in ProtoFlow."""

def __init__(self,
nclasses=None,
prototypes_per_class=1,
prototype_distribution=None,
prototype_initializer='zeros',
prototype_initializer="zeros",
prototype_constraint=None,
trainable_prototypes=True,
**kwargs):
if 'input_shape' not in kwargs and 'input_dim' in kwargs:
Expand All @@ -28,6 +32,7 @@ def __init__(self,
assert self.nclasses == len(prototype_distribution)
self.prototype_distribution = prototype_distribution
self.prototype_initializer = initializers.get(prototype_initializer)
self.prototype_constraint = constraints.get(prototype_constraint)
self.trainable_prototypes = trainable_prototypes

# Make a label list and flatten the list of lists using itertools
Expand Down Expand Up @@ -55,13 +60,15 @@ def get_config(self):

class Prototypes1D(_Prototypes):
"""Point Prototypes."""

def build(self, input_shape):
num_of_prototypes = sum(self.prototype_distribution)
self.prototypes = self.add_weight(
name='prototypes',
shape=(num_of_prototypes, input_shape[-1]),
dtype=self.dtype,
initializer=self.prototype_initializer,
constraint=self.prototype_constraint,
trainable=self.trainable_prototypes)
super().build(input_shape)

Expand All @@ -77,6 +84,7 @@ class AppendPrototypes1D(Prototypes1D):

`shape_transformation`: Callable that is to be applied to get a matrix.
"""

def __init__(self, shape_transformation=None, **kwargs):
self.shape_transformation = shape_transformation or tf.identity
super().__init__(**kwargs)
Expand Down