Skip to content

Commit

Permalink
refactor numpy enoder
Browse files Browse the repository at this point in the history
  • Loading branch information
jlibovicky committed Mar 12, 2018
1 parent 2fc5613 commit c778f5f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
1 change: 0 additions & 1 deletion neuralmonkey/encoders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .cnn_encoder import CNNEncoder
from .cnn_encoder import CNNTemporalView
from .numpy_encoder import VectorEncoder
from .raw_rnn_encoder import RawRNNEncoder
from .recurrent import FactoredEncoder
from .recurrent import RecurrentEncoder
Expand Down
36 changes: 11 additions & 25 deletions neuralmonkey/encoders/numpy_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable=too-few-public-methods


class VectorEncoder(ModelPart, Stateful):
class StatefulNumberEncoder(ModelPart, Stateful):

def __init__(self,
name: str,
Expand Down Expand Up @@ -59,13 +59,13 @@ def feed_dict(self, dataset: Dataset, train: bool = False) -> FeedDict:
return {self.vector: dataset.get_series(self.data_id)}


class PostCNNImageEncoder(ModelPart, SpatialStatefulWithOutput):
class SpatialNumpyEncoder(ModelPart, SpatialStatefulWithOutput):
# pylint: disable=too-many-arguments
def __init__(self,
name: str,
input_shape: List[int],
output_shape: int,
data_id: str,
output_shape: int = None,
save_checkpoint: Optional[str] = None,
load_checkpoint: Optional[str] = None,
initializers: InitializerSpecs = None) -> None:
Expand All @@ -74,40 +74,26 @@ def __init__(self,
initializers)

assert len(input_shape) == 3
if output_shape <= 0:
if output_shape is not None and output_shape <= 0:
raise ValueError("Output vector dimension must be postive.")

self.data_id = data_id

with self.use_scope():
features_shape = [None] + input_shape # type: ignore
self.image_features = tf.placeholder(tf.float32,
shape=features_shape,
name="image_input")

self.flat = tf.reduce_mean(self.image_features,
axis=[1, 2],
name="average_image")

self.project_w = get_variable(
name="img_init_proj_W",
shape=[input_shape[2], output_shape],
initializer=tf.glorot_normal_initializer())
self.project_b = get_variable(
name="img_init_b", shape=[output_shape],
initializer=tf.zeros_initializer())
self.input_shape = input_shape

@tensor
def output(self) -> tf.Tensor:
return tf.tanh(tf.matmul(self.flat, self.project_w) + self.project_b)
return tf.reduce_mean(
self.spatial_states, axis=[1, 2], name="average_image")

@tensor
def spatial_states(self) -> tf.Tensor:
return self.image_features
features_shape = [None] + self.input_shape # type: ignore
return tf.placeholder(
tf.float32, shape=features_shape, name="spatial_states")

@tensor
def spatial_mask(self) -> tf.Tensor:
return tf.ones(tf.shape(self.spatial_states)[:3])

def feed_dict(self, dataset: Dataset, train: bool = False) -> FeedDict:
return {self.image_features: dataset.get_series(self.data_id)}
return {self.spatial_states: dataset.get_series(self.data_id)}

0 comments on commit c778f5f

Please sign in to comment.