Beispiel #1
0
def run_model(input_sequence, output_size):
    """Runs model on input sequence."""

    access_config = {
        "memory_size": FLAGS.memory_size,
        "word_size": FLAGS.word_size,
        "num_reads": FLAGS.num_read_heads,
        "num_writes": FLAGS.num_write_heads,
    }
    access_config_2 = {
        "memory_size": FLAGS.memory_size_2,
        "word_size": FLAGS.word_size_2,
        "num_reads": FLAGS.num_read_heads_2,
        "num_writes": FLAGS.num_write_heads_2,
        "name": "memory_2",
    }
    controller_config = {
        "hidden_size": FLAGS.hidden_size,
    }
    clip_value = FLAGS.clip_value

    dnc_core = dnc.DNC(access_config, access_config_2, controller_config,
                       output_size, clip_value)
    initial_state = dnc_core.initial_state(FLAGS.batch_size)
    output_sequence, output_final_state = tf.nn.dynamic_rnn(
        cell=dnc_core,
        inputs=input_sequence,
        time_major=True,
        initial_state=initial_state)

    return output_sequence, output_final_state
def run_model(input_sequence, output_size):
    """Runs model on input sequence."""

    access_config = {
        "memory_size": FLAGS.memory_size,
        "word_size": FLAGS.word_size,
        "num_reads": FLAGS.num_read_heads,
        "num_writes": FLAGS.num_write_heads,
    }
    controller_config = {
        "hidden_size": FLAGS.hidden_size,
    }
    clip_value = FLAGS.clip_value
    #Creo la cella dnc
    dnc_core = dnc.DNC(access_config, controller_config, output_size,
                       clip_value)
    initial_state = dnc_core.initial_state(FLAGS.batch_size)

    #Funzione che ritorna una coppia (output,state), dove output in questo caso sara un tensore
    #di forma  [batch_size,max_time,cell.output_size] perche il flag time_major e impostato a False
    #se lo si setta a True invece la forma dell'output diventa [max_time,batch_size,cell.output_size].
    output_sequence, _ = tf.nn.dynamic_rnn(cell=dnc_core,
                                           inputs=input_sequence,
                                           time_major=False,
                                           initial_state=initial_state)

    return output_sequence
Beispiel #3
0
def run_model(input_sequence, output_size):
    """Runs model on input sequence."""

    access_config = {
        "memory_size": FLAGS.memory_size,
        "word_size": FLAGS.word_size,
        "num_reads": FLAGS.num_read_heads,
        "num_writes": FLAGS.num_write_heads,
    }
    controller_config = {
        "hidden_size": FLAGS.hidden_size,
    }
    clip_value = FLAGS.clip_value

    dnc_core = dnc.DNC(access_config, controller_config, output_size,
                       clip_value)
    initial_state = dnc_core.initial_state(FLAGS.batch_size)
    output_sequence, _ = tf.nn.dynamic_rnn(cell=dnc_core,
                                           inputs=input_sequence,
                                           time_major=True,
                                           initial_state=initial_state)

    #  print('_:')
    #  for tensor_print in _:
    #    print(tensor_print)

    return output_sequence, _
    def __call__(self, input_var, output_size, scope, reuse=False):
        print('>' * 5, "scope", scope)
        #self.scopes.append(scope)
        with tf.variable_scope(scope, reuse=reuse):
            access_config = {
                "memory_size": FLAGS.memory_size,
                "word_size": FLAGS.word_size,
                "num_reads": FLAGS.num_read_heads,
                "num_writes": FLAGS.num_write_heads,
            }
            controller_config = {
                "hidden_size": FLAGS.hidden_size,
            }
            clip_value = FLAGS.clip_value
            self.dnc_model = dnc.DNC(access_config, controller_config,
                                     output_size, clip_value)
            self.dnc_model_state = self.dnc_model.initial_state(
                self.batch_size, dtype=tf.float32)

            reshaped_input_var = tf.reshape(
                input_var, [1, -1, int(input_var.shape[1])], name="reshape_in")

            prediction, self.dnc_model_state = tf.nn.static_rnn(
                cell=self.dnc_model,
                inputs=reshaped_input_var,
                sequence_length=1,
                initial_state=self.dnc_model_state)

            #trainable_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)
            #self.saver = tf.train.Saver(trainable_variables)

            # should be [self.batch_size, output_size] ?
            reshaped_output = tf.reshape(prediction, [-1, output_size],
                                         name="reshape_out")
            return reshaped_output
Beispiel #5
0
def run_model(input_sequence, output_size):
    """Runs model on input sequence."""

    access_config = {
        "memory_size": FLAGS.memory_size,
        "word_size": FLAGS.word_size,
        "num_reads": FLAGS.num_read_heads,
        "num_writes": FLAGS.num_write_heads,
    }
    controller_config = {
        "hidden_size": FLAGS.hidden_size,
    }
    clip_value = FLAGS.clip_value

    if (FLAGS.model == "DNC"):
        dnc_core = dnc.DNC(access_config, controller_config, output_size,
                           clip_value)
        initial_state = dnc_core.initial_state(FLAGS.batch_size)
        output_sequence, _ = tf.nn.dynamic_rnn(cell=dnc_core,
                                               inputs=input_sequence,
                                               time_major=True,
                                               initial_state=initial_state)

        return output_sequence

    elif (FLAGS.model == "LSTM"):
        lstm_core = tf.nn.rnn_cell.LSTMCell(FLAGS.num_bits + 1)
        initial_state = lstm_core.zero_state(FLAGS.batch_size,
                                             dtype=tf.float32)
        output_sequence, _ = tf.nn.dynamic_rnn(cell=lstm_core,
                                               inputs=input_sequence,
                                               time_major=True,
                                               initial_state=initial_state)

        return output_sequence
Beispiel #6
0
 def _get_actor_model(self, scope):
     """ output size refers to the action distribution """
     with tf.variable_scope(scope):
         access_config, controller_config, clip_value = self._get_dnc_configs()
         dnc_model = dnc.DNC(access_config, controller_config, self._output_size, clip_value)
         dnc_model_state_init = dnc_model.initial_state(self._batch_size, dtype=self._dtype)
         input_ph = tf.placeholder(shape=(None, self._batch_size, self._input_size), dtype=self._dtype, name='input_ph')
         dnc_output, dnc_model_state = tf.nn.dynamic_rnn(
             cell=dnc_model,
             inputs=input_ph,
             time_major=True,
             initial_state=dnc_model_state_init)
         soft_output = tf.nn.softmax(logits=dnc_output, dim=-1, name="output_softmax")
         return input_ph, dnc_model_state_init, soft_output, dnc_model_state
Beispiel #7
0
 def _get_critic_model(self, scope):
     with tf.variable_scope(scope):
         access_config, controller_config, clip_value = self._get_dnc_configs()
         dnc_model = dnc.DNC(access_config, controller_config, 1, clip_value)
         dnc_model_state_init = dnc_model.initial_state(self._batch_size, dtype=self._dtype)
         obs_input_ph = tf.placeholder(shape=(None, self._batch_size, self._input_size),
                                       dtype=self._dtype, name='obs_input_ph')
         act_input_ph = tf.placeholder(shape=(None, self._batch_size, self._output_size),
                                       dtype=self._dtype, name='act_input_ph')
         concat_input = tf.concat([obs_input_ph, act_input_ph], 2)
         dnc_output, dnc_model_state = tf.nn.dynamic_rnn(
             cell=dnc_model,
             inputs=concat_input,
             time_major=True,
             initial_state=dnc_model_state_init)
         return obs_input_ph, act_input_ph, dnc_model_state_init, dnc_output, dnc_model_state
Beispiel #8
0
    def __init__(self, ntoken, ninp, nhid, nlayers, dropout=0.5):
        super(DNCLM, self).__init__()
        self.nhid = nhid
        self.nlayers = nlayers

        self.drop = nn.Dropout(dropout)
        self.encoder = nn.Embedding(ntoken, ninp)
        self.rnn = dnc.DNC(input_size=ninp,
                           hidden_size=nhid,
                           rnn_type='lstm',
                           num_layers=nlayers,
                           nr_cells=500,
                           cell_size=16,
                           read_heads=2,
                           dropout=dropout,
                           batch_first=False,
                           gpu_id=0,
                           debug=False)
        self.decoder = nn.Linear(ninp, ntoken)

        self.init_weights()
Beispiel #9
0
    def __init__(self,
                 shape,
                 classifier='softmax',
                 dynamic_cell='dnc',
                 embunits=60,
                 feunits=30,
                 depth=None,
                 trees=None,
                 pretrain=False,
                 categorical_index=None,
                 vocabulary=None,
                 pretrain_sample=None,
                 set_size=None,
                 mode='dynamic',
                 data='both',
                 kernel_output=3000,
                 iter_init=None,
                 target_path=None,
                 feature_path=None,
                 vocabulary_size=None,
                 save_path=None,
                 unstructured_path=None):
        self.graph = tf.Graph()
        self.shape = shape
        self.batch_size = shape[0]
        self.pretrain_sample = pretrain_sample
        self.classifier = classifier
        self.dynamic_cell = dynamic_cell
        self.feunits = feunits
        self.embunits = embunits
        self.categorical_index = categorical_index
        self.set_size = set_size
        self.kerneloutput = kernel_output
        self.iter_init = iter_init
        self.feature_path = feature_path
        self.target_path = target_path
        self.vocabulary_size = vocabulary_size
        self.save_path = save_path
        self.unstructured_path = unstructured_path
        self.mode = mode
        self.pretrain = pretrain
        self.first_pretrain_iter = True

        self.size_tensor = tf.Variable(shape[2])

        if (classifier == 'decision_tree' or classifier == 'random_forest'):
            if (classifier == 'decision_tree'):
                trees = 1
            self.feunits = trees * ((2**depth) - 1)

        with self.graph.as_default():
            self.session = tf.Session()

            self.keep_prob = tf.placeholder(tf.float32, name='keep_prob')
            self.is_training = tf.placeholder(tf.bool, name='is_training')

            if (mode == 'static'):
                self.X = tf.placeholder(tf.float32,
                                        shape=(None, shape[1]),
                                        name="Input")
            else:
                self.X = tf.placeholder(tf.float32,
                                        shape=(None, shape[0], shape[1]),
                                        name="Input")
            self.Y = tf.placeholder(tf.float32, shape=(None), name="Label")
            self.initial_state = tf.get_variable(
                'initial_state', (self.batch_size, self.feunits),
                dtype=tf.float32)

            #*********************
            #** Embedding Layer **
            #*********************
            if (data == 'unstructured' or data == 'both'):
                if (pretrain):
                    self.embeddingLayer = tf.layers.dense(
                        inputs=self.X,
                        units=embunits,
                        activation=None,
                        kernel_initializer=self._pre_train_embeddings,
                        name="Embedding")
                else:
                    self.embeddingLayer = tf.layers.dense(
                        inputs=self.X,
                        units=embunits,
                        activation=None,
                        kernel_initializer=tf.random_uniform_initializer(),
                        name="Embedding")
            else:
                self.embeddingLayer = self.X

            #*************************************
            #** Static Feature Extraction Layer **
            #*************************************
            self.staticFeatureExtractionLayer1 = tf.nn.dropout(
                tf.layers.dense(
                    inputs=self.embeddingLayer,
                    units=self.feunits,
                    activation=tf.nn.relu,
                    name="Static_FE1",
                    kernel_initializer=tf.variance_scaling_initializer()),
                keep_prob=self.keep_prob)
            self.staticFeatureExtractionLayer2 = tf.nn.dropout(
                tf.layers.dense(
                    inputs=self.staticFeatureExtractionLayer1,
                    units=20,
                    activation=tf.nn.relu,
                    name="Static_FE2",
                    kernel_initializer=tf.variance_scaling_initializer()),
                keep_prob=self.keep_prob)

            if (mode == 'dynamic'):
                #**************************************
                #** Dynamic Feature Extraction Layer **
                #**************************************
                #Differentiable Neural Computer
                if (dynamic_cell == 'dnc'):
                    access_config = {
                        "memory_size": self.batch_size,
                        "word_size": self.feunits,
                        "num_reads": 1,
                        "num_writes": 1,
                    }
                    controller_config = {
                        "hidden_size": self.feunits,
                    }
                    clip_value = 1
                    self.dynamicFeatureExtractionCell = dnc.DNC(
                        access_config, controller_config, self.feunits,
                        clip_value)
                    self.initial_state = self.dynamicFeatureExtractionCell.initial_state(
                        self.batch_size)
                    outputs, _ = tf.nn.dynamic_rnn(
                        self.dynamicFeatureExtractionCell,
                        self.staticFeatureExtractionLayer2,
                        time_major=False,
                        initial_state=self.initial_state,
                        dtype=tf.float32,
                        scope="DNC")
                else:

                    #Recurrent Neural Network
                    if (dynamic_cell == 'rnn'):
                        self.dynamicFeatureExtractionCell = tf.contrib.rnn.BasicRNNCell(
                            self.feunits)
                        sequence_fe_name = "RNN"

                #Long Short Term Memory Network
                    if (dynamic_cell == 'lstm'):
                        self.dynamicFeatureExtractionCell = tf.contrib.rnn.BasicLSTMCell(
                            self.feunits)
                        sequence_fe_name = "LSTM"
                    outputs, _ = tf.nn.dynamic_rnn(
                        self.dynamicFeatureExtractionCell,
                        self.staticFeatureExtractionLayer2,
                        time_major=False,
                        dtype=tf.float32,
                        scope=sequence_fe_name)

                self.dynamicFeatureExtractionLayer = outputs[:,
                                                             self.batch_size -
                                                             1, :]  #last output of each user

            else:
                self.dynamicFeatureExtractionLayer = self.staticFeatureExtractionLayer2

            #**************************
            #** Classification Layer **
            #**************************
            #Softmax
            if (classifier == 'softmax'):
                self.classificationLayer = tf.layers.dense(
                    inputs=self.dynamicFeatureExtractionLayer,
                    activation=None,
                    units=10,
                    kernel_initializer=tf.contrib.layers.xavier_initializer(),
                    activity_regularizer=tf.contrib.layers.l2_regularizer(
                        0.001))

            #Kernel Softmax
            elif (classifier == 'kernel'):
                self.kernel_mapper = tf.contrib.kernel_methods.RandomFourierFeatureMapper(
                    input_dim=20,
                    output_dim=self.kerneloutput,
                    stddev=5.0,
                    name='RFFM')
                self.classificationLayer = tf.layers.dense(
                    inputs=self.kernel_mapper.map(
                        self.dynamicFeatureExtractionLayer),
                    activation=None,
                    units=50,
                    kernel_initializer=tf.contrib.layers.xavier_initializer(),
                    activity_regularizer=tf.contrib.layers.l2_regularizer(
                        1e-6),
                    name="KLR")

            #**** Loss ****
            cost = tf.reduce_mean(
                tf.nn.weighted_cross_entropy_with_logits(
                    targets=self.Y,
                    logits=self.classificationLayer,
                    pos_weight=175)) + tf.losses.get_regularization_loss()

            self.loss_function = cost

            self.opt = tf.train.AdamOptimizer().minimize(cost)

            self.threshold = tf.constant(0.5)

            init = tf.global_variables_initializer()
            init_local = tf.local_variables_initializer()
            self.session.run([init, init_local])

            self.outscore = tf.sigmoid(self.classificationLayer)

            self.pred = tf.where(tf.greater(self.outscore, self.threshold),
                                 tf.ones(tf.shape(self.classificationLayer)),
                                 tf.zeros(tf.shape(self.classificationLayer)))

            tf.summary.FileWriter(save_path, self.graph)
Beispiel #10
0
    def __init__(self,
                 input_size,
                 output_size,
                 batch_size,
                 cost_fun,
                 scope="dnc_wrapper"):
        self.batch_size = batch_size
        self.scope = scope

        # to be set in the compile method
        self.save_dir = None
        self.save_name = None
        self.session = None
        self.dtype = tf.float32

        with tf.variable_scope(scope):
            # dnc model
            access_config = {
                "memory_size": FLAGS.memory_size,
                "word_size": FLAGS.word_size,
                "num_reads": FLAGS.num_read_heads,
                "num_writes": FLAGS.num_write_heads,
            }
            controller_config = {
                "hidden_size": FLAGS.hidden_size,
            }
            clip_value = FLAGS.clip_value
            self.dnc_model = dnc.DNC(access_config, controller_config,
                                     output_size, clip_value)
            self.dnc_model_state_init = self.dnc_model.initial_state(
                batch_size, dtype=tf.float32)

            # further tf objects
            # learning rate can be a tensor object, fed with feed_dict, if wanted
            optimizer = tf.train.RMSPropOptimizer(
                FLAGS.learning_rate, epsilon=FLAGS.optimizer_epsilon)
            global_step = tf.get_variable(name="global_step",
                                          shape=[],
                                          dtype=tf.int64,
                                          initializer=tf.zeros_initializer(),
                                          trainable=False,
                                          collections=[
                                              tf.GraphKeys.GLOBAL_VARIABLES,
                                              tf.GraphKeys.GLOBAL_STEP
                                          ])

            # prediction and training
            # dynamic_rnn input: [max_time, batch_size, depth]
            # print(input)   #Tensor("repeat_copy/Reshape_32:0", shape=(?, 16, 6), dtype=float32)
            # print(target)  #Tensor("repeat_copy/Reshape_33:0", shape=(?, 16, 5), dtype=float32)
            # print(mask)    #Tensor("repeat_copy/transpose:0", shape=(?, 16), dtype=float32)
            self.input_placeholder = tf.placeholder(shape=(None, batch_size,
                                                           input_size),
                                                    dtype=tf.float32,
                                                    name='input_placeholder')
            self.target_placeholder = tf.placeholder(shape=(None, batch_size,
                                                            output_size),
                                                     dtype=tf.float32,
                                                     name='target_placeholder')
            self.mask_placeholder = tf.placeholder(shape=(None, batch_size),
                                                   dtype=tf.float32,
                                                   name='mask_placeholder')

            self.prediction, self.dnc_model_state = tf.nn.dynamic_rnn(
                cell=self.dnc_model,
                inputs=self.input_placeholder,
                time_major=True,
                initial_state=self.dnc_model_state_init)

            # Used for visualization.
            self.output_node = tf.round(
                tf.expand_dims(self.mask_placeholder, -1) *
                tf.sigmoid(self.prediction))

            self.train_loss_node = cost_fun(self.prediction,
                                            self.target_placeholder,
                                            self.mask_placeholder)

            # used for weight setting
            weight_setter_nodes = []
            self.weight_setter_feeds = []
            self.trainable_variables = tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope)
            for i in range(len(self.trainable_variables)):
                v = self.trainable_variables[i]
                var = tf.Variable(np.zeros(v.shape), dtype=tf.float32)
                weight_setter_nodes.append(v.assign(var))
                self.weight_setter_feeds.append(var)
            self.weight_setter_node = tf.group(*weight_setter_nodes)
            print(self.scope, 'has a total of', len(self.trainable_variables),
                  'trainable vars')

            # Set up optimizer with global norm clipping.
            grads, _ = tf.clip_by_global_norm(
                tf.gradients(self.train_loss_node, self.trainable_variables),
                FLAGS.max_grad_norm)

            self.train_step_node = optimizer.apply_gradients(
                zip(grads, self.trainable_variables), global_step=global_step)

            self.saver = tf.train.Saver(self.trainable_variables)
            self.var_init = tf.variables_initializer(
                tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                  scope=self.scope))
Beispiel #11
0
    def __init__(self, FLAGS):
        FLAGS.hidden_size = 50
        FLAGS.memory_size = 128
        FLAGS.word_size = 20
        FLAGS.num_write_heads = 1
        FLAGS.num_read_heads = 1
        FLAGS.clip_value = 20

        self.embedding = tf.placeholder(tf.float32)
        self.X = tf.placeholder(tf.float32,
            shape=[None, FLAGS.input_dim, FLAGS.num_classes], name='inputs_X')
        self.y = tf.placeholder(tf.float32,
            shape=[None, FLAGS.num_classes], name='targets_y')
        self.l = tf.placeholder(tf.float32, [],  # need [] for tf.scalar_mul
            name="learning_rate")
        self.e = tf.placeholder(tf.float32, [],
            name="decay_rate")
        with tf.variable_scope("DNC"):

            # softmax weights (proper initialization)
            self.W_softmax = tf.Variable(tf.random_uniform(
                [FLAGS.num_hidden_units, FLAGS.num_classes],
                -np.sqrt(2.0 / FLAGS.num_hidden_units),
                np.sqrt(2.0 / FLAGS.num_hidden_units)),
                dtype=tf.float32)
            self.b_softmax = tf.Variable(tf.zeros(
                [FLAGS.num_classes]),
                dtype=tf.float32)

        # DNC.
        access_config = {
            "memory_size": FLAGS.memory_size,
            "word_size": FLAGS.word_size,
            "num_reads": FLAGS.num_read_heads,
            "num_writes": FLAGS.num_write_heads,
        }
        controller_config = {
            "hidden_size": FLAGS.hidden_size,
        }
        clip_value = FLAGS.clip_value

        self.dnc_core = dnc.DNC(access_config, controller_config, FLAGS.num_hidden_units, clip_value)
        self.new_state = self.dnc_core.initial_state(FLAGS.batch_size)

        with tf.variable_scope("dnc_step") as scope:
            for t in range(0, FLAGS.input_dim):
                if t > 0:
                    scope.reuse_variables()
                self.new_h, self.new_state = self.dnc_core(self.X[:, t, :], self.new_state)
                if t > 0:
                    self.gate_history = tf.concat([self.gate_history, tf.expand_dims([[0], [0]], axis=2)], axis=2)
                    self.hidden_history = tf.concat([self.hidden_history, tf.expand_dims(self.new_state.controller_state.hidden, axis=2)], axis=2)
                    self.memory_history = tf.concat([self.memory_history, tf.expand_dims(self.new_state.access_state.memory, axis=3)], axis=3)
                    self.read_weight_history = tf.concat([self.read_weight_history, tf.expand_dims(self.new_state.access_state.read_weights, axis=3)], axis=3)
                    self.write_weight_history = tf.concat([self.write_weight_history, tf.expand_dims(self.new_state.access_state.write_weights, axis=3)], axis=3)
                else:
                    self.gate_history = tf.expand_dims([[0], [0]], axis=2)
                    self.hidden_history = tf.expand_dims(self.new_state.controller_state.hidden, axis=2)
                    self.memory_history = tf.expand_dims(self.new_state.access_state.memory, axis=3)
                    self.read_weight_history = tf.expand_dims(self.new_state.access_state.read_weights, axis=3)
                    self.write_weight_history = tf.expand_dims(self.new_state.access_state.write_weights, axis=3)

        # All inputs processed! Time for softmax
        self.logits = tf.matmul(self.new_h, self.W_softmax) + self.b_softmax

        # Loss
        self.loss = tf.reduce_mean(tf.losses.mean_squared_error(labels=self.y, predictions=self.logits))  # If embedding instead of one-hot, don't want softmax (want actual values at each index, not just a classifier).

        # Optimization
        self.lr = tf.Variable(0.0, trainable=False)
        self.trainable_vars = tf.trainable_variables()
        # clip the gradient to avoid vanishing or blowing up gradients
        self.grads, self.norm = tf.clip_by_global_norm(
            tf.gradients(self.loss, self.trainable_vars), FLAGS.max_gradient_norm)
        optimizer = tf.train.AdamOptimizer(self.lr)
        self.update = optimizer.apply_gradients(
            zip(self.grads, self.trainable_vars))

        # Accuracy
        self.accuracy = embedding_util.get_01_accuracy(self.y, self.logits, self.embedding)

        # Components for model saving
        self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=4)