<commit_before>from typing import cast, Any, Callable, Iterable, Optional, List

import tensorflow as tf

from typeguard import check_argument_types
from neuralmonkey.nn.projection import multilayer_projection
from neuralmonkey.dataset import Dataset
from neuralmonkey.model.model_part import ModelPart, FeedDict
from neuralmonkey.checking import assert_shape

# tests: lint, mypy

# pylint: disable=too-many-instance-attributes


class SequenceRegressor(ModelPart):
    """A simple MLP regression over encoders.

    The API pretends it is an RNN decoder which always generates a sequence of
    length exactly one.
    """
    # pylint: disable=too-many-arguments
    def __init__(self,
                 name: str,
                 encoders: List[Any],
                 data_id: str,
                 layers: Optional[List[int]] = None,
                 activation_fn: Callable[[tf.Tensor], tf.Tensor]=tf.tanh,
                 dropout_keep_prob: float = 0.5,
                 save_checkpoint: Optional[str] = None,
                 load_checkpoint: Optional[str] = None) -> None:
        ModelPart.__init__(self, name, save_checkpoint, load_checkpoint)
        assert check_argument_types()

        self.encoders = encoders
        self.data_id = data_id
        self.layers = layers
        self.activation_fn = activation_fn
        self.dropout_keep_prob = dropout_keep_prob
        self.max_output_len = 1

        with tf.variable_scope(name):
            self.learning_step = tf.get_variable(
                "learning_step", [], trainable=False,
                initializer=tf.constant_initializer(0))

            self.dropout_placeholder = \
                tf.placeholder(tf.float32, name="dropout_plc")
            self.gt_inputs = tf.placeholder(tf.float32, shape=[None],
                                            name="targets")

            mlp_input = tf.concat([enc.encoded for enc in encoders], 1)
            # TODO extend it to output into multidimensional space
            layers.append(1)
            mlp = multilayer_projection(
                mlp_input, layers, activation=self.activation_fn,
                dropout_plc=self.dropout_placeholder)

            assert_shape(mlp, [-1, 1])

            self.predicted = mlp
            self.cost = tf.reduce_mean(tf.square(mlp - self.gt_inputs))

            tf.summary.scalar(
                'val_optimization_cost', self.cost,
                collections=["summary_val"])
            tf.summary.scalar(
                'train_optimization_cost',
                self.cost, collections=["summary_train"])
    # pylint: enable=too-many-arguments

    @property
    def train_loss(self):
        return self.cost

    @property
    def runtime_loss(self):
        return self.cost<commit_msg>bug corrected<commit_after>from typing import cast, Any, Callable, Iterable, Optional, List

import tensorflow as tf

from typeguard import check_argument_types
from neuralmonkey.nn.projection import multilayer_projection
from neuralmonkey.dataset import Dataset
from neuralmonkey.model.model_part import ModelPart, FeedDict
from neuralmonkey.checking import assert_shape

# tests: lint, mypy

# pylint: disable=too-many-instance-attributes


class SequenceRegressor(ModelPart):
    """A simple MLP regression over encoders.

    The API pretends it is an RNN decoder which always generates a sequence of
    length exactly one.
    """
    # pylint: disable=too-many-arguments
    def __init__(self,
                 name: str,
                 encoders: List[Any],
                 data_id: str,
                 layers: Optional[List[int]] = None,
                 activation_fn: Callable[[tf.Tensor], tf.Tensor]=tf.tanh,
                 dropout_keep_prob: float = 0.5,
                 save_checkpoint: Optional[str] = None,
                 load_checkpoint: Optional[str] = None) -> None:
        ModelPart.__init__(self, name, save_checkpoint, load_checkpoint)
        assert check_argument_types()

        self.encoders = encoders
        self.data_id = data_id
        self.layers = layers
        self.activation_fn = activation_fn
        self.dropout_keep_prob = dropout_keep_prob
        self.max_output_len = 1

        with tf.variable_scope(name):
            self.learning_step = tf.get_variable(
                "learning_step", [], trainable=False,
                initializer=tf.constant_initializer(0))

            self.dropout_placeholder = \
                tf.placeholder(tf.float32, name="dropout_plc")
            self.gt_inputs = tf.placeholder(tf.float32, shape=[None],
                                            name="targets")

            mlp_input = tf.concat([enc.encoded for enc in encoders], 1)
            # TODO extend it to output into multidimensional space
            layers.append(1)
            mlp = multilayer_projection(
                mlp_input, layers, activation=self.activation_fn,
                dropout_plc=self.dropout_placeholder)

            assert_shape(mlp, [-1, 1])

            self.predicted = mlp
            self.cost = tf.reduce_mean(
                tf.square(mlp - tf.expand_dims(self.gt_inputs, 1)))

            tf.summary.scalar(
                'val_optimization_cost', self.cost,
                collections=["summary_val"])
            tf.summary.scalar(
                'train_optimization_cost',
                self.cost, collections=["summary_train"])
    # pylint: enable=too-many-arguments

    @property
    def train_loss(self):
        return self.cost

    @property
    def runtime_loss(self):
        return self.cost
{'fraction_non_alphanumeric': 0.08500717360114778, 'fraction_numerical': 0.0044835007173601145, 'mean_word_length': 2.1597733711048157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2639849', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1512, 'n_words': 346}