예제 #1
0
파일: uba.py 프로젝트: zss8848/3book
def do_birnn_wordbag(trainX, testX, trainY, testY):
    y_test=testY
    #trainX = pad_sequences(trainX, maxlen=100, value=0.)
    #testX = pad_sequences(testX, maxlen=100, value=0.)
    # Converting labels to binary vectors
    trainY = to_categorical(trainY, nb_classes=2)
    testY = to_categorical(testY, nb_classes=2)

    # Network building
    # Network building
    net = input_data(shape=[None, 100])
    net = tflearn.embedding(net, input_dim=10000, output_dim=128)
    net = tflearn.bidirectional_rnn(net, BasicLSTMCell(128), BasicLSTMCell(128))
    net = dropout(net, 0.5)
    net = fully_connected(net, 2, activation='softmax')
    net = regression(net, optimizer='adam', loss='categorical_crossentropy')

    # Training
    model = tflearn.DNN(net, tensorboard_verbose=0)
    model.fit(trainX, trainY, validation_set=(testX, testY), show_metric=True,
              batch_size=1,run_id="uba",n_epoch=10)

    y_predict_list = model.predict(testX)
    #print y_predict_list

    y_predict = []
    for i in y_predict_list:
        #print  i[0]
        if i[0] >= 0.5:
            y_predict.append(0)
        else:
            y_predict.append(1)

    print(classification_report(y_test, y_predict))
    print metrics.confusion_matrix(y_test, y_predict)
예제 #2
0
    def model(self,
              type=None,
              mode="train",
              num_layers=2,
              state_size=32,
              learning_rate=0.001,
              tensorboard_verbose=3):

        net = tflearn.input_data(shape=[None, 9])
        net = tflearn.embedding(net,
                                input_dim=21,
                                output_dim=32,
                                weights_init='xavier')

        if type == 'bi_rnn':
            out_rnn = tflearn.bidirectional_rnn(net, tflearn.BasicLSTMCell(32),
                                                tflearn.BasicLSTMCell(32))

        elif type == 'basic_lstm':
            for i in range(4):
                net = tflearn.lstm(net, n_units=40, return_seq=True)
            #net = tflearn.lstm(net, n_units=40, return_seq=True)
            out_rnn = tflearn.lstm(net, n_units=40, return_seq=False)

        elif type == 'basic_rnn':
            out_rnn = tflearn.simple_rnn(net, 40)

        else:
            out_rnn = net

        net = tflearn.fully_connected(out_rnn, 100, activation='prelu')
        net = tflearn.layers.normalization.batch_normalization(net)
        net = tflearn.dropout(net, 0.1)
        net = tflearn.fully_connected(net, 1, activation='sigmoid')
        """
        single_cell = getattr(tf.contrib.rnn, cell_type)(cell_size, state_is_tuple=True)

        if num_layers == 1:
            cell = single_cell
        else:
            cell = tf.contrib.rnn.MultiRNNCell([single_cell] * num_layers)
        """

        with tf.name_scope(
                "TargetsData"
        ):  # placeholder for target variable (i.e. trainY input)
            targetY = tf.placeholder(shape=[None, 1],
                                     dtype=tf.float32,
                                     name="Y")

        network = tflearn.regression(net,
                                     placeholder=targetY,
                                     optimizer=self.optimizer(learning_rate),
                                     learning_rate=learning_rate,
                                     loss=tflearn.mean_square(net, targetY),
                                     metric=self.accuracy(net, targetY),
                                     name='no rnn')

        model = tflearn.DNN(network, tensorboard_verbose=tensorboard_verbose)
        return model
예제 #3
0
def build_model(vocabFile, model_type='bilstm'):

    processor = VocabularyProcessor.restore(vocabFile)
    n_words = len(processor.vocabulary_)

    net = tflearn.input_data([None, 300])
    net = tflearn.embedding(net, input_dim=n_words, output_dim=200)

    if model_type == 'bilstm':
        net = tflearn.bidirectional_rnn(net, tflearn.BasicLSTMCell(200),
                                        tflearn.BasicLSTMCell(200))
        net = dropout(net, 0.5)
    elif model_type == 'lstm':
        net = tflearn.lstm(net, 200, dropout=0.5)
        net = dropout(net, 0.5)
    elif model_type == 'cnn':
        net = conv_model(net)

    net = tflearn.fully_connected(net, 2, activation='softmax')
    net = tflearn.regression(net,
                             optimizer='adam',
                             learning_rate=0.05,
                             loss='categorical_crossentropy')

    return net
예제 #4
0
def build_net3():
    """Bidirectional RNN"""
    net = tflearn.input_data(shape=[None, time_window, 1])
    lstm1 = tflearn.BasicLSTMCell(num_units=lstm_units)
    lstm2 = tflearn.BasicLSTMCell(num_units=lstm_units)
    #net = tflearn.fully_connected(net, 10, activation='sigmoid')
    #net = tflearn.reshape(net, [-1, 10, 1])
    #net = tflearn.lstm(net, dropout=(1.0, 0.5), n_units=lstm_units, return_seq=True)
    net = tflearn.bidirectional_rnn(net, lstm1, lstm2, return_seq=False)
    #net = tflearn.fully_connected(net, 5, activation='linear')
    #net = tflearn.dropout(net, 0.5)
    #net = tflearn.fully_connected(net, lstm_units, activation='relu')
    #net = tflearn.dropout(net, 0.5)

    #For model 2
    net = tflearn.fully_connected(net, predict_steps+1, activation='linear')

    #For model 1 and 3
    #net = tflearn.fully_connected(net, 1, activation='linear')

    #For model 4
    #net = tflearn.fully_connected(net, 3, activation='softmax')
    #net = tflearn.regression(net, metric='accuracy', optimizer=optimizer,
    #loss='categorical_crossentropy',
    #learning_rate=learn_rate, shuffle_batches=False)

    net = tflearn.regression(net, metric=None, optimizer=optimizer, loss='mean_square',
    learning_rate=learn_rate, shuffle_batches=False)

    model = tflearn.DNN(net, clip_gradients=0.0, tensorboard_verbose=0)
    return model
예제 #5
0
def bi_lstm(trainX, trainY, testX, testY):
    trainX = pad_sequences(trainX, maxlen=200, value=0.)
    testX = pad_sequences(testX, maxlen=200, value=0.)
    # Converting labels to binary vectors
    trainY = to_categorical(trainY, nb_classes=2)
    testY = to_categorical(testY, nb_classes=2)

    # Network building
    net = tflearn.input_data(shape=[None, 200])
    net = tflearn.embedding(net, input_dim=20000, output_dim=128)
    net = tflearn.bidirectional_rnn(net, BasicLSTMCell(128),
                                    BasicLSTMCell(128))
    net = tflearn.dropout(net, 0.5)
    net = tflearn.fully_connected(net, 2, activation='softmax')
    net = tflearn.regression(net,
                             optimizer='adam',
                             loss='categorical_crossentropy')

    # Training
    model = tflearn.DNN(net, clip_gradients=0., tensorboard_verbose=2)
    model.fit(trainX,
              trainY,
              validation_set=0.1,
              show_metric=True,
              batch_size=64,
              run_id="rnn-bilstm")
def simple_blstm():
	input_layer = tf.input_data(shape=[None, 5, 19])
	model = tf.bidirectional_rnn(input_layer, tf.BasicLSTMCell(91), tf.BasicLSTMCell(91))
	model = tf.dropout(model, 0.5)
	model = tf.fully_connected(model, 11, activation='sigmoid')

	sgd = tf.SGD(learning_rate=0.01, lr_decay=0.96, decay_step=1000)
	model = tf.regression(model, optimizer=sgd, loss='categorical_crossentropy')

	return tf.DNN(model, clip_gradients=0., tensorboard_verbose=0)
예제 #7
0
def run():
    net = tflearn.input_data([None, 100])
    net = tflearn.embedding(net, input_dim=20000, output_dim=128)
    net = tflearn.bidirectional_rnn(
        net, tflearn.BasicLSTMCell(128), tflearn.BasicLSTMCell(128))
    net = tflearn.dropout(net, 0.5)
    net = tflearn.fully_connected(net, 2, activation='softmax')
    net = tflearn.regression(
        net, optimizer='adam', loss='categorical_crossentropy')

    m = tflearn.DNN(net, clip_gradients=0., tensorboard_verbose=2)
    m.fit(trainX, trainY, validation_set=0.1, show_metric=True, batch_size=64)
    m.save('models/bidirectional_rnn.tfl')
예제 #8
0
def bi_lstm(trainX, trainY,testX, testY):
    trainX = pad_sequences(trainX, maxlen=200, value=0.)
    testX = pad_sequences(testX, maxlen=200, value=0.)
    # Converting labels to binary vectors
    trainY = to_categorical(trainY, nb_classes=2)
    testY = to_categorical(testY, nb_classes=2)

    # Network building
    net = tflearn.input_data(shape=[None, 200])
    net = tflearn.embedding(net, input_dim=20000, output_dim=128)
    net = tflearn.bidirectional_rnn(net, BasicLSTMCell(128), BasicLSTMCell(128))
    net = tflearn.dropout(net, 0.5)
    net = tflearn.fully_connected(net, 2, activation='softmax')
    net = tflearn.regression(net, optimizer='adam', loss='categorical_crossentropy')

    # Training
    model = tflearn.DNN(net, clip_gradients=0., tensorboard_verbose=2)
    model.fit(trainX, trainY, validation_set=0.1, show_metric=True, batch_size=64,run_id="rnn-bilstm")
예제 #9
0
    def add_deep_layers(self, net, model_type, out_embedding_dim, layer_size, n_layers):

        if model_type == 'embedding_rnn':
            out_rnn = tflearn.embedding(net, input_dim=21, output_dim=out_embedding_dim, weights_init='xavier')

        elif model_type == 'bi_rnn':
            out_rnn = tflearn.bidirectional_rnn(net, tflearn.BasicLSTMCell(layer_size), tflearn.BasicLSTMCell(layer_size))

        elif model_type == 'deep_rnn':
            for i in range(n_layers):
                net = tflearn.lstm(net, n_units=layer_size, return_seq=True)
            out_rnn = tflearn.lstm(net, layer_size)

        elif model_type == 'basic_rnn':
            out_rnn = tflearn.simple_rnn(net, layer_size)

        else:
            out_rnn = net

        return out_rnn
예제 #10
0
# X, Y, char_idx = \
#     textfile_to_semi_redundant_sequences(path, seq_maxlen=maxlen, redun_step=3)

# TODO - What do these look like?
# print(X)
# print(Y)

# Saves our dictionary for later use
pickle.dump(char_idx, open(char_idx_file, 'wb'))

# New
g = tflearn.input_data([None, maxlen, len(char_idx)])
# g = tflearn.fully_connected(g, len(char_idx), activation='softmax')
# g = tflearn.embedding(g, 10000, 342)
g = tflearn.bidirectional_rnn(g,
                              tflearn.BasicLSTMCell(342),
                              tflearn.BasicLSTMCell(342),
                              dynamic=True)
g = tflearn.dropout(g, 0.5)
g = tflearn.fully_connected(g, len(char_idx), activation='softmax')
g = tflearn.regression(g,
                       optimizer='adam',
                       loss='categorical_crossentropy',
                       learning_rate=0.001)

# Old
# g = tflearn.input_data([None, maxlen, len(char_idx)])
# g = tflearn.lstm(g, 512, return_seq=True)
# g = tflearn.dropout(g, 0.5)
# g = tflearn.lstm(g, 512, return_seq=True)
# g = tflearn.dropout(g, 0.5)
# g = tflearn.lstm(g, 512)