SHARE
TWEET

Untitled

a guest Jun 27th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I have created with keras tensorflow.I use NSL KDD dataset.
  2.      
  3. import tensorflow as tf
  4. from keras import backend as K
  5.  
  6. from tensorflow.python.saved_model import builder as saved_model_builder
  7. from tensorflow.python.saved_model import tag_constants, signature_constants, signature_def_utils_impl
  8.  
  9. from keras.models import Sequential
  10. from keras.layers.core import Dense, Dropout, Activation
  11. from keras.optimizers import SGD
  12. import numpy as np
  13. sess = tf.Session()
  14. K.set_session(sess)
  15. K.set_learning_phase(0)
  16. model_version = "2"
  17.  
  18.  
  19. import numpy as np
  20. import matplotlib.pyplot as plt
  21. import pandas as pd
  22. # Importing the dataset
  23. dataset = pd.read_csv('KDD_Dataset.csv')
  24. X = dataset.iloc[:, :-1].values
  25. y = dataset.iloc[:, 41:42].values
  26. # Encoding categorical data X
  27. from sklearn.preprocessing import LabelEncoder
  28. labelencoder_X = LabelEncoder()
  29. X[:,0] = labelencoder_X.fit_transform(X[:,0])
  30. X[:,1] = labelencoder_X.fit_transform(X[:,1])
  31. X[:,2] = labelencoder_X.fit_transform(X[:,2])
  32. #
  33. from sklearn.preprocessing import OneHotEncoder
  34. onehotencoder_0 = OneHotEncoder(categorical_features=[0])
  35. onehotencoder_1 = OneHotEncoder(categorical_features=[1])
  36. onehotencoder_2 = OneHotEncoder(categorical_features=[2])
  37. X = onehotencoder_0.fit_transform(X).toarray()
  38. X = onehotencoder_1.fit_transform(X).toarray()
  39. X = onehotencoder_2.fit_transform(X).toarray()
  40.  
  41. # Encoding categorical data y
  42. from sklearn.preprocessing import LabelEncoder
  43. labelencoder_y = LabelEncoder()
  44. y = labelencoder_y.fit_transform(y)
  45. max(y)
  46.  
  47. # Splitting the dataset into the Training set and Test set
  48. #from sklearn.cross_validation import train_test_split
  49. from sklearn.model_selection import train_test_split
  50. X_train, X_test, y_train, y_test = train_test_split(X, y,
  51.                                                     test_size = 0.2,
  52.                                                     random_state = 0)
  53. # create the model
  54. model = Sequential()
  55. model.add(Dense(41, input_dim=8, init='uniform', activation='relu'))
  56. model.add(Dense(20, init='uniform', activation='relu'))
  57. model.add(Dense(1, init='uniform', activation='sigmoid'))
  58. # compile the model
  59.  
  60. model.compile(loss='binary_crossentropy', optimizer=sgd,metrics=['accuracy'])
  61.  
  62. model.fit(X_train, y_train, validation_data=(X_test, y_test), nb_epoch=200, batch_size=5, verbose=0)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top