Esempio n. 1
0
def diagonal_lstm(inputs, scope='diagonal_lstm'):
    with tf.compat.v1.variable_scope(scope):

        skewed_inputs = skew(inputs, scope="skewed_i")

        input_to_state = conv2d(skewed_inputs,
                                64, [1, 1],
                                mask_type="b",
                                scope="i_to_s")
        column_wise_inputs = tf.transpose(input_to_state, perm=[0, 2, 1, 3])

        batch, width, height, channel = column_wise_inputs.get_shape().as_list(
        )
        rnn_inputs = tf.reshape(column_wise_inputs,
                                [-1, width, height * channel])

        cell = DiagonalLSTMCell(16, height, channel)

        outputs, states = tf.compat.v1.nn.dynamic_rnn(cell,
                                                      inputs=rnn_inputs,
                                                      dtype=tf.float32)
        width_first_outputs = tf.reshape(outputs, [-1, width, height, 16])

        skewed_outputs = tf.transpose(width_first_outputs, perm=[0, 2, 1, 3])
        outputs = unskew(skewed_outputs)

        return outputs
Esempio n. 2
0
def skew(inputs, scope="skew"):

    with tf.compat.v1.name_scope(scope):

        batch, height, width, channel = inputs.get_shape().as_list()
        rows = tf.split(inputs, height, 1)

        new_width = width + height - 1
        new_rows = []

        for idx, row in enumerate(rows):
            transposed_row = tf.transpose(tf.squeeze(row, [1]), perm=[0, 2, 1])
            squeezed_row = tf.reshape(transposed_row, [-1, width])
            padded_row = tf.pad(squeezed_row, paddings=((0, 0), (idx, height - 1 - idx)))

            unsqueezed_row = tf.reshape(padded_row, [-1, channel, new_width])
            untransposed_row = tf.transpose(unsqueezed_row, perm=[0, 2, 1])
            new_rows.append(untransposed_row)

        outputs = tf.stack(new_rows, axis=1, name="output")

    return outputs
def LSTM_Sentiment(input_tensor):

    #  Reference Paper: https://www.bioinf.jku.at/publications/older/2604.pdf

    lstmCell = tf.compat.v1.nn.rnn_cell.BasicLSTMCell(1024)
    output_rnn, _ = tf.compat.v1.nn.dynamic_rnn(lstmCell, input_tensor, dtype=tf.float32)

    W_fc = tf.Variable(tf.random.truncated_normal([1024, 2]))
    b_fc = tf.Variable(tf.constant(0.1, shape=[2]))

    output_transposed = tf.transpose(output_rnn, perm=[1, 0, 2])
    output = tf.gather(output_transposed, int(output_transposed.get_shape()[0]) - 1)

    return tf.identity(tf.matmul(output, W_fc) + b_fc, name="output")