#!/usr/bin/env python # # file: $ISIP_EXP/tuh_dpath/exp_0074/scripts/model.py # # revision history: # 20190925 (TE): first version # # usage: # # This script hold the model architecture #------------------------------------------------------------------------------ # import tensorflow modules # import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras import Sequential from tensorflow.keras.initializers import GlorotUniform # import modules # import os import random import numpy as np # define global values # SEED = 1337 DEF_NUM_FEATS = 2 NUM_NODES = 26 NUM_CLASSES = 2 NEW_LINE = "\n" #----------------------------------------------------------------------------- # # helper functions are listed here # #----------------------------------------------------------------------------- # function: set_seed # # arguments: seed - the seed for all the rng # # returns: none # # this method seeds all the random number generators and makes # the results deterministic # def set_seed(seed): random.seed(seed) os.environ['PYTHONHASHSEED'] = str(seed) tf.random.set_seed(seed) np.random.seed(seed) # # end of method # function: get_data # # arguments: fp - file pointer # num_feats - the number of features in a sample # # returns: data - the signals/features # labels - the correct labels for them # # this method takes in a fp and returns the data and labels # def get_data(fp, num_feats): # initialize the data and labels # data = [] labels = [] # for each line of the file # for line in fp.read().split(NEW_LINE): # split the string by white space # temp = line.split() # if we dont have feats + 1 label # if not (len(temp) > 1): continue # append the labels and data # labels.append(int(temp[0])) data.append([float(sample) for sample in temp[1:]]) # exit gracefully # return data, labels # # end of function #------------------------------------------------------------------------------ # # the model is defined here # #------------------------------------------------------------------------------ # function: Model # # arguments: input_size - int representing size of input # hidden_size - number of nodes in the hidden layer # num_classes - number of classes to classify # seed - the number to seed the layers' random weights # # return: model - the entire MLP model # # This method returns an MLP model # def Model(input_size, hidden_size, num_classes, seed): # define the model # model = Sequential() model.add(layers.Dense(hidden_size, activation='relu', input_shape=(input_size, ), kernel_initializer=GlorotUniform(seed=seed))) model.add(layers.Dense(num_classes, activation='softmax', input_shape=(hidden_size,), kernel_initializer=GlorotUniform(seed=seed))) # exit gracefully # return model # # end of function # # end of file