def _mlp(self) -> MultilayerPerceptron:
     mlp_input = tf.concat([enc.output for enc in self.encoders], 1)
     return MultilayerPerceptron(mlp_input,
                                 self.layers,
                                 self.dropout_keep_prob,
                                 len(self.vocabulary),
                                 activation_fn=self.activation_fn,
                                 train_mode=self.train_mode)
    def __init__(self,
                 name: str,
                 encoders: List[Stateful],
                 vocabulary: Vocabulary,
                 data_id: str,
                 layers: List[int],
                 activation_fn: Callable[[tf.Tensor], tf.Tensor] = tf.nn.relu,
                 dropout_keep_prob: float = 0.5,
                 reuse: ModelPart = None,
                 save_checkpoint: str = None,
                 load_checkpoint: str = None,
                 initializers: InitializerSpecs = None) -> None:
        """Construct a new instance of the sequence classifier.

        Args:
            name: Name of the decoder. Should be unique accross all Neural
                Monkey objects
            encoders: Input encoders of the decoder
            vocabulary: Target vocabulary
            data_id: Target data series
            layers: List defining structure of the NN. Ini example:
                    layers=[100,20,5] ;creates classifier with hidden layers of
                                       size 100, 20, 5 and one output layer
                                       depending on the size of vocabulary
            activation_fn: activation function used on the output of each
                           hidden layer.
            dropout_keep_prob: Probability of keeping a value during dropout
        """
        check_argument_types()
        ModelPart.__init__(self, name, reuse, save_checkpoint, load_checkpoint,
                           initializers)

        self.encoders = encoders
        self.vocabulary = vocabulary
        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 self.use_scope():
            self.gt_inputs = [tf.placeholder(tf.int32, [None], "targets")]

            mlp_input = tf.concat([enc.output for enc in self.encoders], 1)
            self._mlp = MultilayerPerceptron(mlp_input,
                                             self.layers,
                                             self.dropout_keep_prob,
                                             len(self.vocabulary),
                                             activation_fn=self.activation_fn,
                                             train_mode=self.train_mode)

        tf.summary.scalar("train_optimization_cost",
                          self.cost,
                          collections=["summary_train"])
    def __init__(self,
                 name: str,
                 encoders: List[Any],
                 vocabulary: Vocabulary,
                 data_id: str,
                 layers: Optional[List[int]] = None,
                 activation: 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)

        self.encoders = encoders
        self.vocabulary = vocabulary
        self.data_id = data_id
        self.layers = layers
        self.activation = activation
        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.int32, shape=[None], name="targets")
            ]
            mlp_input = tf.concat(1, [enc.encoded for enc in encoders])
            mlp = MultilayerPerceptron(mlp_input,
                                       layers, self.dropout_placeholder,
                                       len(vocabulary))

            self.loss_with_gt_ins = tf.reduce_mean(
                tf.nn.sparse_softmax_cross_entropy_with_logits(
                    mlp.logits, self.gt_inputs[0]))
            self.loss_with_decoded_ins = self.loss_with_gt_ins
            self.cost = self.loss_with_gt_ins

            self.decoded_seq = [mlp.classification]
            self.decoded_logits = [mlp.logits]
            self.runtime_logprobs = [tf.nn.log_softmax(mlp.logits)]

            tf.scalar_summary('val_optimization_cost',
                              self.cost,
                              collections=["summary_val"])
            tf.scalar_summary('train_optimization_cost',
                              self.cost,
                              collections=["summary_train"])
Beispiel #4
0
    def __init__(self,
                 encoders,
                 vocabulary,
                 data_id,
                 name,
                 layers=[],
                 activation=tf.tanh,
                 dropout_keep_p=0.5):
        self.encoders = encoders
        self.vocabulary = vocabulary
        self.data_id = data_id
        self.layers = layers
        self.activation = activation
        self.dropout_keep_p = dropout_keep_p
        self.name = name
        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.int32, shape=[None], name="targets")
            ]
            mlp_input = tf.concat(1, [enc.encoded for enc in encoders])
            mlp = MultilayerPerceptron(mlp_input,
                                       layers, self.dropout_placeholder,
                                       len(vocabulary))

            self.loss_with_gt_ins = tf.reduce_mean(
                tf.nn.sparse_softmax_cross_entropy_with_logits(
                    mlp.logits, self.gt_inputs[0]))
            self.loss_with_decoded_ins = self.loss_with_gt_ins
            self.cost = self.loss_with_gt_ins

            self.decoded_seq = [mlp.classification]
            self.decoded_logits = [mlp.logits]

            tf.scalar_summary('val_optimization_cost',
                              self.cost,
                              collections=["summary_val"])
            tf.scalar_summary('train_optimization_cost',
                              self.cost,
                              collections=["summary_train"])
    def __init__(self,
                 name: str,
                 encoders: List[Any],
                 vocabulary: Vocabulary,
                 data_id: str,
                 layers: List[int],
                 activation_fn: Callable[[tf.Tensor], tf.Tensor] = tf.nn.relu,
                 dropout_keep_prob: float = 0.5,
                 save_checkpoint: Optional[str] = None,
                 load_checkpoint: Optional[str] = None) -> None:
        """Construct a new instance of the sequence classifier.
        Args:
            name: Name of the decoder. Should be unique accross all Neural
                Monkey objects
            encoders: Input encoders of the decoder
            vocabulary: Target vocabulary
            data_id: Target data series
            layers: List defining structure of the NN. Ini example:
                    layers=[100,20,5] ;creates classifier with hidden layers of
                                       size 100, 20, 5 and one output layer
                                       depending on the size of vocabulary
            activation_fn: activation function used on the output of each
                           hidden layer.
            dropout_keep_prob: Probability of keeping a value during dropout
        """
        ModelPart.__init__(self, name, save_checkpoint, load_checkpoint)

        self.encoders = encoders
        self.vocabulary = vocabulary
        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 self.use_scope():
            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.int32, shape=[None], name="targets")
            ]
            mlp_input = tf.concat([enc.encoded for enc in encoders], 1)
            mlp = MultilayerPerceptron(mlp_input,
                                       layers,
                                       self.dropout_placeholder,
                                       len(vocabulary),
                                       activation_fn=self.activation_fn)

            self.loss_with_gt_ins = tf.reduce_mean(
                tf.nn.sparse_softmax_cross_entropy_with_logits(
                    logits=mlp.logits, labels=self.gt_inputs[0]))
            self.loss_with_decoded_ins = self.loss_with_gt_ins
            self.cost = self.loss_with_gt_ins

            self.decoded_seq = [mlp.classification]
            self.decoded_logits = [mlp.logits]
            self.runtime_logprobs = [tf.nn.log_softmax(mlp.logits)]

            tf.summary.scalar('val_optimization_cost',
                              self.cost,
                              collections=["summary_val"])
            tf.summary.scalar('train_optimization_cost',
                              self.cost,
                              collections=["summary_train"])