Guest User

Untitled

a guest
Feb 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import numpy
  2. import pandas
  3. from keras.models import Sequential
  4. from keras.layers import Dense, Dropout
  5. from keras.wrappers.scikit_learn import KerasClassifier
  6. from keras.utils import np_utils
  7. from sklearn.preprocessing import LabelEncoder
  8. from tobow import tobow
  9.  
  10. numpy.random.seed(7)
  11.  
  12. model_path = "classifier/classifier.json"
  13. weights_path = "classifier/weights.h5"
  14. encoder_path = "classifier/encoder.npy"
  15.  
  16. training_data = "data/training/training-data.csv"
  17. training_label = "data/training/training-label.csv"
  18.  
  19.  
  20. X_dataframe = pandas.read_csv(training_data, header=None)
  21. X = X_dataframe.values
  22. Y_dataframe = pandas.read_csv(training_label, header=None)
  23. Y = Y_dataframe.values
  24.  
  25. dummy_x = []
  26. for text in X:
  27. dummy_x.append(numpy.array(tobow(text[0])[0]))
  28.  
  29. bow = numpy.array(dummy_x)
  30.  
  31. # encode class values as integers
  32. encoder = LabelEncoder()
  33. encoder.fit(Y)
  34. numpy.save(encoder_path,encoder.classes_)
  35. encoded_Y = encoder.transform(Y)
  36. # convert integers to dummy variables (i.e. one hot encoded)
  37. dummy_y = np_utils.to_categorical(encoded_Y)
  38.  
  39. # define baseline model
  40. def baseline_model():
  41. # create model
  42. model = Sequential()
  43. model.add(Dense(5000, input_shape=(len(bow[0]),), activation='relu'))
  44. model.add(Dropout(0.2))
  45. model.add(Dense(len(dummy_y[0]), activation='softmax'))
  46. # Compile model
  47. model.summary()
  48. model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  49. return model
  50.  
  51. model = baseline_model()
  52. #training process
  53. model.fit(bow, dummy_y, epochs=30)
  54.  
  55. #Save to Json
  56. model_json = model.to_json()
  57. with open(model_path, "w") as json_file:
  58. json_file.write(model_json)
  59. model.save_weights(weights_path)
  60. print "Model has been saved"
Add Comment
Please, Sign In to add comment