Demo entry 6781454

tensorflow

   

Submitted by anonymous on Jan 04, 2019 at 02:18
Language: Python 3. Code size: 4.5 kB.

# -*- coding: utf-8 -*-
"""
Created on Thu Jan  3 21:13:08 2019

@author: 小巷有狗
"""
import tensorflow as tf
import data_processing
import numpy as np
train_x, train_y = data_processing.dataProcessing('train.txt')
val_x , val_y  = data_processing.dataProcessing('dev.txt')
test_x, test_y = data_processing.loadTestFile('test.txt')
lstm_size = 256
lstm_layers = 1
batch_size = 500
learning_rate = 0.15
n_words = data_processing.get_nwords()

epochs = 100

def get_batchs(x, y, batch_size = 100):
    n_batchs = len(x) // batch_size
    x, y = x[:n_batchs * batch_size], y[:n_batchs * batch_size]
    for ii in range(0, len(x), batch_size):
        yield x[ii:ii+batch_size], y[ii:ii+batch_size]


def train():
    graph = tf.Graph()
    with graph.as_default():
        inputs = tf.placeholder(tf.int32, [None, None], name = 'inputs')
        labels = tf.placeholder(tf.int32, [None, None], name = 'labels')
        keep_prod = tf.placeholder(tf.float32, name = 'keep_prod')
    
        
        embed_size = 300
   
        embedding = tf.Variable(tf.random_uniform((n_words, embed_size), -1, 1))
        embed = tf.nn.embedding_lookup(embedding, inputs)

        lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
        
        drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prod)
        
        cell = tf.contrib.rnn.MultiRNNCell([drop] * lstm_layers)
        
        initial_state = cell.zero_state(batch_size, tf.float32)
   
        outputs, final_state = tf.nn.dynamic_rnn(cell, embed, initial_state=initial_state)
        #我们之只关心最终的输出结果,并用来作为情绪预测结果。我们用```outputs[:, -1]来获取最后的输出,并计算与labels的损失
        predictions = tf.contrib.layers.fully_connected(outputs[:, -1], 1, activation_fn=tf.sigmoid)
        cost = tf.losses.mean_squared_error(labels, predictions)
        
        optimizer = tf.train.AdadeltaOptimizer(learning_rate).minimize(cost)
        #计算准确率
        correct_pred = tf.equal(tf.cast(tf.round(predictions), tf.int32), labels)
        accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

 
        saver = tf.train.Saver()
     
    with tf.Session(graph=graph) as sess:
        sess.run(tf.global_variables_initializer())
        iteration = 1
        for e in range(epochs):
            state = sess.run(initial_state)
            
            for ii, (x, y) in enumerate(get_batchs(train_x, train_y, batch_size), 1):
                feed = {inputs : x,
                       labels : np.array(y)[:, None],
                       keep_prod : 0.5,
                       initial_state : state}
                loss, state, _ = sess.run([cost, final_state, optimizer], feed_dict = feed)
                
            if iteration % 5 == 0:
                print("Epoch: {}/{}".format(e, epochs),
                          "Iteration: {}".format(iteration),
                          "Train loss: {:.3f}".format(loss))
            if iteration % 25 == 0:
                val_acc = []
                val_state = sess.run(cell.zero_state(batch_size, tf.float32))
                for x, y in get_batchs(val_x, val_y, batch_size):
                    feed = {
                        inputs : x,
                        labels : np.array(y)[:, None],
                        keep_prod : 1,
                        initial_state : val_state
                    }
                    batch_acc, val_state = sess.run([accuracy, final_state], feed_dict = feed)
                    val_acc.append(batch_acc)
                print("Val acc: {:.3f}".format(np.mean(val_acc)))
            iteration += 1
        saver.save(sess, 'checkpoints/sentiment.ckpt')
        
        test_acc = []
        with tf.Session(graph = graph) as sess:
    
            saver.restore(sess, tf.train.latest_checkpoint('checkpoints'))
            test_state = sess.run(cell.zero_state(batch_size, tf.float32))
            for ii, (x, y) in enumerate(get_batchs(test_x, test_y, batch_size), 1):
                feed = {inputs : x,
                        labels : np.array(y)[:, None],
                        keep_prod : 1,
                        initial_state : test_state}
                batch_acc, test_state = sess.run([accuracy, final_state], feed_dict = feed)
                print(predictions)
                test_acc.append(batch_acc)
            print("Test accuracy: {:.3f}".format(np.mean(test_acc)))



if __name__ == '__main__':
    train()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).