code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
msg = struct.pack('>Q', counter) hs = hmac.new(key, msg, hashlib.sha1).digest() offset = six.indexbytes(hs, 19) & 0x0f val = struct.unpack('>L', hs[offset:offset + 4])[0] & 0x7fffffff return '{val:0{digits}d}'.format(val=val % 10 ** digits, digits=digits)
def hotp(key, counter, digits=6)
These test vectors come from RFC-4226 (https://tools.ietf.org/html/rfc4226#page-32). >>> key = b'12345678901234567890' >>> for c in range(10): ... hotp(key, c) '755224' '287082' '359152' '969429' '338314' '254676' '287922' '162583' '399871' '520489'
2.452462
2.442047
1.004265
if hasattr(t, 'timestamp'): timestamp = t.timestamp() else: # python 2 if t.tzinfo is None: timestamp = time.mktime(t.timetuple()) else: utc_naive = t.replace(tzinfo=None) - t.utcoffset() timestamp = (utc_naive - datetime.datetime(1970, 1, 1)).total_seconds() return int(timestamp) // step
def T(t, step=30)
The TOTP T value (number of time steps since the epoch)
2.581765
2.562455
1.007536
return hotp(key, T(t, step), digits)
def totp(key, t, digits=6, step=30)
These test vectors come from RFC-6238 (https://tools.ietf.org/html/rfc6238#appendix-B). >>> import datetime >>> key = b'12345678901234567890' >>> totp(key, datetime.datetime.fromtimestamp(59), digits=8) '94287082' >>> totp(key, datetime.datetime.fromtimestamp(1111111109), digits=8) '07081804' >>> totp(key, datetime.datetime.fromtimestamp(20000000000), digits=8) '65353130'
9.937113
28.242903
0.351845
with tf.Graph().as_default(), tf.Session() as self.tf_session: self.build_model() tf.global_variables_initializer().run() third = self.num_epochs // 3 for i in range(self.num_epochs): lr_decay = self.lr_decay ** max(i - third, 0.0) self.tf_session.run( tf.assign(self.lr_var, tf.multiply(self.learning_rate, lr_decay))) train_perplexity = self._run_train_step(train_set, 'train') print("Epoch: %d Train Perplexity: %.3f" % (i + 1, train_perplexity)) test_perplexity = self._run_train_step(test_set, 'test') print("Test Perplexity: %.3f" % test_perplexity)
def fit(self, train_set, test_set)
Fit the model to the given data. :param train_set: training data :param test_set: test data
2.255164
2.380933
0.947177
epoch_size = ((len(data) // self.batch_size) - 1) // self.num_steps costs = 0.0 iters = 0 step = 0 state = self._init_state.eval() op = self._train_op if mode == 'train' else tf.no_op() for step, (x, y) in enumerate( utilities.seq_data_iterator( data, self.batch_size, self.num_steps)): cost, state, _ = self.tf_session.run( [self.cost, self.final_state, op], {self.input_data: x, self.input_labels: y, self._init_state: state}) costs += cost iters += self.num_steps if step % (epoch_size // 10) == 10: print("%.3f perplexity" % (step * 1.0 / epoch_size)) return np.exp(costs / iters)
def _run_train_step(self, data, mode='train')
Run a single training step. :param data: input data :param mode: 'train' or 'test'.
2.335296
2.56391
0.910834
with tf.variable_scope( "model", reuse=None, initializer=self.initializer): self._create_placeholders() self._create_rnn_cells() self._create_initstate_and_embeddings() self._create_rnn_architecture() self._create_optimizer_node()
def build_model(self)
Build the model's computational graph.
4.675543
4.281501
1.092034
self.input_data = tf.placeholder( tf.int32, [self.batch_size, self.num_steps]) self.input_labels = tf.placeholder( tf.int32, [self.batch_size, self.num_steps])
def _create_placeholders(self)
Create the computational graph's placeholders.
2.010957
1.851438
1.086159
lstm_cell = tf.nn.rnn_cell.LSTMCell( self.num_hidden, forget_bias=0.0) lstm_cell = tf.nn.rnn_cell.DropoutWrapper( lstm_cell, output_keep_prob=self.dropout) self.cell = tf.nn.rnn_cell.MultiRNNCell( [lstm_cell] * self.num_layers)
def _create_rnn_cells(self)
Create the LSTM cells.
1.790144
1.696761
1.055036
self._init_state = self.cell.zero_state(self.batch_size, tf.float32) embedding = tf.get_variable( "embedding", [self.vocab_size, self.num_hidden]) inputs = tf.nn.embedding_lookup(embedding, self.input_data) self.inputs = tf.nn.dropout(inputs, self.dropout)
def _create_initstate_and_embeddings(self)
Create the initial state for the cell and the data embeddings.
2.195997
1.994905
1.100803
self.inputs = [tf.squeeze(i, [1]) for i in tf.split( axis=1, num_or_size_splits=self.num_steps, value=self.inputs)] outputs, state = tf.nn.rnn( self.cell, self.inputs, initial_state=self._init_state) output = tf.reshape(tf.concat(axis=1, values=outputs), [-1, self.num_hidden]) softmax_w = tf.get_variable( "softmax_w", [self.num_hidden, self.vocab_size]) softmax_b = tf.get_variable("softmax_b", [self.vocab_size]) logits = tf.add(tf.matmul(output, softmax_w), softmax_b) loss = tf.nn.seq2seq.sequence_loss_by_example( [logits], [tf.reshape(self.input_labels, [-1])], [tf.ones([self.batch_size * self.num_steps])]) self.cost = tf.div(tf.reduce_sum(loss), self.batch_size) self.final_state = state
def _create_rnn_architecture(self)
Create the training architecture and the last layer of the LSTM.
1.730312
1.666353
1.038383
self.lr_var = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() grads, _ = tf.clip_by_global_norm(tf.gradients(self.cost, tvars), self.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self.lr_var) self._train_op = optimizer.apply_gradients(zip(grads, tvars))
def _create_optimizer_node(self)
Create the optimizer node of the graph.
1.723865
1.606339
1.073164
self.input_data = tf.placeholder( tf.float32, [None, n_features], name='x-input') self.input_labels = tf.placeholder( tf.float32, [None, n_classes], name='y-input') self.keep_prob = tf.placeholder( tf.float32, name='keep-probs')
def _create_placeholders(self, n_features, n_classes)
Create the TensorFlow placeholders for the model. :param n_features: number of features of the first layer :param n_classes: number of classes :return: self
1.798876
1.905452
0.944068
if self.do_pretrain: self._create_variables_pretrain() else: self._create_variables_no_pretrain(n_features)
def _create_variables(self, n_features)
Create the TensorFlow variables for the model. :param n_features: number of features :return: self
3.451294
3.925236
0.879258
self.encoding_w_ = [] self.encoding_b_ = [] for l, layer in enumerate(self.layers): if l == 0: self.encoding_w_.append(tf.Variable(tf.truncated_normal( shape=[n_features, self.layers[l]], stddev=0.1))) self.encoding_b_.append(tf.Variable(tf.truncated_normal( [self.layers[l]], stddev=0.1))) else: self.encoding_w_.append(tf.Variable(tf.truncated_normal( shape=[self.layers[l - 1], self.layers[l]], stddev=0.1))) self.encoding_b_.append(tf.Variable(tf.truncated_normal( [self.layers[l]], stddev=0.1)))
def _create_variables_no_pretrain(self, n_features)
Create model variables (no previous unsupervised pretraining). :param n_features: number of features :return: self
1.609972
1.65984
0.969956
for l, layer in enumerate(self.layers): self.encoding_w_[l] = tf.Variable( self.encoding_w_[l], name='enc-w-{}'.format(l)) self.encoding_b_[l] = tf.Variable( self.encoding_b_[l], name='enc-b-{}'.format(l))
def _create_variables_pretrain(self)
Create model variables (previous unsupervised pretraining). :return: self
2.600356
2.819436
0.922297
next_train = self.input_data self.layer_nodes = [] for l, layer in enumerate(self.layers): with tf.name_scope("encode-{}".format(l)): y_act = tf.add( tf.matmul(next_train, self.encoding_w_[l]), self.encoding_b_[l] ) if self.finetune_enc_act_func[l] is not None: layer_y = self.finetune_enc_act_func[l](y_act) else: layer_y = None # the input to the next layer is the output of this layer next_train = tf.nn.dropout(layer_y, self.keep_prob) self.layer_nodes.append(next_train) self.encode = next_train
def _create_encoding_layers(self)
Create the encoding layers for supervised finetuning. :return: output of the final encoding layer.
3.399144
3.432483
0.990287
next_decode = self.encode for l, layer in reversed(list(enumerate(self.layers))): with tf.name_scope("decode-{}".format(l)): # Create decoding variables if self.tied_weights: dec_w = tf.transpose(self.encoding_w_[l]) else: dec_w = tf.Variable(tf.transpose( self.encoding_w_[l].initialized_value())) dec_b = tf.Variable(tf.constant( 0.1, shape=[dec_w.get_shape().dims[1].value])) self.decoding_w.append(dec_w) self.decoding_b.append(dec_b) y_act = tf.add( tf.matmul(next_decode, dec_w), dec_b ) if self.finetune_dec_act_func[l] is not None: layer_y = self.finetune_dec_act_func[l](y_act) else: layer_y = None # the input to the next layer is the output of this layer next_decode = tf.nn.dropout(layer_y, self.keep_prob) self.layer_nodes.append(next_decode) self.reconstruction = next_decode
def _create_decoding_layers(self)
Create the decoding layers for reconstruction finetuning. :return: output of the final encoding layer.
3.087573
3.074942
1.004108
mnist = input_data.read_data_sets("MNIST_data/", one_hot=one_hot) # Training set trX = mnist.train.images trY = mnist.train.labels # Validation set vlX = mnist.validation.images vlY = mnist.validation.labels # Test set teX = mnist.test.images teY = mnist.test.labels if mode == 'supervised': return trX, trY, vlX, vlY, teX, teY elif mode == 'unsupervised': return trX, vlX, teX
def load_mnist_dataset(mode='supervised', one_hot=True)
Load the MNIST handwritten digits dataset. :param mode: 'supervised' or 'unsupervised' mode :param one_hot: whether to get one hot encoded labels :return: train, validation, test data: for (X, y) if 'supervised', for (X) if 'unsupervised'
1.478827
1.585658
0.932627
# Training set trX = None trY = np.array([]) # Test set teX = np.array([]) teY = np.array([]) for fn in os.listdir(cifar_dir): if not fn.startswith('batches') and not fn.startswith('readme'): fo = open(os.path.join(cifar_dir, fn), 'rb') data_batch = pickle.load(fo) fo.close() if fn.startswith('data'): if trX is None: trX = data_batch['data'] trY = data_batch['labels'] else: trX = np.concatenate((trX, data_batch['data']), axis=0) trY = np.concatenate((trY, data_batch['labels']), axis=0) if fn.startswith('test'): teX = data_batch['data'] teY = data_batch['labels'] trX = trX.astype(np.float32) / 255. teX = teX.astype(np.float32) / 255. if mode == 'supervised': return trX, trY, teX, teY elif mode == 'unsupervised': return trX, teX
def load_cifar10_dataset(cifar_dir, mode='supervised')
Load the cifar10 dataset. :param cifar_dir: path to the dataset directory (cPicle format from: https://www.cs.toronto.edu/~kriz/cifar.html) :param mode: 'supervised' or 'unsupervised' mode :return: train, test data: for (X, y) if 'supervised', for (X) if 'unsupervised'
1.727785
1.808985
0.955113
with tf.name_scope(name): in_dim = prev_layer.get_shape()[1].value W = tf.Variable(tf.truncated_normal([in_dim, out_dim], stddev=0.1)) b = tf.Variable(tf.constant(0.1, shape=[out_dim])) out = tf.add(tf.matmul(prev_layer, W), b) return (out, W, b)
def linear(prev_layer, out_dim, name="linear")
Create a linear fully-connected layer. Parameters ---------- prev_layer : tf.Tensor Last layer's output tensor. out_dim : int Number of output units. Returns ------- tuple ( tf.Tensor : Linear output tensor tf.Tensor : Linear weights variable tf.Tensor : Linear biases variable )
1.559873
1.623201
0.960986
with tf.name_scope(name): if regtype != 'none': regs = tf.constant(0.0) for v in variables: if regtype == 'l2': regs = tf.add(regs, tf.nn.l2_loss(v)) elif regtype == 'l1': regs = tf.add(regs, tf.reduce_sum(tf.abs(v))) return tf.multiply(regcoef, regs) else: return None
def regularization(variables, regtype, regcoef, name="regularization")
Compute the regularization tensor. Parameters ---------- variables : list of tf.Variable List of model variables. regtype : str Type of regularization. Can be ["none", "l1", "l2"] regcoef : float, Regularization coefficient. name : str, optional (default = "regularization") Name for the regularization op. Returns ------- tf.Tensor : Regularization tensor.
1.771316
1.844392
0.96038
with tf.name_scope(name): mod_pred = tf.argmax(mod_y, 1) correct_pred = tf.equal(mod_pred, tf.argmax(ref_y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) if summary: tf.summary.scalar('accuracy', accuracy) return accuracy
def accuracy(mod_y, ref_y, summary=True, name="accuracy")
Accuracy computation op. Parameters ---------- mod_y : tf.Tensor Model output tensor. ref_y : tf.Tensor Reference input tensor. summary : bool, optional (default = True) Whether to save tf summary for the op. Returns ------- tf.Tensor : accuracy op. tensor
1.585064
1.82578
0.868157
self._create_placeholders(n_features, n_classes) self._create_variables(n_features, n_classes) self.mod_y = tf.nn.softmax( tf.add(tf.matmul(self.input_data, self.W_), self.b_)) self.cost = self.loss.compile(self.mod_y, self.input_labels) self.train_step = tf.train.GradientDescentOptimizer( self.learning_rate).minimize(self.cost) self.accuracy = Evaluation.accuracy(self.mod_y, self.input_labels)
def build_model(self, n_features, n_classes)
Create the computational graph. :param n_features: number of features :param n_classes: number of classes :return: self
2.684997
2.812569
0.954642
self.W_ = tf.Variable( tf.zeros([n_features, n_classes]), name='weights') self.b_ = tf.Variable( tf.zeros([n_classes]), name='biases')
def _create_variables(self, n_features, n_classes)
Create the TensorFlow variables for the model. :param n_features: number of features :param n_classes: number of classes :return: self
2.064609
2.382578
0.866544
next_train = train_set next_valid = validation_set for l, layer_obj in enumerate(layer_objs): print('Training layer {}...'.format(l + 1)) next_train, next_valid = self._pretrain_layer_and_gen_feed( layer_obj, set_params_func, next_train, next_valid, layer_graphs[l]) return next_train, next_valid
def pretrain_procedure(self, layer_objs, layer_graphs, set_params_func, train_set, validation_set=None)
Perform unsupervised pretraining of the model. :param layer_objs: list of model objects (autoencoders or rbms) :param layer_graphs: list of model tf.Graph objects :param set_params_func: function used to set the parameters after pretraining :param train_set: training set :param validation_set: validation set :return: return data encoded by the last layer
2.79985
3.276072
0.854636
layer_obj.fit(train_set, train_set, validation_set, validation_set, graph=graph) with graph.as_default(): set_params_func(layer_obj, graph) next_train = layer_obj.transform(train_set, graph=graph) if validation_set is not None: next_valid = layer_obj.transform(validation_set, graph=graph) else: next_valid = None return next_train, next_valid
def _pretrain_layer_and_gen_feed(self, layer_obj, set_params_func, train_set, validation_set, graph)
Pretrain a single autoencoder and encode the data for the next layer. :param layer_obj: layer model :param set_params_func: function used to set the parameters after pretraining :param train_set: training set :param validation_set: validation set :param graph: tf object for the rbm :return: encoded train data, encoded validation data
2.288198
2.60668
0.877821
layers_out = [] with self.tf_graph.as_default(): with tf.Session() as self.tf_session: self.tf_saver.restore(self.tf_session, self.model_path) for l in self.layer_nodes: layers_out.append(l.eval({self.input_data: dataset, self.keep_prob: 1})) if layers_out == []: raise Exception("This method is not implemented for this model") else: return layers_out
def get_layers_output(self, dataset)
Get output from each layer of the network. :param dataset: input data :return: list of np array, element i is the output of layer i
2.853119
2.868888
0.994504
g = graph if graph is not None else self.tf_graph with g.as_default(): with tf.Session() as self.tf_session: self.tf_saver.restore(self.tf_session, self.model_path) out = {} for par in params: if type(params[par]) == list: for i, p in enumerate(params[par]): out[par + '-' + str(i+1)] = p.eval() else: out[par] = params[par].eval() return out
def get_parameters(self, params, graph=None)
Get the parameters of the model. :param params: dictionary of keys (str names) and values (tensors). :return: evaluated tensors in params
2.27003
2.391631
0.949155
if len(train_Y.shape) != 1: num_classes = train_Y.shape[1] else: raise Exception("Please convert the labels with one-hot encoding.") g = graph if graph is not None else self.tf_graph with g.as_default(): # Build model self.build_model(train_X.shape[1], num_classes) with tf.Session() as self.tf_session: # Initialize tf stuff summary_objs = tf_utils.init_tf_ops(self.tf_session) self.tf_merged_summaries = summary_objs[0] self.tf_summary_writer = summary_objs[1] self.tf_saver = summary_objs[2] # Train model self._train_model(train_X, train_Y, val_X, val_Y) # Save model self.tf_saver.save(self.tf_session, self.model_path)
def fit(self, train_X, train_Y, val_X=None, val_Y=None, graph=None)
Fit the model to the data. Parameters ---------- train_X : array_like, shape (n_samples, n_features) Training data. train_Y : array_like, shape (n_samples, n_classes) Training labels. val_X : array_like, shape (N, n_features) optional, (default = None). Validation data. val_Y : array_like, shape (N, n_classes) optional, (default = None). Validation labels. graph : tf.Graph, optional (default = None) Tensorflow Graph object. Returns -------
2.57239
2.677833
0.960623
with self.tf_graph.as_default(): with tf.Session() as self.tf_session: self.tf_saver.restore(self.tf_session, self.model_path) feed = { self.input_data: test_X, self.keep_prob: 1 } return self.mod_y.eval(feed)
def predict(self, test_X)
Predict the labels for the test set. Parameters ---------- test_X : array_like, shape (n_samples, n_features) Test data. Returns ------- array_like, shape (n_samples,) : predicted labels.
2.64261
3.091104
0.854908
with self.tf_graph.as_default(): with tf.Session() as self.tf_session: self.tf_saver.restore(self.tf_session, self.model_path) feed = { self.input_data: test_X, self.input_labels: test_Y, self.keep_prob: 1 } return self.accuracy.eval(feed)
def score(self, test_X, test_Y)
Compute the mean accuracy over the test set. Parameters ---------- test_X : array_like, shape (n_samples, n_features) Test data. test_Y : array_like, shape (n_samples, n_features) Test labels. Returns ------- float : mean accuracy over the test set
2.191324
2.517377
0.870479
self.do_pretrain = True def set_params_func(autoenc, autoencgraph): params = autoenc.get_parameters(graph=autoencgraph) self.encoding_w_.append(params['enc_w']) self.encoding_b_.append(params['enc_b']) return SupervisedModel.pretrain_procedure( self, self.autoencoders, self.autoencoder_graphs, set_params_func=set_params_func, train_set=train_set, validation_set=validation_set)
def pretrain(self, train_set, validation_set=None)
Perform Unsupervised pretraining of the autoencoder.
4.686777
4.533575
1.033793
self._create_placeholders(n_features, n_classes) self._create_variables(n_features) next_train = self._create_encoding_layers() self.mod_y, _, _ = Layers.linear(next_train, n_classes) self.layer_nodes.append(self.mod_y) self.cost = self.loss.compile(self.mod_y, self.input_labels) self.train_step = self.trainer.compile(self.cost) self.accuracy = Evaluation.accuracy(self.mod_y, self.input_labels)
def build_model(self, n_features, n_classes)
Create the computational graph. This graph is intented to be created for finetuning, i.e. after unsupervised pretraining. :param n_features: Number of features. :param n_classes: number of classes. :return: self
4.010767
4.293543
0.934139
self.encoding_w_ = [] self.encoding_b_ = [] for l, layer in enumerate(self.layers): w_name = 'enc-w-{}'.format(l) b_name = 'enc-b-{}'.format(l) if l == 0: w_shape = [n_features, self.layers[l]] else: w_shape = [self.layers[l - 1], self.layers[l]] w_init = tf.truncated_normal(shape=w_shape, stddev=0.1) W = tf.Variable(w_init, name=w_name) tf.summary.histogram(w_name, W) self.encoding_w_.append(W) b_init = tf.constant(0.1, shape=[self.layers[l]]) b = tf.Variable(b_init, name=b_name) tf.summary.histogram(b_name, b) self.encoding_b_.append(b)
def _create_variables_no_pretrain(self, n_features)
Create model variables (no previous unsupervised pretraining). :param n_features: number of features :return: self
1.75208
1.802631
0.971957
summary_merged = tf.summary.merge_all() init_op = tf.global_variables_initializer() saver = tf.train.Saver() sess.run(init_op) # Retrieve run identifier run_id = 0 for e in os.listdir(Config().logs_dir): if e[:3] == 'run': r = int(e[3:]) if r > run_id: run_id = r run_id += 1 run_dir = os.path.join(Config().logs_dir, 'run' + str(run_id)) print('Tensorboard logs dir for this run is %s' % (run_dir)) summary_writer = tf.summary.FileWriter(run_dir, sess.graph) return (summary_merged, summary_writer, saver)
def init_tf_ops(sess)
Initialize TensorFlow operations. This function initialize the following tensorflow ops: * init variables ops * summary ops * create model saver Parameters ---------- sess : object Tensorflow `Session` object Returns ------- tuple : (summary_merged, summary_writer) * tf merged summaries object * tf summary writer object * tf saver object
2.402615
2.474165
0.971081
try: result = sess.run([merged_summaries, tens], feed_dict=feed) summary_str = result[0] out = result[1] summary_writer.add_summary(summary_str, epoch) except tf.errors.InvalidArgumentError: out = sess.run(tens, feed_dict=feed) return out
def run_summaries( sess, merged_summaries, summary_writer, epoch, feed, tens)
Run the summaries and error computation on the validation set. Parameters ---------- sess : tf.Session Tensorflow session object. merged_summaries : tf obj Tensorflow merged summaries obj. summary_writer : tf.summary.FileWriter Tensorflow summary writer obj. epoch : int Current training epoch. feed : dict Validation feed dict. tens : tf.Tensor Tensor to display and evaluate during training. Can be self.accuracy for SupervisedModel or self.cost for UnsupervisedModel. Returns ------- err : float, mean error over the validation set.
1.994191
2.226703
0.89558
self.do_pretrain = True def set_params_func(rbmmachine, rbmgraph): params = rbmmachine.get_parameters(graph=rbmgraph) self.encoding_w_.append(params['W']) self.encoding_b_.append(params['bh_']) return SupervisedModel.pretrain_procedure( self, self.rbms, self.rbm_graphs, set_params_func=set_params_func, train_set=train_set, validation_set=validation_set)
def pretrain(self, train_set, validation_set=None)
Perform Unsupervised pretraining of the DBN.
5.872273
5.826441
1.007866
for l, layer in enumerate(self.layers): w_name = 'enc-w-{}'.format(l) b_name = 'enc-b-{}'.format(l) self.encoding_w_[l] = tf.Variable( self.encoding_w_[l], name=w_name) tf.summary.histogram(w_name, self.encoding_w_[l]) self.encoding_b_[l] = tf.Variable( self.encoding_b_[l], name=b_name) tf.summary.histogram(b_name, self.encoding_w_[l])
def _create_variables_pretrain(self)
Create model variables (previous unsupervised pretraining). :return: self
2.144416
2.229868
0.961678
pbar = tqdm(range(self.num_epochs)) for i in pbar: self._run_train_step(train_X) if val_X is not None: feed = {self.input_data_orig: val_X, self.input_data: val_X} err = tf_utils.run_summaries( self.tf_session, self.tf_merged_summaries, self.tf_summary_writer, i, feed, self.cost) pbar.set_description("Reconstruction loss: %s" % (err)) return self
def _train_model(self, train_X, train_Y=None, val_X=None, val_Y=None)
Train the model. Parameters ---------- train_X : array_like Training data, shape (num_samples, num_features). train_Y : array_like, optional (default = None) Reference training data, shape (num_samples, num_features). val_X : array_like, optional, default None Validation data, shape (num_val_samples, num_features). val_Y : array_like, optional, default None Reference validation data, shape (num_val_samples, num_features). Returns ------- self : trained model instance
3.66841
4.110765
0.892391
x_corrupted = utilities.corrupt_input( train_X, self.tf_session, self.corr_type, self.corr_frac) shuff = list(zip(train_X, x_corrupted)) np.random.shuffle(shuff) batches = [_ for _ in utilities.gen_batches(shuff, self.batch_size)] for batch in batches: x_batch, x_corr_batch = zip(*batch) tr_feed = {self.input_data_orig: x_batch, self.input_data: x_corr_batch} self.tf_session.run(self.train_step, feed_dict=tr_feed)
def _run_train_step(self, train_X)
Run a training step. A training step is made by randomly corrupting the training set, randomly shuffling it, divide it into batches and run the optimizer for each batch. Parameters ---------- train_X : array_like Training data, shape (num_samples, num_features). Returns ------- self
2.944478
3.18007
0.925916
self._create_placeholders(n_features) self._create_variables(n_features, W_, bh_, bv_) self._create_encode_layer() self._create_decode_layer() variables = [self.W_, self.bh_, self.bv_] regterm = Layers.regularization(variables, self.regtype, self.regcoef) self.cost = self.loss.compile( self.reconstruction, self.input_data_orig, regterm=regterm) self.train_step = self.trainer.compile(self.cost)
def build_model(self, n_features, W_=None, bh_=None, bv_=None)
Create the computational graph. Parameters ---------- n_features : int Number of units in the input layer. W_ : array_like, optional (default = None) Weight matrix np array. bh_ : array_like, optional (default = None) Hidden bias np array. bv_ : array_like, optional (default = None) Visible bias np array. Returns ------- self
4.061566
4.375747
0.928199
if W_: self.W_ = tf.Variable(W_, name='enc-w') else: self.W_ = tf.Variable( tf.truncated_normal( shape=[n_features, self.n_components], stddev=0.1), name='enc-w') if bh_: self.bh_ = tf.Variable(bh_, name='hidden-bias') else: self.bh_ = tf.Variable(tf.constant( 0.1, shape=[self.n_components]), name='hidden-bias') if bv_: self.bv_ = tf.Variable(bv_, name='visible-bias') else: self.bv_ = tf.Variable(tf.constant( 0.1, shape=[n_features]), name='visible-bias')
def _create_variables(self, n_features, W_=None, bh_=None, bv_=None)
Create the TensorFlow variables for the model. :return: self
1.637626
1.681158
0.974106
with tf.name_scope("encoder"): activation = tf.add( tf.matmul(self.input_data, self.W_), self.bh_ ) if self.enc_act_func: self.encode = self.enc_act_func(activation) else: self.encode = activation return self
def _create_encode_layer(self)
Create the encoding layer of the network. Returns ------- self
4.099478
4.236703
0.96761
with tf.name_scope("decoder"): activation = tf.add( tf.matmul(self.encode, tf.transpose(self.W_)), self.bv_ ) if self.dec_act_func: self.reconstruction = self.dec_act_func(activation) else: self.reconstruction = activation return self
def _create_decode_layer(self)
Create the decoding layer of the network. Returns ------- self
4.443258
4.625976
0.960502
return tf.nn.relu(tf.sign(probs - rand))
def sample_prob(probs, rand)
Get samples from a tensor of probabilities. :param probs: tensor of probabilities :param rand: tensor (of the same shape as probs) of random values :return: binary sample of probabilities
6.427274
10.436087
0.61587
corruption_ratio = np.round(corrfrac * data.shape[1]).astype(np.int) if corrtype == 'none': return np.copy(data) if corrfrac > 0.0: if corrtype == 'masking': return masking_noise(data, sess, corrfrac) elif corrtype == 'salt_and_pepper': return salt_and_pepper_noise(data, corruption_ratio) else: return np.copy(data)
def corrupt_input(data, sess, corrtype, corrfrac)
Corrupt a fraction of data according to the chosen noise method. :return: corrupted data
2.73239
2.974014
0.918755
low = -const * np.sqrt(6.0 / (fan_in + fan_out)) high = const * np.sqrt(6.0 / (fan_in + fan_out)) return tf.random_uniform((fan_in, fan_out), minval=low, maxval=high)
def xavier_init(fan_in, fan_out, const=1)
Xavier initialization of network weights. https://stackoverflow.com/questions/33640581/how-to-do-xavier-initialization-on-tensorflow :param fan_in: fan in of the network (n_features) :param fan_out: fan out of the network (n_components) :param const: multiplicative constant
1.46939
1.901683
0.772679
data = np.array(data) for i in range(0, data.shape[0], batch_size): yield data[i:i + batch_size]
def gen_batches(data, batch_size)
Divide input data into batches. :param data: input data :param batch_size: size of each batch :return: data divided into batches
1.986278
2.671394
0.743536
nc = 1 + np.max(dataY) onehot = [np.zeros(nc, dtype=np.int8) for _ in dataY] for i, j in enumerate(dataY): onehot[i][j] = 1 return onehot
def to_one_hot(dataY)
Convert the vector of labels dataY into one-hot encoding. :param dataY: vector of labels :return: one-hot encoded labels
2.503179
2.924561
0.855916
if data.min() < 0 or data.max() > 1: data = normalize(data) out_data = data.copy() for i, sample in enumerate(out_data): for j, val in enumerate(sample): if np.random.random() <= val: out_data[i][j] = 1 else: out_data[i][j] = 0 return out_data
def conv2bin(data)
Convert a matrix of probabilities into binary values. If the matrix has values <= 0 or >= 1, the values are normalized to be in [0, 1]. :type data: numpy array :param data: input matrix :return: converted binary matrix
2.457293
2.473417
0.993481
out_data = data.copy() for i, sample in enumerate(out_data): out_data[i] /= sum(out_data[i]) return out_data
def normalize(data)
Normalize the data to be in the [0, 1] range. :param data: :return: normalized data
3.129776
3.873492
0.807999
data_noise = data.copy() rand = tf.random_uniform(data.shape) data_noise[sess.run(tf.nn.relu(tf.sign(v - rand))).astype(np.bool)] = 0 return data_noise
def masking_noise(data, sess, v)
Apply masking noise to data in X. In other words a fraction v of elements of X (chosen at random) is forced to zero. :param data: array_like, Input data :param sess: TensorFlow session :param v: fraction of elements to distort, float :return: transformed data
4.159185
4.582458
0.907632
X_noise = X.copy() n_features = X.shape[1] mn = X.min() mx = X.max() for i, sample in enumerate(X): mask = np.random.randint(0, n_features, v) for m in mask: if np.random.random() < 0.5: X_noise[i][m] = mn else: X_noise[i][m] = mx return X_noise
def salt_and_pepper_noise(X, v)
Apply salt and pepper noise to data in X. In other words a fraction v of elements of X (chosen at random) is set to its maximum or minimum value according to a fair coin flip. If minimum or maximum are not given, the min (max) value in X is taken. :param X: array_like, Input data :param v: int, fraction of elements to distort :return: transformed data
2.362056
2.578466
0.91607
layers = args_to_expand['layers'] try: items = args_to_expand.iteritems() except AttributeError: items = args_to_expand.items() for key, val in items: if isinstance(val, list) and len(val) != len(layers): args_to_expand[key] = [val[0] for _ in layers] return args_to_expand
def expand_args(**args_to_expand)
Expand the given lists into the length of the layers. This is used as a convenience so that the user does not need to specify the complete list of parameters for model initialization. IE the user can just specify one parameter and this function will expand it
2.630174
2.495777
1.05385
if flagtype == 'int': return [int(_) for _ in flagval.split(',') if _] elif flagtype == 'float': return [float(_) for _ in flagval.split(',') if _] elif flagtype == 'str': return [_ for _ in flagval.split(',') if _] else: raise Exception("incorrect type")
def flag_to_list(flagval, flagtype)
Convert a string of comma-separated tf flags to a list of values.
2.181426
2.015566
1.082289
if act_func == 'sigmoid': return tf.nn.sigmoid elif act_func == 'tanh': return tf.nn.tanh elif act_func == 'relu': return tf.nn.relu
def str2actfunc(act_func)
Convert activation function name to tf function.
1.749652
1.729551
1.011622
if seed >= 0: np.random.seed(seed) tf.set_random_seed(seed) return True else: return False
def random_seed_np_tf(seed)
Seed numpy and tensorflow random number generators. :param seed: seed parameter
2.154981
3.006446
0.716787
assert len(img) == width * height or len(img) == width * height * 3 if img_type == 'grey': misc.imsave(outfile, img.reshape(width, height)) elif img_type == 'color': misc.imsave(outfile, img.reshape(3, width, height))
def gen_image(img, width, height, outfile, img_type='grey')
Save an image with the given parameters.
2.44973
2.451854
0.999134
weights = np.load(weights_npy) perm = np.random.permutation(weights.shape[1])[:n_images] for p in perm: w = np.array([i[p] for i in weights]) image_path = outdir + 'w_{}.png'.format(p) gen_image(w, width, height, image_path, img_type)
def get_weights_as_images(weights_npy, width, height, outdir='img/', n_images=10, img_type='grey')
Create and save the weights of the hidden units as images. :param weights_npy: path to the weights .npy file :param width: width of the images :param height: height of the images :param outdir: output directory :param n_images: number of images to generate :param img_type: 'grey' or 'color' (RGB)
2.724723
3.003972
0.90704
shuff = list(zip(train_set, train_labels)) pbar = tqdm(range(self.num_epochs)) for i in pbar: np.random.shuffle(list(shuff)) batches = [_ for _ in utilities.gen_batches( shuff, self.batch_size)] for batch in batches: x_batch, y_batch = zip(*batch) self.tf_session.run( self.train_step, feed_dict={self.input_data: x_batch, self.input_labels: y_batch, self.keep_prob: self.dropout}) if validation_set is not None: feed = {self.input_data: validation_set, self.input_labels: validation_labels, self.keep_prob: 1} acc = tf_utils.run_summaries( self.tf_session, self.tf_merged_summaries, self.tf_summary_writer, i, feed, self.accuracy) pbar.set_description("Accuracy: %s" % (acc))
def _train_model(self, train_set, train_labels, validation_set, validation_labels)
Train the model. :param train_set: training set :param train_labels: training labels :param validation_set: validation set :param validation_labels: validation labels :return: self
2.385856
2.545369
0.937332
self._create_placeholders(n_features, n_classes) self._create_layers(n_classes) self.cost = self.loss.compile(self.mod_y, self.input_labels) self.train_step = self.trainer.compile(self.cost) self.accuracy = Evaluation.accuracy(self.mod_y, self.input_labels)
def build_model(self, n_features, n_classes)
Create the computational graph of the model. :param n_features: Number of features. :param n_classes: number of classes. :return: self
3.865248
4.414441
0.875592
return tf.nn.max_pool( x, ksize=[1, dim, dim, 1], strides=[1, dim, dim, 1], padding='SAME')
def max_pool(x, dim)
Max pooling operation.
2.377384
2.178937
1.091075
g = graph if graph is not None else self.tf_graph with g.as_default(): with tf.Session() as self.tf_session: self.tf_saver.restore(self.tf_session, self.model_path) feed = {self.input_data: data, self.keep_prob: 1} return self.reconstruction.eval(feed)
def reconstruct(self, data, graph=None)
Reconstruct data according to the model. Parameters ---------- data : array_like, shape (n_samples, n_features) Data to transform. graph : tf.Graph, optional (default = None) Tensorflow Graph object Returns ------- array_like, transformed data
2.783825
3.139928
0.886589
g = graph if graph is not None else self.tf_graph with g.as_default(): with tf.Session() as self.tf_session: self.tf_saver.restore(self.tf_session, self.model_path) feed = { self.input_data: data, self.input_labels: data_ref, self.keep_prob: 1 } return self.cost.eval(feed)
def score(self, data, data_ref, graph=None)
Compute the reconstruction loss over the test set. Parameters ---------- data : array_like Data to reconstruct. data_ref : array_like Reference data. Returns ------- float: Mean error.
2.67873
3.028924
0.884383
with tf.name_scope(name_scope): return self.opt_.minimize(cost)
def compile(self, cost, name_scope="train")
Compile the optimizer with the given training parameters. Parameters ---------- cost : Tensor A Tensor containing the value to minimize. name_scope : str , optional (default="train") Optional name scope for the optimizer graph ops.
4.542771
5.293953
0.858106
with tf.name_scope(self.name): if self.lfunc == 'cross_entropy': clip_inf = tf.clip_by_value(mod_y, 1e-10, float('inf')) clip_sup = tf.clip_by_value(1 - mod_y, 1e-10, float('inf')) cost = - tf.reduce_mean(tf.add( tf.multiply(ref_y, tf.log(clip_inf)), tf.multiply(tf.subtract(1.0, ref_y), tf.log(clip_sup)))) elif self.lfunc == 'softmax_cross_entropy': cost = tf.losses.softmax_cross_entropy(ref_y, mod_y) elif self.lfunc == 'mse': cost = tf.sqrt(tf.reduce_mean( tf.square(tf.subtract(ref_y, mod_y)))) else: cost = None if cost is not None: cost = cost + regterm if regterm is not None else cost tf.summary.scalar(self.lfunc, cost) else: cost = None return cost
def compile(self, mod_y, ref_y, regterm=None)
Compute the loss function tensor. Parameters ---------- mode_y : tf.Tensor model output tensor ref_y : tf.Tensor reference input tensor regterm : tf.Tensor, optional (default = None) Regularization term tensor Returns ------- Loss function tensor.
2.153043
2.11676
1.017141
self._create_placeholders(n_features, n_features) if encoding_w and encoding_b: self.encoding_w_ = encoding_w self.encoding_b_ = encoding_b else: self._create_variables(n_features) self._create_encoding_layers() self._create_decoding_layers() variables = [] variables.extend(self.encoding_w_) variables.extend(self.encoding_b_) regterm = Layers.regularization(variables, self.regtype, self.regcoef) self.cost = self.loss.compile( self.reconstruction, self.input_labels, regterm=regterm) self.train_step = self.trainer.compile(self.cost)
def build_model(self, n_features, encoding_w=None, encoding_b=None)
Create the computational graph for the reconstruction task. :param n_features: Number of features :param encoding_w: list of weights for the encoding layers. :param encoding_b: list of biases for the encoding layers. :return: self
3.239802
3.321724
0.975337
pbar = tqdm(range(self.num_epochs)) for i in pbar: self._run_train_step(train_set) if validation_set is not None: feed = self._create_feed_dict(validation_set) err = tf_utils.run_summaries( self.tf_session, self.tf_merged_summaries, self.tf_summary_writer, i, feed, self.cost) pbar.set_description("Reconstruction loss: %s" % (err))
def _train_model(self, train_set, train_ref=None, validation_set=None, Validation_ref=None)
Train the model. :param train_set: training set :param validation_set: validation set. optional, default None :return: self
3.689616
3.986075
0.925626
np.random.shuffle(train_set) batches = [_ for _ in utilities.gen_batches(train_set, self.batch_size)] updates = [self.w_upd8, self.bh_upd8, self.bv_upd8] for batch in batches: self.tf_session.run(updates, feed_dict=self._create_feed_dict(batch))
def _run_train_step(self, train_set)
Run a training step. A training step is made by randomly shuffling the training set, divide into batches and run the variable update nodes for each batch. :param train_set: training set :return: self
4.276536
4.349119
0.983311
return { self.input_data: data, self.hrand: np.random.rand(data.shape[0], self.num_hidden), self.vrand: np.random.rand(data.shape[0], data.shape[1]) }
def _create_feed_dict(self, data)
Create the dictionary of data to feed to tf session during training. :param data: training/validation set batch :return: dictionary(self.input_data: data, self.hrand: random_uniform, self.vrand: random_uniform)
3.384568
2.038436
1.660375
self._create_placeholders(n_features) self._create_variables(n_features) self.encode = self.sample_hidden_from_visible(self.input_data)[0] self.reconstruction = self.sample_visible_from_hidden( self.encode, n_features) hprob0, hstate0, vprob, hprob1, hstate1 = self.gibbs_sampling_step( self.input_data, n_features) positive = self.compute_positive_association(self.input_data, hprob0, hstate0) nn_input = vprob for step in range(self.gibbs_sampling_steps - 1): hprob, hstate, vprob, hprob1, hstate1 = self.gibbs_sampling_step( nn_input, n_features) nn_input = vprob negative = tf.matmul(tf.transpose(vprob), hprob1) self.w_upd8 = self.W.assign_add( self.learning_rate * (positive - negative) / self.batch_size) self.bh_upd8 = self.bh_.assign_add(tf.multiply(self.learning_rate, tf.reduce_mean( tf.subtract(hprob0, hprob1), 0))) self.bv_upd8 = self.bv_.assign_add(tf.multiply(self.learning_rate, tf.reduce_mean( tf.subtract(self.input_data, vprob), 0))) variables = [self.W, self.bh_, self.bv_] regterm = Layers.regularization(variables, self.regtype, self.regcoef) self.cost = self.loss.compile(vprob, self.input_data, regterm=regterm)
def build_model(self, n_features, regtype='none')
Build the Restricted Boltzmann Machine model in TensorFlow. :param n_features: number of features :param regtype: regularization type :return: self
3.176386
3.133616
1.013649
self.input_data = tf.placeholder(tf.float32, [None, n_features], name='x-input') self.hrand = tf.placeholder(tf.float32, [None, self.num_hidden], name='hrand') self.vrand = tf.placeholder(tf.float32, [None, n_features], name='vrand') # not used in this model, created just to comply with # unsupervised_model.py self.input_labels = tf.placeholder(tf.float32) self.keep_prob = tf.placeholder(tf.float32, name='keep-probs')
def _create_placeholders(self, n_features)
Create the TensorFlow placeholders for the model. :param n_features: number of features :return: self
3.008217
3.122218
0.963487
w_name = 'weights' self.W = tf.Variable(tf.truncated_normal( shape=[n_features, self.num_hidden], stddev=0.1), name=w_name) tf.summary.histogram(w_name, self.W) bh_name = 'hidden-bias' self.bh_ = tf.Variable(tf.constant(0.1, shape=[self.num_hidden]), name=bh_name) tf.summary.histogram(bh_name, self.bh_) bv_name = 'visible-bias' self.bv_ = tf.Variable(tf.constant(0.1, shape=[n_features]), name=bv_name) tf.summary.histogram(bv_name, self.bv_)
def _create_variables(self, n_features)
Create the TensorFlow variables for the model. :param n_features: number of features :return: self
1.721527
1.796678
0.958172
hprobs, hstates = self.sample_hidden_from_visible(visible) vprobs = self.sample_visible_from_hidden(hprobs, n_features) hprobs1, hstates1 = self.sample_hidden_from_visible(vprobs) return hprobs, hstates, vprobs, hprobs1, hstates1
def gibbs_sampling_step(self, visible, n_features)
Perform one step of gibbs sampling. :param visible: activations of the visible units :param n_features: number of features :return: tuple(hidden probs, hidden states, visible probs, new hidden probs, new hidden states)
2.578766
2.304712
1.11891
hprobs = tf.nn.sigmoid(tf.add(tf.matmul(visible, self.W), self.bh_)) hstates = utilities.sample_prob(hprobs, self.hrand) return hprobs, hstates
def sample_hidden_from_visible(self, visible)
Sample the hidden units from the visible units. This is the Positive phase of the Contrastive Divergence algorithm. :param visible: activations of the visible units :return: tuple(hidden probabilities, hidden binary states)
7.03786
6.000099
1.172957
visible_activation = tf.add( tf.matmul(hidden, tf.transpose(self.W)), self.bv_ ) if self.visible_unit_type == 'bin': vprobs = tf.nn.sigmoid(visible_activation) elif self.visible_unit_type == 'gauss': vprobs = tf.truncated_normal( (1, n_features), mean=visible_activation, stddev=self.stddev) else: vprobs = None return vprobs
def sample_visible_from_hidden(self, hidden, n_features)
Sample the visible units from the hidden units. This is the Negative phase of the Contrastive Divergence algorithm. :param hidden: activations of the hidden units :param n_features: number of features :return: visible probabilities
3.437349
3.535637
0.972201
if self.visible_unit_type == 'bin': positive = tf.matmul(tf.transpose(visible), hidden_states) elif self.visible_unit_type == 'gauss': positive = tf.matmul(tf.transpose(visible), hidden_probs) else: positive = None return positive
def compute_positive_association(self, visible, hidden_probs, hidden_states)
Compute positive associations between visible and hidden units. :param visible: visible units :param hidden_probs: hidden units probabilities :param hidden_states: hidden units states :return: positive association = dot(visible.T, hidden)
3.230227
2.990796
1.080056
n_features, self.num_hidden = shape[0], shape[1] self.gibbs_sampling_steps = gibbs_sampling_steps self.build_model(n_features) init_op = tf.global_variables_initializer() self.tf_saver = tf.train.Saver() with tf.Session() as self.tf_session: self.tf_session.run(init_op) self.tf_saver.restore(self.tf_session, model_path)
def load_model(self, shape, gibbs_sampling_steps, model_path)
Load a trained model from disk. The shape of the model (num_visible, num_hidden) and the number of gibbs sampling steps must be known in order to restore the model. :param shape: tuple(num_visible, num_hidden) :param gibbs_sampling_steps: :param model_path: :return: self
2.206481
2.303788
0.957762
g = graph if graph is not None else self.tf_graph with g.as_default(): with tf.Session() as self.tf_session: self.tf_saver.restore(self.tf_session, self.model_path) return { 'W': self.W.eval(), 'bh_': self.bh_.eval(), 'bv_': self.bv_.eval() }
def get_parameters(self, graph=None)
Return the model parameters in the form of numpy arrays. :param graph: tf graph object :return: model parameters
2.76438
2.739837
1.008958
best_action = None best_match_count = -1 min_cost = min(ic, dc, sc) if min_cost == sc and cost == 0: best_action = EQUAL best_match_count = sm elif min_cost == sc and cost == 1: best_action = REPLACE best_match_count = sm elif min_cost == ic and im > best_match_count: best_action = INSERT best_match_count = im elif min_cost == dc and dm > best_match_count: best_action = DELETE best_match_count = dm return best_action
def lowest_cost_action(ic, dc, sc, im, dm, sm, cost)
Given the following values, choose the action (insertion, deletion, or substitution), that results in the lowest cost (ties are broken using the 'match' score). This is used within the dynamic programming algorithm. * ic - insertion cost * dc - deletion cost * sc - substitution cost * im - insertion match (score) * dm - deletion match (score) * sm - substitution match (score)
2.236201
2.267691
0.986113
# pylint: disable=unused-argument best_action = None lowest_cost = float("inf") max_match = max(im, dm, sm) if max_match == sm and cost == 0: best_action = EQUAL lowest_cost = sm elif max_match == sm and cost == 1: best_action = REPLACE lowest_cost = sm elif max_match == im and ic < lowest_cost: best_action = INSERT lowest_cost = ic elif max_match == dm and dc < lowest_cost: best_action = DELETE lowest_cost = dc return best_action
def highest_match_action(ic, dc, sc, im, dm, sm, cost)
Given the following values, choose the action (insertion, deletion, or substitution), that results in the highest match score (ties are broken using the distance values). This is used within the dynamic programming algorithm. * ic - insertion cost * dc - deletion cost * sc - substitution cost * im - insertion match (score) * dm - deletion match (score) * sm - substitution match (score)
2.481655
2.440671
1.016792
m = len(seq1) n = len(seq2) # Special, easy cases: if seq1 == seq2: return 0, n if m == 0: return n, 0 if n == 0: return m, 0 v0 = [0] * (n + 1) # The two 'error' columns v1 = [0] * (n + 1) m0 = [0] * (n + 1) # The two 'match' columns m1 = [0] * (n + 1) for i in range(1, n + 1): v0[i] = i for i in range(1, m + 1): v1[0] = i for j in range(1, n + 1): cost = 0 if test(seq1[i - 1], seq2[j - 1]) else 1 # The costs ins_cost = v1[j - 1] + 1 del_cost = v0[j] + 1 sub_cost = v0[j - 1] + cost # Match counts ins_match = m1[j - 1] del_match = m0[j] sub_match = m0[j - 1] + int(not cost) action = action_function(ins_cost, del_cost, sub_cost, ins_match, del_match, sub_match, cost) if action in [EQUAL, REPLACE]: v1[j] = sub_cost m1[j] = sub_match elif action == INSERT: v1[j] = ins_cost m1[j] = ins_match elif action == DELETE: v1[j] = del_cost m1[j] = del_match else: raise Exception('Invalid dynamic programming option returned!') # Copy the columns over for i in range(0, n + 1): v0[i] = v1[i] m0[i] = m1[i] return v1[n], m1[n]
def edit_distance(seq1, seq2, action_function=lowest_cost_action, test=operator.eq)
Computes the edit distance between the two given sequences. This uses the relatively fast method that only constructs two columns of the 2d array for edits. This function actually uses four columns because we track the number of matches too.
2.014706
1.948981
1.033723
matches = 0 # Create a 2d distance array m = len(seq1) n = len(seq2) # distances array: d = [[0 for x in range(n + 1)] for y in range(m + 1)] # backpointer array: bp = [[None for x in range(n + 1)] for y in range(m + 1)] # matches array: matches = [[0 for x in range(n + 1)] for y in range(m + 1)] # source prefixes can be transformed into empty string by # dropping all characters for i in range(1, m + 1): d[i][0] = i bp[i][0] = [DELETE, i - 1, i, 0, 0] # target prefixes can be reached from empty source prefix by inserting # every characters for j in range(1, n + 1): d[0][j] = j bp[0][j] = [INSERT, 0, 0, j - 1, j] # compute the edit distance... for i in range(1, m + 1): for j in range(1, n + 1): cost = 0 if test(seq1[i - 1], seq2[j - 1]) else 1 # The costs of each action... ins_cost = d[i][j - 1] + 1 # insertion del_cost = d[i - 1][j] + 1 # deletion sub_cost = d[i - 1][j - 1] + cost # substitution/match # The match scores of each action ins_match = matches[i][j - 1] del_match = matches[i - 1][j] sub_match = matches[i - 1][j - 1] + int(not cost) action = action_function(ins_cost, del_cost, sub_cost, ins_match, del_match, sub_match, cost) if action == EQUAL: d[i][j] = sub_cost matches[i][j] = sub_match bp[i][j] = [EQUAL, i - 1, i, j - 1, j] elif action == REPLACE: d[i][j] = sub_cost matches[i][j] = sub_match bp[i][j] = [REPLACE, i - 1, i, j - 1, j] elif action == INSERT: d[i][j] = ins_cost matches[i][j] = ins_match bp[i][j] = [INSERT, i - 1, i - 1, j - 1, j] elif action == DELETE: d[i][j] = del_cost matches[i][j] = del_match bp[i][j] = [DELETE, i - 1, i, j - 1, j - 1] else: raise Exception('Invalid dynamic programming action returned!') opcodes = get_opcodes_from_bp_table(bp) return d[m][n], matches[m][n], opcodes
def edit_distance_backpointer(seq1, seq2, action_function=lowest_cost_action, test=operator.eq)
Similar to :py:func:`~edit_distance.edit_distance` except that this function keeps backpointers during the search. This allows us to return the opcodes (i.e. the specific edits that were used to change from one string to another). This function contructs the full 2d array (actually it contructs three of them: one for distances, one for matches, and one for backpointers).
1.890734
1.869245
1.011496
x = len(bp) - 1 y = len(bp[0]) - 1 opcodes = [] while x != 0 or y != 0: this_bp = bp[x][y] opcodes.append(this_bp) if this_bp[0] == EQUAL or this_bp[0] == REPLACE: x = x - 1 y = y - 1 elif this_bp[0] == INSERT: y = y - 1 elif this_bp[0] == DELETE: x = x - 1 opcodes.reverse() return opcodes
def get_opcodes_from_bp_table(bp)
Given a 2d list structure, collect the opcodes from the best path.
2.007486
1.78229
1.126352
if len(sys.argv) != 3: print('Usage: {} <file1> <file2>'.format(sys.argv[0])) exit(-1) file1 = sys.argv[1] file2 = sys.argv[2] with open(file1) as f1, open(file2) as f2: for line1, line2 in zip(f1, f2): print("Line 1: {}".format(line1.strip())) print("Line 2: {}".format(line2.strip())) dist, _, _ = edit_distance_backpointer(line1.split(), line2.split()) print('Distance: {}'.format(dist)) print('=' * 80)
def main()
Read two files line-by-line and print edit distances between each pair of lines. Will terminate at the end of the shorter of the two files.
2.012406
1.804086
1.115471
self.set_seq1(a) self.set_seq2(b) self._reset_object()
def set_seqs(self, a, b)
Specify two alternative sequences -- reset any cached values.
5.1481
4.366308
1.179051
opcodes = self.get_opcodes() match_opcodes = filter(lambda x: x[0] == EQUAL, opcodes) return map(lambda opcode: [opcode[1], opcode[3], opcode[2] - opcode[1]], match_opcodes)
def get_matching_blocks(self)
Similar to :py:meth:`get_opcodes`, but returns only the opcodes that are equal and returns them in a somewhat different format (i.e. ``(i, j, n)`` ).
4.535108
3.643013
1.244878
if not self.opcodes: d, m, opcodes = edit_distance_backpointer(self.seq1, self.seq2, action_function=self.action_function, test=self.test) if self.dist: assert d == self.dist if self._matches: assert m == self._matches self.dist = d self._matches = m self.opcodes = opcodes return self.opcodes
def get_opcodes(self)
Returns a list of opcodes. Opcodes are the same as defined by :py:mod:`difflib`.
4.442063
4.240044
1.047646
return 2.0 * self.matches() / (len(self.seq1) + len(self.seq2))
def ratio(self)
Ratio of matches to the average sequence length.
6.597006
3.356857
1.965233
d, m = edit_distance(self.seq1, self.seq2, action_function=self.action_function, test=self.test) if self.dist: assert d == self.dist if self._matches: assert m == self._matches self.dist = d self._matches = m
def _compute_distance_fast(self)
Calls edit_distance, and asserts that if we already have values for matches and distance, that they match.
4.716973
3.329834
1.416579
result = (response.result() for response in reqs) ret = [r.json() if r.status_code == 200 else None for r in result] return ret
def async_request(self, reqs:list)->list
异步并发请求 :param reqs: 请求列表 :return:
4.804553
4.546259
1.056815
params = {'symbol': symbol, 'period': period, 'size': size} url = u.MARKET_URL + '/market/history/kline' return http_get_request(url, params, _async=_async)
def get_kline(self, symbol, period, size=150, _async=False)
获取KLine :param symbol :param period: 可选值:{1min, 5min, 15min, 30min, 60min, 1day, 1mon, 1week, 1year } :param size: 可选值: [1,2000] :return:
3.575552
4.370774
0.818059
params = {'symbol': symbol, 'type': _type} url = u.MARKET_URL + '/market/depth' return http_get_request(url, params, _async=_async)
def get_last_depth(self, symbol, _type, _async=False)
获取marketdepth :param symbol :param type: 可选值:{ percent10, step0, step1, step2, step3, step4, step5 } :return:
4.957492
5.31062
0.933505
params = {'symbol': symbol} url = u.MARKET_URL + '/market/trade' return http_get_request(url, params, _async=_async)
def get_last_ticker(self, symbol, _async=False)
获取tradedetail :param symbol :return:
6.674034
7.378162
0.904566
params = {'symbol': symbol, 'size': size} url = u.MARKET_URL + '/market/history/trade' return http_get_request(url, params, _async=_async)
def get_ticker(self, symbol, size=1, _async=False)
获取历史ticker :param symbol: :param size: 可选[1,2000] :return:
4.528526
5.375974
0.842364
params = {} url = u.MARKET_URL + '/market/tickers' return http_get_request(url, params, _async=_async)
def get_all_last_24h_kline(self, _async=False)
获取所有24小时的概况 :param _async: :return:
6.982159
7.557101
0.92392
assert site in ['Pro', 'HADAX'] params = {} path = f'/v1{"/" if site == "Pro" else "/hadax/"}common/symbols' return api_key_get(params, path, _async=_async)
def get_symbols(self, site='Pro', _async=False)
获取 支持的交易对 :param site: :return:
8.538712
7.181347
1.189013
assert site in ['Pro', 'HADAX'] params = {} path = f'/v1{"/" if site == "Pro" else "/hadax/"}common/currencys' return api_key_get(params, path, _async=_async)
def get_currencys(self, site='Pro', _async=False)
获取所有币种 :param site: :return:
7.041618
6.652194
1.058541
path = '/v1/account/accounts' params = {} return api_key_get(params, path, _async=_async)
def get_accounts(self, _async=False)
:return:
7.150921
6.024688
1.186936
acc_id = self.acc_id if acc_id is None else acc_id assert site in ['Pro', 'HADAX'] path = f'/v1{"/" if site == "Pro" else "/hadax/"}account/accounts/{acc_id}/balance' # params = {'account-id': self.acct_id} params = {} return api_key_get(params, path, _async=_async)
def get_balance(self, acc_id=None, site='Pro', _async=False)
获取当前账户资产 :return:
4.554513
4.52967
1.005484