def build_rnn_from_options_dict(x, x_lengths, options_dict):
    network_dict = {}

    # Shared layers
    if options_dict["bidirectional"]:
        rnn_outputs, rnn_states = tflego.build_bidirectional_multi_rnn(
            x,
            x_lengths,
            options_dict["rnn_n_hiddens"],
            rnn_type=options_dict["rnn_type"],
            keep_prob=options_dict["rnn_keep_prob"])
    else:
        rnn_outputs, rnn_states = tflego.build_multi_rnn(
            x,
            x_lengths,
            options_dict["rnn_n_hiddens"],
            rnn_type=options_dict["rnn_type"],
            keep_prob=options_dict["rnn_keep_prob"])
    if options_dict["rnn_type"] == "lstm":
        rnn_states = rnn_states.h
    rnn = tflego.build_feedforward(rnn_states,
                                   options_dict["ff_n_hiddens"],
                                   keep_prob=options_dict["ff_keep_prob"])
    network_dict["encoding"] = rnn

    # Split layers
    language_id = tf.placeholder(TF_ITYPE, [None])
    network_dict["language_id"] = language_id
    split_networks = []
    for i_lang in range(options_dict["n_languages"]):
        with tf.variable_scope("split_ff_{}".format(i_lang)):
            split_ff = tflego.build_feedforward(
                rnn,
                options_dict["split_ff_n_hiddens"] +
                [options_dict["n_types_per_lang"]],
                keep_prob=options_dict["ff_keep_prob"])
        split_networks.append(split_ff)

    # Output
    i_lang = len(split_networks) - 1
    for split_ff in split_networks[::-1]:
        if i_lang == len(split_networks) - 1:
            output = split_ff
        else:
            output = tf.where(tf.equal(language_id, i_lang), split_ff, output)
        i_lang -= 1
    network_dict["output"] = output

    return network_dict
Exemple #2
0
def build_rnn_from_options_dict(x, x_lengths, options_dict):
    network_dict = {}
    if options_dict["bidirectional"]:
        rnn_outputs, rnn_states = tflego.build_bidirectional_multi_rnn(
            x,
            x_lengths,
            options_dict["rnn_n_hiddens"],
            rnn_type=options_dict["rnn_type"],
            keep_prob=options_dict["rnn_keep_prob"])
    else:
        rnn_outputs, rnn_states = tflego.build_multi_rnn(
            x,
            x_lengths,
            options_dict["rnn_n_hiddens"],
            rnn_type=options_dict["rnn_type"],
            keep_prob=options_dict["rnn_keep_prob"])
    if options_dict["rnn_type"] == "lstm":
        rnn_states = rnn_states.h
    rnn = tflego.build_feedforward(rnn_states,
                                   options_dict["ff_n_hiddens"],
                                   keep_prob=options_dict["ff_keep_prob"])
    network_dict["encoding"] = rnn
    with tf.variable_scope("ff_layer_final"):
        rnn = tflego.build_linear(rnn, options_dict["n_classes"])
    network_dict["output"] = rnn
    return network_dict
def build_siamese_rnn_side(x, x_lengths, rnn_n_hiddens, ff_n_hiddens,
        rnn_type="lstm", rnn_keep_prob=1.0, ff_keep_prob=1.0,
        bidirectional=False):
    """
    Multi-layer RNN serving as one side of a Siamese model.

    Parameters
    ----------
    x : Tensor [n_data, maxlength, d_in]
    """

    if bidirectional:
        rnn_outputs, rnn_states = tflego.build_bidirectional_multi_rnn(
            x, x_lengths, rnn_n_hiddens, rnn_type=rnn_type,
            keep_prob=rnn_keep_prob
            )
    else:
        rnn_outputs, rnn_states = tflego.build_multi_rnn(
            x, x_lengths, rnn_n_hiddens, rnn_type=rnn_type,
            keep_prob=rnn_keep_prob
            )
    if rnn_type == "lstm":
        rnn_states = rnn_states.h
    rnn = tflego.build_feedforward(
        rnn_states, ff_n_hiddens, keep_prob=ff_keep_prob
        )
    return rnn
Exemple #4
0
def build_cnn_from_options_dict(x, options_dict, ff_keep_prob=1.0):
    network_dict = {}
    cnn = tflego.build_cnn(x,
                           options_dict["input_shape"],
                           options_dict["filter_shapes"],
                           options_dict["pool_shapes"],
                           padding="VALID")
    cnn = tf.contrib.layers.flatten(cnn)
    cnn = tflego.build_feedforward(cnn,
                                   options_dict["ff_n_hiddens"],
                                   keep_prob=ff_keep_prob)
    network_dict["encoding"] = cnn
    with tf.variable_scope("ff_layer_final"):
        cnn = tflego.build_linear(cnn, options_dict["n_classes"])
    network_dict["output"] = cnn
    return network_dict
def build_siamese_cnn_side(x, input_shape, filter_shapes, pool_shapes,
        ff_n_hiddens, ff_keep_prob=1.0):
    """
    CNN serving as one side of a Siamese model.
    
    See `tflego.build_cnn` for more details on the parameters.

    Parameters
    ----------
    x : Tensor [n_data, n_input]
        Input to the CNN, which is reshaped to match `input_shape`.
    input_shape : list
        The shape of the input to the CNN as [n_data, height, width, d_in].
    """
    cnn = tflego.build_cnn(x, input_shape, filter_shapes, pool_shapes, padding="VALID")
    cnn = tf.contrib.layers.flatten(cnn)
    cnn = tflego.build_feedforward(cnn, ff_n_hiddens, keep_prob=ff_keep_prob)
    return cnn