Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- #training file for LSTM rnn
- import random
- import time
- import shutil
- from tqdm import tqdm
- import os
- import numpy as np
- import pickle as pk
- import tensorflow as tf
- from Network import LSTM_Network
- epoch = 5
- with open('/d2/data/apps/sn_pred/modelTraining/Preprocessing_part/sorted_train_data.pkl','rb') as f:
- sorted_train_data = pk.load(f)
- with open('/d2/data/apps/sn_pred/modelTraining/Preprocessing_part/sorted_test_data.pkl','rb') as f:
- sorted_test_data = pk.load(f)
- with open('/d2/data/apps/sn_pred/modelTraining/Preprocessing_part/mapping_labels.pkl','rb') as f:
- mapping_labels = pk.load(f)
- print("len_of_cate",len(mapping_labels))
- # #sorted for minimal padding
- # sorted_train_data = sorted(train_data_in, key=lambda x: len(x))[2:]
- # sorted_test_data = sorted(test_data_in, key=lambda x: len(x))
- # #saving the test for validation
- # np.save('sorted_test_data', sorted_test_data)
- print("train_data", len(sorted_train_data))
- print("test_data", len(sorted_test_data))
- def sequence_padding(seqs_):
- """
- Padding the seq with same length for RNN
- :param seqs_:
- taking batch of without padded seqs as argument ex : [[278698,3442], [194661] , [1098,2341,77]]
- :return:
- padded with zeros with max len of seq in batch ex : [ [278698,3442,0] , [194661,0,0] ,[1098,2341,77]]
- :raise:
- if seq is not 2 dimension:
- TypeError: object of type 'int' has no len()
- """
- max_sequence = max(list(map(len, seqs_)))
- # getting Max length of sequence
- padded_sequence = [i + [0] * (max_sequence - len(i)) if len(i) < max_sequence else i for i in seqs_]
- # padded_sequence = list(map(lambda x:x + [0]*(max_sequence-len(x)) if len(x)<max_sequence else x,seqs))
- # padded sequence with max length of sequence
- return padded_sequence
- # return padded sequence
- def reshape_data(batch_seq_data):
- """
- Reshaping the sequences with np array format
- :param batch_seq_data:
- taking batch of seq as argument
- :return:
- batch of np array values
- other_columns.append((data_loads.iloc[m]['u_cause'],
- data_loads.iloc[m]['subcategory'],
- data_loads.iloc[m]['category'],
- data_loads.iloc[m]['priority'],
- data_loads.iloc[m]['severity'],
- data_loads.iloc[m]['urgency'],
- data_loads.iloc[m]['location'],
- data_loads.iloc[m]['opened_by'],
- data_loads.iloc[m]['sys_created_by'],
- data_loads.iloc[m]['u_business_service'],
- data_loads.iloc[m]['u_vendor']))
- """
- # inputs = padding([list(i[:-1]) for i in batch_seq_data], hj=j)
- # encoded_long_word_pad = sequence_padding([list(i[:-1]) for i in batch_seq_data])
- padded_long_word = sequence_padding([m[0] for m in batch_seq_data])
- padd_short_word = sequence_padding([m[1] for m in batch_seq_data])
- padd_long_char = sequence_padding([m[2] for m in batch_seq_data])
- padd_short_char = sequence_padding([m[3] for m in batch_seq_data])
- other_columns = [m[4] for m in batch_seq_data]
- u_causes = [m[0] for m in other_columns]
- subcategory = [m[1] for m in other_columns]
- category = [m[2] for m in other_columns]
- priority = [m[3] for m in other_columns]
- severity = [m[4] for m in other_columns]
- urgency = [m[5] for m in other_columns]
- location = [m[6] for m in other_columns]
- opened_by = [m[7] for m in other_columns]
- sys_created_by = [m[8] for m in other_columns]
- u_business_service = [m[9] for m in other_columns]
- u_vendor = [m[10] for m in other_columns],
- labels = [m[5] for m in batch_seq_data]
- return {
- 'padded_long_word' : np.array(padded_long_word),
- 'padd_short_word' : np.array(padd_short_word),
- 'padd_long_char' : np.array(padd_long_char),
- 'padd_short_char' : np.array(padd_short_char),
- 'oth_1' : np.array(u_causes),
- 'oth_2' : np.array(subcategory),
- 'oth_3' : np.array(category),
- 'oth_4' : np.array(priority),
- 'oth_5' : np.array(severity),
- 'oth_6' : np.array(urgency),
- 'oth_7' : np.array(location),
- 'oth_8' : np.array(opened_by),
- 'oth_9' : np.array(sys_created_by),
- 'oth_10' : np.array(u_business_service),
- 'oth_11' : np.array(u_vendor),
- 'lables' : np.array(labels)
- }
- def evaluate_(model, batch_size=50):
- """
- Checking the test accuracy on testing data set
- :param model:
- current lstm model
- :param batch_size:
- batch size for test data set
- :return:
- mean accuracy of test data set
- :raise:
- if input shape is different from placeholder shape:
- ValueError: Cannot feed value of shape
- """
- sess = tf.get_default_session()
- # batch_data = test_data_in
- # batch_labesl = train_labels
- iteration = len(sorted_test_data) // batch_size
- accuracy = []
- for i in range(iteration):
- batch_data_i = sorted_test_data[i * batch_size:(i + 1) * batch_size]
- input_test = reshape_data(batch_data_i)
- padded_long_word_e = input_test['padded_long_word']
- padd_short_word_e = input_test['padd_short_word']
- padd_long_char_e = input_test['padd_long_char']
- padd_short_char_e = input_test['padd_short_char']
- oth_1 = input_test['oth_1']
- oth_2 = input_test['oth_2']
- oth_3 = input_test['oth_3']
- oth_4 = input_test['oth_4']
- oth_5 = input_test['oth_5']
- oth_6 = input_test['oth_6']
- oth_7 = input_test['oth_7']
- oth_8 = input_test['oth_8']
- oth_9 = input_test['oth_9']
- oth_10 = input_test['oth_10']
- oth_11 = input_test['oth_11'][0]
- labels_e = input_test['lables']
- network_out = sess.run(model.output, feed_dict={model.placeholder['long_word_sente']: padded_long_word_e,
- model.placeholder['short_word_sente']: padd_short_word_e,
- model.placeholder['long_char_sente']: padd_long_char_e ,
- model.placeholder['short_char_sente']:padd_short_char_e,
- model.placeholder['oth_col_1']:oth_1,
- model.placeholder['oth_col_2']:oth_2,
- model.placeholder['oth_col_3']:oth_3,
- model.placeholder['oth_col_4']:oth_4,
- model.placeholder['oth_col_5']:oth_5,
- model.placeholder['oth_col_6']:oth_6,
- model.placeholder['oth_col_7']:oth_7,
- model.placeholder['oth_col_8']:oth_8,
- model.placeholder['oth_col_9']:oth_9,
- model.placeholder['oth_col_10']:oth_10,
- model.placeholder['oth_col_11']:oth_11,
- model.placeholder['label_s']:labels_e })
- accuracy.append(network_out['accuracy'])
- return np.mean(np.array(accuracy))
- def train_model(model, batch_size=50):
- """
- :param model:
- current lstm model
- :param batch_size:
- batch size for training
- :print:
- epoch
- iteration
- training_loss
- accuracy
- """
- saver = tf.train.Saver()
- with tf.Session() as sess:
- sess.run(tf.global_variables_initializer())
- iteration = len(sorted_train_data) // batch_size
- print("iteration", iteration)
- time.sleep(5)
- for i in range(epoch):
- for j in range(iteration):
- batch_data_j = sorted_train_data[j * batch_size:(j + 1) * batch_size]
- input_train = reshape_data(batch_data_j)
- padded_long_word_t = input_train['padded_long_word']
- padd_short_word_t = input_train['padd_short_word']
- padd_long_char_t = input_train['padd_long_char']
- padd_short_char_t = input_train['padd_short_char']
- oth_1 = input_train['oth_1']
- oth_2 = input_train['oth_2']
- oth_3 = input_train['oth_3']
- oth_4 = input_train['oth_4']
- oth_5 = input_train['oth_5']
- oth_6 = input_train['oth_6']
- oth_7 = input_train['oth_7']
- oth_8 = input_train['oth_8']
- oth_9 = input_train['oth_9']
- oth_10 = input_train['oth_10']
- oth_11 = input_train['oth_11'][0]
- #import ipdb; ipdb.set_trace();
- labels_t = input_train['lables']
- network_out, _ = sess.run([model.output, model.train],
- feed_dict={model.placeholder['long_word_sente']: padded_long_word_t,
- model.placeholder['short_word_sente']: padd_short_word_t,
- model.placeholder['long_char_sente']: padd_long_char_t ,
- model.placeholder['short_char_sente']:padd_short_char_t,
- model.placeholder['oth_col_1']:oth_1,
- model.placeholder['oth_col_2']:oth_2,
- model.placeholder['oth_col_3']:oth_3,
- model.placeholder['oth_col_4']:oth_4,
- model.placeholder['oth_col_5']:oth_5,
- model.placeholder['oth_col_6']:oth_6,
- model.placeholder['oth_col_7']:oth_7,
- model.placeholder['oth_col_8']:oth_8,
- model.placeholder['oth_col_9']:oth_9,
- model.placeholder['oth_col_10']:oth_10,
- model.placeholder['oth_col_11']:oth_11,
- model.placeholder['label_s']:labels_t })
- print({'epoch': i,
- 'iteration': j,
- 'training_loss': network_out['loss'],
- 'training_accuracy': network_out['accuracy']
- })
- if j % 100 == 0:
- with open('iterres.txt', 'a') as f:
- f.write(
- str({'epoch': i, 'test_accuracy': evaluate_(model, batch_size=150), 'iteration': j}) + '\n')
- os.system('mkdir ' + str(i) + 'epoch' + str(j))
- saver.save(sess, '.' + str(i) + 'epoch' + str(j) + '/' + str(i))
- if float(evaluate_(model, batch_size=150))>0.76:
- shutil.make_archive(str(i) + 'epoch' + str(j),'zip','.' + str(i) + 'epoch' + str(j))
- os.system('rm -rf ' + str(i) + 'epoch' + str(j))
- os.system('mkdir ' + str(i) + 'epoch' + str(j))
- saver.save(sess, '.' + str(i) + 'epoch' + str(j) + '/' + str(i))
- print({'epoch': i, 'test_accuracy': evaluate_(model)})
- with open('epochandresult', 'a') as f:
- f.write(str({'epoch': i, 'test_accuracy': evaluate_(model)}) + '\n')
- if __name__ == "__main__":
- model = LSTM_Network(dropout_value_= 0.5,
- word_vocab_size_= 30002,
- word_embedding_dim_= 150 ,
- forget_bias_= 1.0,
- rnn_num_units= 150,
- labels_nos = len(mapping_labels),
- char_vocab_size_ = 34 ,
- char_embedding_dim_= 50)
- train_model(model)
- #### Error: ValueError: Cannot feed value of shape (1, 50) for Tensor 'oth_col_11:0', which has shape '(?,)'
Add Comment
Please, Sign In to add comment