def __init__(self, path_to_model, input_tensor=None): """ Initializes the attributes for the class NeuralNetDetector. Parameters: ----------- path_to_model: str location of trained neural net autoencoder """ if not path_to_model.endswith('.ckpt'): path_to_model = path_to_model + '.ckpt' self.path_to_model = path_to_model # load parameter of autoencoder path_to_filters_ae = change_extension(path_to_model, 'yaml') self.ae_dict = load_yaml(path_to_filters_ae) n_input = self.ae_dict['n_input'] n_features = self.ae_dict['n_features'] # initialize autoencoder weight self.W_ae = tf.Variable( tf.random_uniform((n_input, n_features), -1.0 / np.sqrt(n_input), 1.0 / np.sqrt(n_input))) # create saver variables self.saver = tf.train.Saver({"W_ae": self.W_ae}) # make score tensorflow tensor from waveform self.score_tf = self._make_graph(input_tensor)
def __init__(self, path_to_ae_model): """ Initializes the attributes for the class NeuralNetDetector. Parameters: ----------- path_to_ae_model: str location of trained neural net autoencoder """ # add locations as attributes self.path_to_ae_model = path_to_ae_model # load parameter of autoencoder path_to_filters_ae = change_extension(path_to_ae_model, 'yaml') self.ae_dict = load_yaml(path_to_filters_ae) n_input = self.ae_dict['n_input'] n_features = self.ae_dict['n_features'] # initialize autoencoder weight self.W_ae = tf.Variable( tf.random_uniform((n_input, n_features), -1.0 / np.sqrt(n_input), 1.0 / np.sqrt(n_input))) # create saver variables self.saver_ae = tf.train.Saver({"W_ae": self.W_ae})
def __init__(self, path_to_triage_model): """ Initializes the attributes for the class NeuralNetDetector. Parameters: ----------- config: configuration file """ self.path_to_triage_model = path_to_triage_model path_to_filters = change_extension(path_to_triage_model, 'yaml') self.filters_dict = load_yaml(path_to_filters) R1 = self.filters_dict['size'] K1, K2 = self.filters_dict['filters'] C = self.filters_dict['n_neighbors'] self.W1 = weight_variable([R1, 1, 1, K1]) self.b1 = bias_variable([K1]) self.W11 = weight_variable([1, 1, K1, K2]) self.b11 = bias_variable([K2]) self.W2 = weight_variable([1, C, K2, 1]) self.b2 = bias_variable([1]) self.saver = tf.train.Saver({ "W1": self.W1, "W11": self.W11, "W2": self.W2, "b1": self.b1, "b11": self.b11, "b2": self.b2 })
def __init__(self, path_to_detector_model, path_to_ae_model): """ Initializes the attributes for the class NeuralNetDetector. Parameters: ----------- """ self.path_to_detector_model = path_to_detector_model self.path_to_ae_model = path_to_ae_model path_to_filters = change_extension(path_to_detector_model, 'yaml') self.filters_dict = load_yaml(path_to_filters) R1 = self.filters_dict['size'] K1, K2 = self.filters_dict['filters'] C = self.filters_dict['n_neighbors'] self.W1 = weight_variable([R1, 1, 1, K1]) self.b1 = bias_variable([K1]) self.W11 = weight_variable([1, 1, K1, K2]) self.b11 = bias_variable([K2]) self.W2 = weight_variable([1, C, K2, 1]) self.b2 = bias_variable([1]) # output of ae encoding (1st layer) path_to_filters_ae = change_extension(path_to_ae_model, 'yaml') ae_dict = load_yaml(path_to_filters_ae) n_input = ae_dict['n_input'] n_features = ae_dict['n_features'] self.W_ae = tf.Variable( tf.random_uniform((n_input, n_features), -1.0 / np.sqrt(n_input), 1.0 / np.sqrt(n_input))) self.saver_ae = tf.train.Saver({"W_ae": self.W_ae}) self.saver = tf.train.Saver({ "W1": self.W1, "W11": self.W11, "W2": self.W2, "b1": self.b1, "b11": self.b11, "b2": self.b2 })
def load(cls, path_to_model, input_tensor=None): if not path_to_model.endswith('.ckpt'): path_to_model = path_to_model + '.ckpt' # load parameter of autoencoder path_to_params = change_extension(path_to_model, 'yaml') params = load_yaml(path_to_params) return cls(path_to_model, params['waveform_length'], params['n_features'], input_tensor)
def load(cls, path_to_model, threshold, channel_index): if not path_to_model.endswith('.ckpt'): path_to_model = path_to_model+'.ckpt' # load nn parameter files path_to_params = change_extension(path_to_model, 'yaml') params = load_yaml(path_to_params) return cls(path_to_model, params['filters_size'], params['waveform_length'], params['n_neighbors'], threshold, channel_index)
def fit(self, x_train): """ Trains the autoencoder for feature extraction Parameters: ----------- x_train: np.array [number of training data, temporal length] noisy isolated spikes for training the autoencoder. y_train: np.array [number of training data, temporal length] clean (denoised) isolated spikes as labels. path_to_model: string name of the .ckpt to be saved. """ # FIXME: y_ae no longer used logger = logging.getLogger(__name__) # parameters n_data, waveform_length = x_train.shape self._validate_dimensions(waveform_length) pca = PCA(n_components=self.n_features).fit(x_train) self.vars_dict['W_ae'] = (tf.Variable( (pca.components_.T).astype('float32'))) # saver saver = tf.train.Saver(self.vars_dict) ############ # training # ############ logger.info('Training autoencoder network...') with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) saver.save(sess, self.path_to_model) path_to_params = change_extension(self.path_to_model, 'yaml') params = dict(waveform_length=self.waveform_length, n_features=self.n_features) self._save_params(path=path_to_params, params=params)
def train(cls, x_train, y_train, n_features, n_iter, n_batch, train_step_size, path_to_model): """ Trains the autoencoder for feature extraction Parameters: ----------- x_train: np.array [number of training data, temporal length] noisy isolated spikes for training the autoencoder. y_train: np.array [number of training data, temporal length] clean (denoised) isolated spikes as labels. path_to_model: string name of the .ckpt to be saved. """ logger = logging.getLogger(__name__) if not path_to_model.endswith('.ckpt'): path_to_model = path_to_model + '.ckpt' # parameters n_data, n_input = x_train.shape pca = PCA(n_components=n_features).fit(x_train) # encoding W_ae = tf.Variable((pca.components_.T).astype('float32')) # saver saver = tf.train.Saver({"W_ae": W_ae}) ############ # training # ############ logger.info('Training autoencoder network...') with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) saver.save(sess, path_to_model) save_ae_network_params(n_input=x_train.shape[1], n_features=n_features, output_path=change_extension( path_to_model, 'yaml'))
def load(cls, path_to_model, threshold, input_tensor=None, load_test_set=False): """Load a model from a file """ if not path_to_model.endswith('.ckpt'): path_to_model = path_to_model + '.ckpt' # load necessary parameters path_to_params = change_extension(path_to_model, 'yaml') params = load_yaml(path_to_params) return cls(path_to_model=path_to_model, filters_size=params['filters_size'], waveform_length=params['waveform_length'], n_neighbors=params['n_neighbors'], threshold=threshold, input_tensor=input_tensor, load_test_set=load_test_set)
def __init__(self, path_to_detector_model): """ Initializes the attributes for the class NeuralNetDetector. Parameters: ----------- path_to_detector_model: str location of trained neural net detectior """ # add locations as attributes self.path_to_detector_model = path_to_detector_model # load nn parameter files path_to_filters = change_extension(path_to_detector_model, 'yaml') self.filters_dict = load_yaml(path_to_filters) # initialize neural net weights and add as attributes R1 = self.filters_dict['size'] K1, K2 = self.filters_dict['filters'] C = self.filters_dict['n_neighbors'] self.W1 = weight_variable([R1, 1, 1, K1]) self.b1 = bias_variable([K1]) self.W11 = weight_variable([1, 1, K1, K2]) self.b11 = bias_variable([K2]) self.W2 = weight_variable([1, C, K2, 1]) self.b2 = bias_variable([1]) # create saver variables self.saver = tf.train.Saver({ "W1": self.W1, "W11": self.W11, "W2": self.W2, "b1": self.b1, "b11": self.b11, "b2": self.b2 })
def __init__(self, path_to_triage_model): """ Initializes the attributes for the class NeuralNetTriage. Parameters: ----------- path_to_detector_model: str location of trained neural net triage """ # save path to the model as an attribute self.path_to_triage_model = path_to_triage_model # load necessary parameters path_to_filters = change_extension(path_to_triage_model, 'yaml') self.filters_dict = load_yaml(path_to_filters) R1 = self.filters_dict['size'] K1, K2 = self.filters_dict['filters'] C = self.filters_dict['n_neighbors'] # initialize and save nn weights self.W1 = weight_variable([R1, 1, 1, K1]) self.b1 = bias_variable([K1]) self.W11 = weight_variable([1, 1, K1, K2]) self.b11 = bias_variable([K2]) self.W2 = weight_variable([1, C, K2, 1]) self.b2 = bias_variable([1]) # initialize savers self.saver = tf.train.Saver({ "W1": self.W1, "W11": self.W11, "W2": self.W2, "b1": self.b1, "b11": self.b11, "b2": self.b2 })
def train_neural_networks(CONFIG, CONFIG_TRAIN, spike_train, data_folder): """Train all neural networks Parameters ---------- """ logger = logging.getLogger(__name__) chosen_templates = CONFIG_TRAIN['templates']['ids'] min_amp = CONFIG_TRAIN['templates']['minimum_amplitude'] nspikes = CONFIG_TRAIN['training']['n_spikes'] n_filters_detect = CONFIG_TRAIN['network_detector']['n_filters'] n_iter = CONFIG_TRAIN['training']['n_iterations'] n_batch = CONFIG_TRAIN['training']['n_batch'] l2_reg_scale = CONFIG_TRAIN['training']['l2_regularization_scale'] train_step_size = CONFIG_TRAIN['training']['step_size'] detectnet_name = CONFIG_TRAIN['network_detector']['name'] + '.ckpt' n_filters_triage = CONFIG_TRAIN['network_triage']['n_filters'] triagenet_name = CONFIG_TRAIN['network_triage']['name'] + '.ckpt' n_features = CONFIG_TRAIN['network_autoencoder']['n_features'] ae_name = CONFIG_TRAIN['network_autoencoder']['name'] + '.ckpt' # generate training data logger.info('Generating training data...') (x_detect, y_detect, x_triage, y_triage, x_ae, y_ae) = make_training_data(CONFIG, spike_train, chosen_templates, min_amp, nspikes, data_folder=data_folder) # train detector logger.info('Training detector network...') train_detector(x_detect, y_detect, n_filters_detect, n_iter, n_batch, l2_reg_scale, train_step_size, detectnet_name) # save detector model parameters logger.info('Saving detector network parameters...') save_detect_network_params(filters=n_filters_detect, size=x_detect.shape[1], n_neighbors=x_detect.shape[2], output_path=change_extension( detectnet_name, 'yaml')) # train triage logger.info('Training triage network...') train_triage(x_triage, y_triage, n_filters_triage, n_iter, n_batch, l2_reg_scale, train_step_size, triagenet_name) # save triage model parameters logger.info('Saving triage network parameters...') save_triage_network_params(filters=n_filters_triage, size=x_detect.shape[1], n_neighbors=x_detect.shape[2], output_path=change_extension( triagenet_name, 'yaml')) # train autoencoder logger.info('Training autoencoder network...') train_ae(x_ae, y_ae, n_features, n_iter, n_batch, train_step_size, ae_name) # save autoencoder model parameters logger.info('Saving autoencoder network parameters...') save_ae_network_params(n_input=x_ae.shape[1], n_features=n_features, output_path=change_extension(ae_name, 'yaml'))
def fit(self, x_train, y_train, test_size=0.3, save_test_set=False): """Trains the triage network Parameters ---------- x_train: np.array [number of data, temporal length, number of channels] training data for the triage network. y_train: np.array [number of data] training label for the triage network. test_size: float, optional Proportion of the training set to be used, data is shuffled before splitting, defaults to 0.3 Returns ------- dict Dictionary with network parameters and metrics Notes ----- Size is determined but the second dimension in x_train """ ##################### # Splitting dataset # ##################### (self.x_train, self.x_test, self.y_train, self.y_test) = train_test_split(x_train, y_train, test_size=test_size) # get parameters n_data, waveform_length_train, n_neighbors_train = self.x_train.shape self._validate_dimensions(waveform_length_train, n_neighbors_train) # x and y input tensors x_tf = tf.placeholder("float", [None, self.waveform_length, self.n_neighbors]) y_tf = tf.placeholder("float", [None]) o_layer, vars_dict = (NeuralNetTriage._make_network( x_tf, self.filters_size, self.waveform_length, self.n_neighbors)) logits = tf.squeeze(o_layer) # cross entropy cross_entropy = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=y_tf)) # regularization term weights = tf.trainable_variables() l2_regularizer = (tf.contrib.layers.l2_regularizer( scale=self.l2_reg_scale)) regularization_penalty = tf.contrib.layers.apply_regularization( l2_regularizer, weights) regularized_loss = cross_entropy + regularization_penalty # train step train_step = tf.train.AdamOptimizer( self.train_step_size).minimize(regularized_loss) # saver saver = tf.train.Saver(vars_dict) ############ # training # ############ self.logger.debug('Training triage network...') with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) pbar = trange(self.n_iter) for i in pbar: idx_batch = np.random.choice(n_data, self.n_batch, replace=False) res = sess.run([train_step, regularized_loss], feed_dict={ x_tf: self.x_train[idx_batch], y_tf: self.y_train[idx_batch] }) if i % 100 == 0: # compute validation loss and metrics output = sess.run({'val loss': regularized_loss}, feed_dict={ x_tf: self.x_test, y_tf: self.y_test }) pbar.set_description('Tr loss: %s, ' 'Val loss: %s' % (res[1], output['val loss'])) self.logger.debug('Saving network: %s', self.path_to_model) saver.save(sess, self.path_to_model) path_to_params = change_extension(self.path_to_model, 'yaml') self.logger.debug('Saving network parameters: %s', path_to_params) params = dict(filters_size=self.filters_size, waveform_length=self.waveform_length, n_neighbors=self.n_neighbors, name=self.model_name) # compute metrics (print them and return them) metrics = self._evaluate() params.update(metrics) # save parameters to disk self._save_params(path=path_to_params, params=params) if save_test_set: self._save_test_set() return params
def fit(self, x_train, y_train): """ Trains the neural network detector for spike detection Parameters: ----------- x_train: np.array [number of training data, temporal length, number of channels] augmented training data consisting of isolated spikes, noise and misaligned spikes. y_train: np.array [number of training data] label for x_train. '1' denotes presence of an isolated spike and '0' denotes the presence of a noise data or misaligned spike. path_to_model: string name of the .ckpt to be saved """ ###################### # Loading parameters # ###################### logger = logging.getLogger(__name__) # get parameters n_data, waveform_length_train, n_neighbors_train = x_train.shape if self.waveform_length != waveform_length_train: raise ValueError('waveform length from network ({}) does not ' 'match training data ({})' .format(self.waveform_length, waveform_length_train)) if self.n_neighbors != n_neighbors_train: raise ValueError('number of n_neighbors from network ({}) does ' 'not match training data ({})' .format(self.n_neigh, n_neighbors_train)) #################### # Building network # #################### # x and y input tensors x_tf = tf.placeholder("float", [self.n_batch, self.waveform_length, self.n_neighbors]) y_tf = tf.placeholder("float", [self.n_batch]) input_tf = tf.expand_dims(x_tf, -1) vars_dict, layer11 = (NeuralNetDetector ._make_network(input_tf, self.waveform_length, self.filters_size, self.n_neighbors, padding='VALID')) W2 = vars_dict['W2'] b2 = vars_dict['b2'] # third layer: spatial convolution o_layer = tf.squeeze(conv2d_VALID(layer11, W2) + b2) ########################## # Optimization objective # ########################## # cross entropy _ = tf.nn.sigmoid_cross_entropy_with_logits(logits=o_layer, labels=y_tf) cross_entropy = tf.reduce_mean(_) weights = tf.trainable_variables() # regularization term l2_regularizer = (tf.contrib.layers .l2_regularizer(scale=self.l2_reg_scale)) regularization = tf.contrib.layers.apply_regularization(l2_regularizer, weights) regularized_loss = cross_entropy + regularization # train step train_step = (tf.train.AdamOptimizer(self.train_step_size) .minimize(regularized_loss)) ############ # Training # ############ # saver saver = tf.train.Saver(vars_dict) logger.debug('Training detector network...') with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) pbar = trange(self.n_iter) for i in pbar: # sample n_batch observations from 0, ..., n_data idx_batch = np.random.choice(n_data, self.n_batch, replace=False) res = sess.run([train_step, regularized_loss], feed_dict={x_tf: x_train[idx_batch], y_tf: y_train[idx_batch]}) if i % 100 == 0: pbar.set_description('Loss: %s' % res[1]) logger.debug('Saving network: %s', self.path_to_model) saver.save(sess, self.path_to_model) # estimate tp and fp with a sample idx_batch = np.random.choice(n_data, self.n_batch, replace=False) output = sess.run(o_layer, feed_dict={x_tf: x_train[idx_batch]}) y_test = y_train[idx_batch] tp = np.mean(output[y_test == 1] > 0) fp = np.mean(output[y_test == 0] > 0) logger.debug('Approximate training true positive rate: ' + str(tp) + ', false positive rate: ' + str(fp)) path_to_params = change_extension(self.path_to_model, 'yaml') logger.debug('Saving network parameters: %s', path_to_params) save_detect_network_params(filters_size=self.filters_size, waveform_length=self.waveform_length, n_neighbors=self.n_neighbors, output_path=path_to_params)
def fit(self, x_train, y_train, test_size=0.3, save_test_set=False): """ Trains the neural network detector for spike detection Parameters ---------- x_train: np.array [number of training data, temporal length, number of channels] augmented training data consisting of isolated spikes, noise and misaligned spikes. y_train: np.array [number of training data] label for x_train. '1' denotes presence of an isolated spike and '0' denotes the presence of a noise data or misaligned spike. test_size: float, optional Proportion of the training set to be used, data is shuffled before splitting, defaults to 0.3 Returns ------- dict Dictionary with network parameters and metrics """ logger = logging.getLogger(__name__) ##################### # Splitting dataset # ##################### (self.x_train, self.x_test, self.y_train, self.y_test) = train_test_split(x_train, y_train, test_size=test_size) ###################### # Loading parameters # ###################### # get parameters n_data, waveform_length_train, n_neighbors_train = self.x_train.shape self._validate_dimensions(waveform_length_train, n_neighbors_train) ########################## # Optimization objective # ########################## # cross entropy _ = tf.nn.sigmoid_cross_entropy_with_logits(logits=self.o_layer_tr, labels=self.y_tf_tr) cross_entropy = tf.reduce_mean(_) weights = tf.trainable_variables() # regularization term l2_regularizer = (tf.contrib.layers.l2_regularizer( scale=self.l2_reg_scale)) regularization = tf.contrib.layers.apply_regularization( l2_regularizer, weights) regularized_loss = cross_entropy + regularization # train step train_step = (tf.train.AdamOptimizer( self.train_step_size).minimize(regularized_loss)) ############ # Training # ############ # saver saver = tf.train.Saver(self.vars_dict) logger.debug('Training detector network...') with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) pbar = trange(self.n_iter) for i in pbar: # sample n_batch observations from 0, ..., n_data idx_batch = np.random.choice(n_data, self.n_batch, replace=False) x_train_batch = self.x_train[idx_batch] y_train_batch = self.y_train[idx_batch] # run a training step and compute training loss res = sess.run([train_step, regularized_loss], feed_dict={ self.x_tf_tr: x_train_batch, self.y_tf_tr: y_train_batch }) if i % 100 == 0: # compute validation loss and metrics output = sess.run({'val loss': regularized_loss}, feed_dict={ self.x_tf_tr: self.x_test, self.y_tf_tr: self.y_test }) pbar.set_description('Tr loss: %s, ' 'Val loss: %s' % (res[1], output['val loss'])) logger.debug('Saving network: %s', self.path_to_model) saver.save(sess, self.path_to_model) path_to_params = change_extension(self.path_to_model, 'yaml') logger.debug('Saving network parameters: %s', path_to_params) params = dict(filters_size=self.filters_size, waveform_length=self.waveform_length, n_neighbors=self.n_neighbors, name=self.model_name) # compute metrics (print them and return them) metrics = self._evaluate() params.update(metrics) # save parameters to disk self._save_params(path=path_to_params, params=params) if save_test_set: self._save_test_set() return params