Exemplo n.º 1
0
    def __init__(self, hidden_size, num_layers, grid_size, dim):
        # super(neighborhood_stat_enc, self).__init__()

        self.hidden_size = hidden_size

        self.input = tf.placeholder(name='input',
                                    shape=(dim, 8),
                                    dtype=tf.float64)

        self.hidden_state = tf.placeholder(name='hidden_state',
                                           shape=(dim, self.hidden_size),
                                           dtype=tf.float64)

        # ctxt_img = tf.convert_to_tensor(imread(ctxt_path[0]), dtype=tf.float64)
        # ctxt_img = tf.convert_to_tensor(tf.pad(ctxt_img, paddings=tf.constant([[1, 1, ], [0, 1], [0, 0]])),
        #                                 dtype=tf.float64)
        #
        # ctxt_img = tf.expand_dims(ctxt_img, axis=0)
        # self._2dconv = tf.nn.conv2d(input=ctxt_img, filter=tf.random_normal(shape=[561, 711, 3, 1],dtype=tf.float64),
        #                             padding='VALID', strides=[1, 1, 1, 1])
        # self._2dconv = tf.squeeze(self._2dconv)
        # in gridLSTM frequency blocks are the units of lstm stacking vertically
        # while time LSTM spans horizontally
        self.rnn = rnn.GridLSTMCell(num_units=num_layers,
                                    feature_size=grid_size,
                                    frequency_skip=grid_size,
                                    use_peepholes=False,
                                    num_frequency_blocks=[int(grid_size / 2)],
                                    share_time_frequency_weights=True,
                                    state_is_tuple=False,
                                    couple_input_forget_gates=True,
                                    reuse=True)

        self.output, self.c_hidden_states = self.rnn(self.input,
                                                     self.hidden_state)
Exemplo n.º 2
0
 def testGridLSTMCellWithFrequencyBlocks(self):
   with self.test_session() as sess:
     num_units = 8
     batch_size = 3
     input_size = 4
     feature_size = 2
     frequency_skip = 1
     num_frequency_blocks = [1, 1]
     total_blocks = num_frequency_blocks[0] + num_frequency_blocks[1]
     start_freqindex_list = [0, 2]
     end_freqindex_list = [2, 4]
     with variable_scope.variable_scope(
         "root", initializer=init_ops.constant_initializer(0.5)):
       cell = rnn_cell.GridLSTMCell(
           num_units=num_units,
           feature_size=feature_size,
           frequency_skip=frequency_skip,
           forget_bias=1.0,
           num_frequency_blocks=num_frequency_blocks,
           start_freqindex_list=start_freqindex_list,
           end_freqindex_list=end_freqindex_list,
           couple_input_forget_gates=True,
           state_is_tuple=True)
       inputs = constant_op.constant(
           np.array(
               [[1., 1., 1., 1.], [2., 2., 2., 2.], [3., 3., 3., 3.]],
               dtype=np.float32),
           dtype=dtypes.float32)
       state_value = constant_op.constant(
           0.1 * np.ones(
               (batch_size, num_units), dtype=np.float32),
           dtype=dtypes.float32)
       init_state = cell.state_tuple_type(
           *([state_value, state_value] * total_blocks))
       output, state = cell(inputs, init_state)
       sess.run([variables.global_variables_initializer()])
       res = sess.run([output, state])
       self.assertEqual(len(res), 2)
       # The numbers in results were not calculated, this is mostly just a
       # smoke test.
       self.assertEqual(res[0].shape,
                        (batch_size, num_units * total_blocks * 2))
       for ss in res[1]:
         self.assertEqual(ss.shape, (batch_size, num_units))
       # Different inputs so different outputs and states
       for i in range(1, batch_size):
         self.assertTrue(
             float(np.linalg.norm((res[0][0, :] - res[0][i, :]))) > 1e-6)
         self.assertTrue(
             float(
                 np.linalg.norm((res[1].state_f00_b00_c[0, :] - res[1]
                                 .state_f00_b00_c[i, :]))) > 1e-6)
Exemplo n.º 3
0
    def __init__(self, hidden_size,hidden_len, num_layers, grid_size, embedding_size,dropout=0):
        # super(neighborhood_vis_loc_encoder, self).__init__()
        # tf.Variable()

        reg_w = tf.contrib.layers.l2_regularizer(scale=0.1)
        self.hidden_size = hidden_size
        self.embedding_size = embedding_size
        self.input = tf.placeholder(dtype=tf.float64, shape=[hidden_len,hidden_len], name="inputs")
        self.state_f00_b00_c = tf.placeholder(name='state_f00_b00_c', shape=(hidden_len, self.hidden_size), dtype=tf.float64)
        self.c_hidden_state = tf.placeholder(name='c_hidden_state', shape=(hidden_len, self.hidden_size), dtype=tf.float64)

        self.state_f00_b00_m = tf.placeholder(name='state_f00_b00_m', shape=(hidden_len, (grid_size * (grid_size/2))), dtype=tf.float64)
        self.num_freq_blocks = tf.placeholder(name='num_freq_blocks', dtype=tf.float32)

        self.rnn = rnn.GridLSTMCell(num_units=num_layers,
                                    feature_size=grid_size,
                                    frequency_skip=grid_size,
                                    use_peepholes=True,
                                    num_frequency_blocks=[int(grid_size/2)],
                                    share_time_frequency_weights=True,
                                    state_is_tuple=False,
                                    couple_input_forget_gates=True,
                                    reuse=tf.AUTO_REUSE)

        # self.rnn = rnn_t.LSTMCell(num_units=num_layers, name='nghood_lstm', use_peepholes=True,
        #                           initializer='normal', dynamic=True,
        #                           dtype=tf.float64)

        self.output = tf.placeholder(dtype=tf.float64, shape=[hidden_len, (grid_size * (grid_size/2))],
                                     name="output")
        # .add_weight(name='weight', shape=(2,hidden_size))
                                    # initial_value=init_w(shape=(2,hidden_size)),
                                    # trainable=True
                                    #regularizer= reg_w.l2(weights=init_w(shape=(2,hidden_size)))
        # self.rnn = nn.GRU(input_size, hidden_size, num_layers,
        #                   batch_first=True, bidirectional=False, dropout=dropout)
        # self.rnn.add_weight(name='weight', shape=(2,hidden_size),
        #                     trainable=True,initializer=init_w(shape=(2,hidden_size)))
        self.forward()
Exemplo n.º 4
0
    def __init__(self, hidden_size, num_layers,grid_size, dim):
        # super(neighborhood_stat_enc, self).__init__()

        self.hidden_size = hidden_size

        self.input = tf.placeholder(name='input', shape=(dim, dim),
                                     dtype=tf.float64)

        self.hidden_state = tf.placeholder(name='hidden_state', shape=(dim, self.hidden_size),
                                    dtype=tf.float64)

        # in gridLSTM frequency blocks are the units of lstm stacking vertically
        # while time LSTM spans horizontally
        self.rnn = rnn.GridLSTMCell(num_units=num_layers,
                                    feature_size=grid_size,
                                    frequency_skip=grid_size,
                                    use_peepholes=False,
                                    num_frequency_blocks=[int(grid_size/2)],
                                    share_time_frequency_weights=True,
                                    state_is_tuple=False,
                                    couple_input_forget_gates=True,
                                    reuse=True)

        self.output, self.c_hidden_states = self.rnn(self.input, self.hidden_state)
Exemplo n.º 5
0
 def testGridLstmCellWithCoupledInputForgetGates(self):
   num_units = 2
   batch_size = 3
   input_size = 4
   feature_size = 2
   frequency_skip = 1
   num_shifts = int((input_size - feature_size) / frequency_skip + 1)
   expected_output = np.array(
       [[0.416383, 0.416383, 0.403238, 0.403238, 0.524020, 0.524020,
         0.565425, 0.565425, 0.557865, 0.557865, 0.609699, 0.609699],
        [0.627331, 0.627331, 0.622393, 0.622393, 0.688342, 0.688342,
         0.708078, 0.708078, 0.694245, 0.694245, 0.715171, 0.715171],
        [0.711050, 0.711050, 0.709197, 0.709197, 0.736533, 0.736533,
         0.744264, 0.744264, 0.737390, 0.737390, 0.745250, 0.745250]],
       dtype=np.float32)
   expected_state = np.array(
       [[0.625556, 0.625556, 0.416383, 0.416383, 0.759134, 0.759134,
         0.524020, 0.524020, 0.798795, 0.798795, 0.557865, 0.557865],
        [0.875488, 0.875488, 0.627331, 0.627331, 0.936432, 0.936432,
         0.688342, 0.688342, 0.941961, 0.941961, 0.694245, 0.694245],
        [0.957327, 0.957327, 0.711050, 0.711050, 0.979522, 0.979522,
         0.736533, 0.736533, 0.980245, 0.980245, 0.737390, 0.737390]],
       dtype=np.float32)
   for state_is_tuple in [False, True]:
     with self.test_session() as sess:
       with variable_scope.variable_scope(
           "state_is_tuple" + str(state_is_tuple),
           initializer=init_ops.constant_initializer(0.5)):
         cell = rnn_cell.GridLSTMCell(
             num_units=num_units,
             feature_size=feature_size,
             frequency_skip=frequency_skip,
             forget_bias=1.0,
             num_frequency_blocks=[num_shifts],
             couple_input_forget_gates=True,
             state_is_tuple=state_is_tuple)
         inputs = constant_op.constant(
             np.array([[1., 1., 1., 1.],
                       [2., 2., 2., 2.],
                       [3., 3., 3., 3.]],
                      dtype=np.float32),
             dtype=dtypes.float32)
         if state_is_tuple:
           state_value = constant_op.constant(
               0.1 * np.ones(
                   (batch_size, num_units), dtype=np.float32),
               dtype=dtypes.float32)
           init_state = cell.state_tuple_type(
               *([state_value, state_value] * num_shifts))
         else:
           init_state = constant_op.constant(
               0.1 * np.ones(
                   (batch_size, num_units * num_shifts * 2), dtype=np.float32),
               dtype=dtypes.float32)
         output, state = cell(inputs, init_state)
         sess.run([variables.global_variables_initializer()])
         res = sess.run([output, state])
         # This is a smoke test: Only making sure expected values not change.
         self.assertEqual(len(res), 2)
         self.assertAllClose(res[0], expected_output)
         if not state_is_tuple:
           self.assertAllClose(res[1], expected_state)
         else:
           # There should be num_shifts * 2 states in the tuple.
           self.assertEqual(len(res[1]), num_shifts * 2)
           # Checking the shape of each state to be batch_size * num_units
           for ss in res[1]:
             self.assertEqual(ss.shape[0], batch_size)
             self.assertEqual(ss.shape[1], num_units)
           self.assertAllClose(np.concatenate(res[1], axis=1), expected_state)
Exemplo n.º 6
0
    def __init__(self,
                 hidden_size,
                 hidden_len,
                 num_layers,
                 grid_size,
                 embedding_size,
                 dim,
                 dropout=0):
        # super(neighborhood_vis_loc_encoder, self).__init__()
        # tf.Variable()
        # reg_w = tf.contrib.layers.l2_regularizer(scale=0.1)
        self.hidden_size = hidden_size
        self.embedding_size = embedding_size
        self.input = tf.placeholder(dtype=tf.float64,
                                    shape=[hidden_len + 2, hidden_len + 2],
                                    name="inputs")

        # TODO: shape 4D
        self.state_f00_b00_c = tf.placeholder(name='state_f00_b00_c',
                                              shape=(hidden_len + 2,
                                                     self.hidden_size),
                                              dtype=tf.float64)
        self.c_hidden_state = tf.placeholder(name='c_hidden_state',
                                             shape=(hidden_len,
                                                    self.hidden_size),
                                             dtype=tf.float64)

        self.state_f00_b00_m = tf.placeholder(
            name='state_f00_b00_m',
            shape=(hidden_len, (grid_size * (grid_size / 2))),
            dtype=tf.float64)
        self.num_freq_blocks = tf.placeholder(name='num_freq_blocks',
                                              dtype=tf.float32)
        self.hidden_size = hidden_size
        # self.input = tf.placeholder(name='input', shape=(dim, 8),
        #                             dtype=tf.float64)
        self.stat_input = tf.placeholder(name='stat_input',
                                         shape=(dim + 2, 8),
                                         dtype=tf.float64)

        self.hidden_state = tf.placeholder(name='hidden_state',
                                           shape=(dim + 2, self.hidden_size),
                                           dtype=tf.float64)

        self.output = tf.placeholder(
            dtype=tf.float64,
            shape=[hidden_len, hidden_len],  # (grid_size * (grid_size/2))],
            name="output")

        self.rnn = rnn.GridLSTMCell(
            num_units=num_layers,
            feature_size=grid_size,
            frequency_skip=grid_size,
            use_peepholes=True,
            num_frequency_blocks=[int(hidden_len / grid_size)
                                  ],  #int(grid_size/2)
            share_time_frequency_weights=True,
            state_is_tuple=False,
            couple_input_forget_gates=True,
            reuse=tf.AUTO_REUSE)

        self.stat_rnn = rnn.GridLSTMCell(
            num_units=num_layers,
            feature_size=grid_size,
            frequency_skip=grid_size,
            use_peepholes=False,
            num_frequency_blocks=[int(grid_size / 2)],
            share_time_frequency_weights=True,
            state_is_tuple=False,
            couple_input_forget_gates=True,
            reuse=tf.AUTO_REUSE)

        # self.rnn = rnn_t.LSTMCell(num_units=num_layers, name='nghood_lstm', use_peepholes=True,
        #                           initializer='normal', dynamic=True,
        #                           dtype=tf.float64)

        self.forward()