You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GroupLASSONN.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import tensorflow as tf
  2. import numpy as np
  3. import pandas as pd
  4. from pylab import rcParams
  5. import matplotlib.pyplot as plt
  6. import warnings
  7. from mlxtend.plotting import plot_decision_regions
  8. from matplotlib.colors import ListedColormap
  9. from tensorflow.keras.models import Sequential
  10. from tensorflow.keras.layers import Dense
  11. from sklearn.model_selection import train_test_split
  12. from group_lasso import BaseGroupLasso, GroupLasso
  13. from tensorflow import keras
  14. import math
  15. #using https://github.com/yngvem/group-lasso/
  16. warnings.filterwarnings('ignore')
  17. nd_path = ['nds/allnds_case.txt', 'nds/allnds_control.txt']
  18. X, y = load_dataset(nd_path)
  19. #https://github.com/bhattbhavesh91/regularization-neural-networks/blob/master/regularization-notebook.ipynb
  20. X_train, X_test, y_train, y_test = train_test_split(X, y,
  21. test_size=0.33,
  22. random_state=42)
  23. reg_model = Sequential()
  24. reg_model.add(Dense(math.sqrt(X_train.shape[1]), input_dim= X_train.shape[1], activation='relu'))
  25. reg_model.add(Dense(1, activation='sigmoid'))
  26. reg_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  27. reg_history = reg_model.fit(X_train, y_train,
  28. validation_data=(X_test, y_test),
  29. epochs=4000, verbose=1)
  30. # serialize model to JSON
  31. model_json = model.to_json()
  32. with open('model.json', 'w') as json_file:
  33. json_file.write(model_json)
  34. # serialize weights to HDF5
  35. model.save_weights('model.h5')
  36. print('Saved model to disk')
  37. # load json and create model
  38. json_file = open('model.json', 'r')
  39. loaded_model_json = json_file.read()
  40. json_file.close()
  41. loaded_model = model_from_json(loaded_model_json)
  42. # load weights into new model
  43. loaded_model.load_weights("model.h5")
  44. print("Loaded model from disk")
  45. # evaluate loaded model on test data
  46. # loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
  47. asd_path = ['nds/asd_case.txt', 'nds/asd_control.txt']
  48. X, y = load_dataset(asd_path)
  49. #https://github.com/bhattbhavesh91/regularization-neural-networks/blob/master/regularization-notebook.ipynb
  50. X_train, X_test, y_train, y_test = train_test_split(X, y,
  51. test_size=0.33,
  52. random_state=42)
  53. reg_model = Sequential()
  54. model.layers[0].set_weights(loaded_model[0])
  55. reg_model.add(Dense(math.sqrt(X_train.shape[1]), input_dim= X_train.shape[1], activation='relu', kernel_regularizer='group_lasso'))
  56. reg_model.add(Dense(1, activation='ReLU'))
  57. reg_model.add(Dense(1, activation='sigmoid'))
  58. reg_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  59. reg_history = reg_model.fit(X_train, y_train,
  60. validation_data=(X_test, y_test),
  61. epochs=4000, verbose=1)
  62. model = keras.models.load_model('path/to/saved/model')
  63. weights = model.get_layer('input').get_weights()
  64. values = weights[0]
  65. for indx, v in enumerate(values):
  66. if v!=0:
  67. print('nonzero input', indx)