markdown
stringlengths
0
37k
code
stringlengths
1
33.3k
path
stringlengths
8
215
repo_name
stringlengths
6
77
license
stringclasses
15 values
Let's also plot the cost function and the gradients.
# Plot learning curve (with costs) costs = np.squeeze(d['costs']) plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations (per hundreds)') plt.title("Learning rate =" + str(d["learning_rate"])) plt.show()
course-deeplearning.ai/course1-nn-and-deeplearning/Logistic+Regression+with+a+Neural+Network+mindset+v3.ipynb
liufuyang/deep_learning_tutorial
mit
Interpretation: You can see the cost decreasing. It shows that the parameters are being learned. However, you see that you could train the model even more on the training set. Try to increase the number of iterations in the cell above and rerun the cells. You might see that the training set accuracy goes up, but the test set accuracy goes down. This is called overfitting. 6 - Further analysis (optional/ungraded exercise) Congratulations on building your first image classification model. Let's analyze it further, and examine possible choices for the learning rate $\alpha$. Choice of learning rate Reminder: In order for Gradient Descent to work you must choose the learning rate wisely. The learning rate $\alpha$ determines how rapidly we update the parameters. If the learning rate is too large we may "overshoot" the optimal value. Similarly, if it is too small we will need too many iterations to converge to the best values. That's why it is crucial to use a well-tuned learning rate. Let's compare the learning curve of our model with several choices of learning rates. Run the cell below. This should take about 1 minute. Feel free also to try different values than the three we have initialized the learning_rates variable to contain, and see what happens.
learning_rates = [0.01, 0.001, 0.0001] models = {} for i in learning_rates: print ("learning rate is: " + str(i)) models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 1500, learning_rate = i, print_cost = False) print ('\n' + "-------------------------------------------------------" + '\n') for i in learning_rates: plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learning_rate"])) plt.ylabel('cost') plt.xlabel('iterations') legend = plt.legend(loc='upper center', shadow=True) frame = legend.get_frame() frame.set_facecolor('0.90') plt.show()
course-deeplearning.ai/course1-nn-and-deeplearning/Logistic+Regression+with+a+Neural+Network+mindset+v3.ipynb
liufuyang/deep_learning_tutorial
mit
Interpretation: - Different learning rates give different costs and thus different predictions results. - If the learning rate is too large (0.01), the cost may oscillate up and down. It may even diverge (though in this example, using 0.01 still eventually ends up at a good value for the cost). - A lower cost doesn't mean a better model. You have to check if there is possibly overfitting. It happens when the training accuracy is a lot higher than the test accuracy. - In deep learning, we usually recommend that you: - Choose the learning rate that better minimizes the cost function. - If your model overfits, use other techniques to reduce overfitting. (We'll talk about this in later videos.) 7 - Test with your own image (optional/ungraded exercise) Congratulations on finishing this assignment. You can use your own image and see the output of your model. To do that: 1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub. 2. Add your image to this Jupyter Notebook's directory, in the "images" folder 3. Change your image's name in the following code 4. Run the code and check if the algorithm is right (1 = cat, 0 = non-cat)!
## START CODE HERE ## (PUT YOUR IMAGE NAME) my_image = "my_image.jpg" # change this to the name of your image file ## END CODE HERE ## # We preprocess the image to fit your algorithm. fname = "images/" + my_image image = np.array(ndimage.imread(fname, flatten=False)) my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T my_predicted_image = predict(d["w"], d["b"], my_image) plt.imshow(image) print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") + "\" picture.")
course-deeplearning.ai/course1-nn-and-deeplearning/Logistic+Regression+with+a+Neural+Network+mindset+v3.ipynb
liufuyang/deep_learning_tutorial
mit
Explore the Data Play around with view_sentence_range to view different parts of the data.
view_sentence_range = (0, 10) """ DON'T MODIFY ANYTHING IN THIS CELL """ import numpy as np print('Dataset Stats') print('Roughly the number of unique words: {}'.format(len({word: None for word in source_text.split()}))) sentences = source_text.split('\n') word_counts = [len(sentence.split()) for sentence in sentences] print('Number of sentences: {}'.format(len(sentences))) print('Average number of words in a sentence: {}'.format(np.average(word_counts))) print() print('English sentences {} to {}:'.format(*view_sentence_range)) print('\n'.join(source_text.split('\n')[view_sentence_range[0]:view_sentence_range[1]])) print() print('French sentences {} to {}:'.format(*view_sentence_range)) print('\n'.join(target_text.split('\n')[view_sentence_range[0]:view_sentence_range[1]]))
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Implement Preprocessing Function Text to Word Ids As you did with other RNNs, you must turn the text into a number so the computer can understand it. In the function text_to_ids(), you'll turn source_text and target_text from words to ids. However, you need to add the <EOS> word id at the end of each sentence from target_text. This will help the neural network predict when the sentence should end. You can get the <EOS> word id by doing: python target_vocab_to_int['<EOS>'] You can get other word ids using source_vocab_to_int and target_vocab_to_int.
def text_to_ids(source_text, target_text, source_vocab_to_int, target_vocab_to_int): """ Convert source and target text to proper word ids :param source_text: String that contains all the source text. :param target_text: String that contains all the target text. :param source_vocab_to_int: Dictionary to go from the source words to an id :param target_vocab_to_int: Dictionary to go from the target words to an id :return: A tuple of lists (source_id_text, target_id_text) """ source_id_text = [[source_vocab_to_int[y] for y in x] for x in [sentence.split() for sentence in source_text.split('\n')]] target_id_text = [[target_vocab_to_int[y] for y in x] for x in [sentence.split() for sentence in target_text.split('\n')]] for l in target_id_text: l.append(target_vocab_to_int['<EOS>']) return source_id_text, target_id_text """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_text_to_ids(text_to_ids)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Preprocess all the data and save it Running the code cell below will preprocess all the data and save it to file.
""" DON'T MODIFY ANYTHING IN THIS CELL """ helper.preprocess_and_save_data(source_path, target_path, text_to_ids)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Check Point This is your first checkpoint. If you ever decide to come back to this notebook or have to restart the notebook, you can start from here. The preprocessed data has been saved to disk.
""" DON'T MODIFY ANYTHING IN THIS CELL """ import numpy as np import helper (source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess()
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Check the Version of TensorFlow and Access to GPU This will check to make sure you have the correct version of TensorFlow and access to a GPU
""" DON'T MODIFY ANYTHING IN THIS CELL """ from distutils.version import LooseVersion import warnings import tensorflow as tf # Check TensorFlow Version assert LooseVersion(tf.__version__) in [LooseVersion('1.0.0'), LooseVersion('1.0.1')], 'This project requires TensorFlow version 1.0 You are using {}'.format(tf.__version__) print('TensorFlow Version: {}'.format(tf.__version__)) # Check for a GPU if not tf.test.gpu_device_name(): warnings.warn('No GPU found. Please use a GPU to train your neural network.') else: print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Build the Neural Network You'll build the components necessary to build a Sequence-to-Sequence model by implementing the following functions below: - model_inputs - process_decoding_input - encoding_layer - decoding_layer_train - decoding_layer_infer - decoding_layer - seq2seq_model Input Implement the model_inputs() function to create TF Placeholders for the Neural Network. It should create the following placeholders: Input text placeholder named "input" using the TF Placeholder name parameter with rank 2. Targets placeholder with rank 2. Learning rate placeholder with rank 0. Keep probability placeholder named "keep_prob" using the TF Placeholder name parameter with rank 0. Return the placeholders in the following the tuple (Input, Targets, Learing Rate, Keep Probability)
def model_inputs(): """ Create TF Placeholders for input, targets, and learning rate. :return: Tuple (input, targets, learning rate, keep probability) """ inputs = tf.placeholder(tf.int32, [None, None], name='input') targets = tf.placeholder(tf.int32, [None, None], name='targets') learning_rate = tf.placeholder(tf.float32, name='learning_rate') keep_prob = tf.placeholder(tf.float32, name='keep_prob') return inputs, targets, learning_rate, keep_prob """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_model_inputs(model_inputs)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Process Decoding Input Implement process_decoding_input using TensorFlow to remove the last word id from each batch in target_data and concat the GO ID to the begining of each batch.
def process_decoding_input(target_data, target_vocab_to_int, batch_size): """ Preprocess target data for dencoding :param target_data: Target Placehoder :param target_vocab_to_int: Dictionary to go from the target words to an id :param batch_size: Batch Size :return: Preprocessed target data """ ending = tf.strided_slice(target_data, [0, 0], [batch_size, -1], [1, 1]) decoding_input = tf.concat([tf.fill([batch_size, 1], target_vocab_to_int['<GO>']), ending], 1) return decoding_input """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_process_decoding_input(process_decoding_input)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Encoding Implement encoding_layer() to create a Encoder RNN layer using tf.nn.dynamic_rnn().
def encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob): """ Create encoding layer :param rnn_inputs: Inputs for the RNN :param rnn_size: RNN Size :param num_layers: Number of layers :param keep_prob: Dropout keep probability :return: RNN state """ enc_cell = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.BasicLSTMCell(rnn_size)] * num_layers) enc_cell = tf.contrib.rnn.DropoutWrapper(enc_cell, output_keep_prob=keep_prob) _, enc_state = tf.nn.dynamic_rnn(enc_cell, rnn_inputs, dtype=tf.float32) return enc_state """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_encoding_layer(encoding_layer)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Decoding - Training Create training logits using tf.contrib.seq2seq.simple_decoder_fn_train() and tf.contrib.seq2seq.dynamic_rnn_decoder(). Apply the output_fn to the tf.contrib.seq2seq.dynamic_rnn_decoder() outputs.
def decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope, output_fn, keep_prob): """ Create a decoding layer for training :param encoder_state: Encoder State :param dec_cell: Decoder RNN Cell :param dec_embed_input: Decoder embedded input :param sequence_length: Sequence Length :param decoding_scope: TenorFlow Variable Scope for decoding :param output_fn: Function to apply the output layer :param keep_prob: Dropout keep probability :return: Train Logits """ decoder = tf.contrib.seq2seq.simple_decoder_fn_train(encoder_state) prediction, _, _ = tf.contrib.seq2seq.dynamic_rnn_decoder(dec_cell, decoder, dec_embed_input, sequence_length, scope=decoding_scope) logits = output_fn(prediction) return logits """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_decoding_layer_train(decoding_layer_train)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Decoding - Inference Create inference logits using tf.contrib.seq2seq.simple_decoder_fn_inference() and tf.contrib.seq2seq.dynamic_rnn_decoder().
def decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id, end_of_sequence_id, maximum_length, vocab_size, decoding_scope, output_fn, keep_prob): """ Create a decoding layer for inference :param encoder_state: Encoder state :param dec_cell: Decoder RNN Cell :param dec_embeddings: Decoder embeddings :param start_of_sequence_id: GO ID :param end_of_sequence_id: EOS Id :param maximum_length: The maximum allowed time steps to decode :param vocab_size: Size of vocabulary :param decoding_scope: TensorFlow Variable Scope for decoding :param output_fn: Function to apply the output layer :param keep_prob: Dropout keep probability :return: Inference Logits """ decoder = tf.contrib.seq2seq.simple_decoder_fn_inference(output_fn, encoder_state, dec_embeddings, start_of_sequence_id, end_of_sequence_id, maximum_length, vocab_size) logits, _, _ = tf.contrib.seq2seq.dynamic_rnn_decoder(dec_cell, decoder, scope=decoding_scope) return logits """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_decoding_layer_infer(decoding_layer_infer)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Build the Decoding Layer Implement decoding_layer() to create a Decoder RNN layer. Create RNN cell for decoding using rnn_size and num_layers. Create the output fuction using lambda to transform it's input, logits, to class logits. Use the your decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope, output_fn, keep_prob) function to get the training logits. Use your decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id, end_of_sequence_id, maximum_length, vocab_size, decoding_scope, output_fn, keep_prob) function to get the inference logits. Note: You'll need to use tf.variable_scope to share variables between training and inference.
def decoding_layer(dec_embed_input, dec_embeddings, encoder_state, vocab_size, sequence_length, rnn_size, num_layers, target_vocab_to_int, keep_prob): """ Create decoding layer :param dec_embed_input: Decoder embedded input :param dec_embeddings: Decoder embeddings :param encoder_state: The encoded state :param vocab_size: Size of vocabulary :param sequence_length: Sequence Length :param rnn_size: RNN Size :param num_layers: Number of layers :param target_vocab_to_int: Dictionary to go from the target words to an id :param keep_prob: Dropout keep probability :return: Tuple of (Training Logits, Inference Logits) """ with tf.variable_scope("decoding") as decoding_scope: dec_cell = tf.contrib.rnn.BasicLSTMCell(rnn_size) dec_cell = tf.contrib.rnn.DropoutWrapper(dec_cell, output_keep_prob=keep_prob) dec_cell = tf.contrib.rnn.MultiRNNCell([dec_cell] * num_layers) _, dec_state = tf.nn.dynamic_rnn(dec_cell, dec_embed_input, dtype=tf.float32) output_fn = lambda x: tf.contrib.layers.fully_connected(x, vocab_size, None, scope=decoding_scope) t_logits = decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope, output_fn, keep_prob) with tf.variable_scope("decoding", reuse=True) as decoding_scope: i_logits = decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, target_vocab_to_int['<GO>'], target_vocab_to_int['<EOS>'], sequence_length, vocab_size, decoding_scope, output_fn, keep_prob) return t_logits, i_logits """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_decoding_layer(decoding_layer)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Build the Neural Network Apply the functions you implemented above to: Apply embedding to the input data for the encoder. Encode the input using your encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob). Process target data using your process_decoding_input(target_data, target_vocab_to_int, batch_size) function. Apply embedding to the target data for the decoder. Decode the encoded input using your decoding_layer(dec_embed_input, dec_embeddings, encoder_state, vocab_size, sequence_length, rnn_size, num_layers, target_vocab_to_int, keep_prob).
def seq2seq_model(input_data, target_data, keep_prob, batch_size, sequence_length, source_vocab_size, target_vocab_size, enc_embedding_size, dec_embedding_size, rnn_size, num_layers, target_vocab_to_int): """ Build the Sequence-to-Sequence part of the neural network :param input_data: Input placeholder :param target_data: Target placeholder :param keep_prob: Dropout keep probability placeholder :param batch_size: Batch Size :param sequence_length: Sequence Length :param source_vocab_size: Source vocabulary size :param target_vocab_size: Target vocabulary size :param enc_embedding_size: Decoder embedding size :param dec_embedding_size: Encoder embedding size :param rnn_size: RNN Size :param num_layers: Number of layers :param target_vocab_to_int: Dictionary to go from the target words to an id :return: Tuple of (Training Logits, Inference Logits) """ rnn_inputs = tf.contrib.layers.embed_sequence(input_data, vocab_size=source_vocab_size, embed_dim=enc_embedding_size) encoder_state = encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob) dec_input = process_decoding_input(target_data, target_vocab_to_int, batch_size) dec_embeddings = tf.Variable(tf.random_uniform([target_vocab_size, dec_embedding_size])) dec_embed_input = tf.nn.embedding_lookup(dec_embeddings, dec_input) t_logits, i_logits = decoding_layer(dec_embed_input, dec_embeddings, encoder_state, target_vocab_size, sequence_length, rnn_size, num_layers, target_vocab_to_int, keep_prob) return t_logits, i_logits """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_seq2seq_model(seq2seq_model)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Neural Network Training Hyperparameters Tune the following parameters: Set epochs to the number of epochs. Set batch_size to the batch size. Set rnn_size to the size of the RNNs. Set num_layers to the number of layers. Set encoding_embedding_size to the size of the embedding for the encoder. Set decoding_embedding_size to the size of the embedding for the decoder. Set learning_rate to the learning rate. Set keep_probability to the Dropout keep probability
# Number of Epochs epochs = 4 # Batch Size batch_size = 128 # RNN Size rnn_size = 384 # Number of Layers num_layers = 2 # Embedding Size encoding_embedding_size = 128 decoding_embedding_size = 128 # Learning Rate learning_rate = 0.001 # Dropout Keep Probability keep_probability = 0.6
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Build the Graph Build the graph using the neural network you implemented.
""" DON'T MODIFY ANYTHING IN THIS CELL """ save_path = 'checkpoints/dev' (source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess() max_source_sentence_length = max([len(sentence) for sentence in source_int_text]) train_graph = tf.Graph() with train_graph.as_default(): input_data, targets, lr, keep_prob = model_inputs() sequence_length = tf.placeholder_with_default(max_source_sentence_length, None, name='sequence_length') input_shape = tf.shape(input_data) train_logits, inference_logits = seq2seq_model( tf.reverse(input_data, [-1]), targets, keep_prob, batch_size, sequence_length, len(source_vocab_to_int), len(target_vocab_to_int), encoding_embedding_size, decoding_embedding_size, rnn_size, num_layers, target_vocab_to_int) tf.identity(inference_logits, 'logits') with tf.name_scope("optimization"): # Loss function cost = tf.contrib.seq2seq.sequence_loss( train_logits, targets, tf.ones([input_shape[0], sequence_length])) # Optimizer optimizer = tf.train.AdamOptimizer(lr) # Gradient Clipping gradients = optimizer.compute_gradients(cost) capped_gradients = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gradients if grad is not None] train_op = optimizer.apply_gradients(capped_gradients)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Train Train the neural network on the preprocessed data. If you have a hard time getting a good loss, check the forms to see if anyone is having the same problem.
""" DON'T MODIFY ANYTHING IN THIS CELL """ import time def get_accuracy(target, logits): """ Calculate accuracy """ max_seq = max(target.shape[1], logits.shape[1]) if max_seq - target.shape[1]: target = np.pad( target, [(0,0),(0,max_seq - target.shape[1])], 'constant') if max_seq - logits.shape[1]: logits = np.pad( logits, [(0,0),(0,max_seq - logits.shape[1]), (0,0)], 'constant') return np.mean(np.equal(target, np.argmax(logits, 2))) train_source = source_int_text[batch_size:] train_target = target_int_text[batch_size:] valid_source = helper.pad_sentence_batch(source_int_text[:batch_size]) valid_target = helper.pad_sentence_batch(target_int_text[:batch_size]) with tf.Session(graph=train_graph) as sess: sess.run(tf.global_variables_initializer()) for epoch_i in range(epochs): for batch_i, (source_batch, target_batch) in enumerate( helper.batch_data(train_source, train_target, batch_size)): start_time = time.time() _, loss = sess.run( [train_op, cost], {input_data: source_batch, targets: target_batch, lr: learning_rate, sequence_length: target_batch.shape[1], keep_prob: keep_probability}) batch_train_logits = sess.run( inference_logits, {input_data: source_batch, keep_prob: 1.0}) batch_valid_logits = sess.run( inference_logits, {input_data: valid_source, keep_prob: 1.0}) train_acc = get_accuracy(target_batch, batch_train_logits) valid_acc = get_accuracy(np.array(valid_target), batch_valid_logits) end_time = time.time() print('Epoch {:>3} Batch {:>4}/{} - Train Accuracy: {:>6.3f}, Validation Accuracy: {:>6.3f}, Loss: {:>6.3f}' .format(epoch_i, batch_i, len(source_int_text) // batch_size, train_acc, valid_acc, loss)) # Save Model saver = tf.train.Saver() saver.save(sess, save_path) print('Model Trained and Saved')
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Save Parameters Save the batch_size and save_path parameters for inference.
""" DON'T MODIFY ANYTHING IN THIS CELL """ # Save parameters for checkpoint helper.save_params(save_path)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Checkpoint
""" DON'T MODIFY ANYTHING IN THIS CELL """ import tensorflow as tf import numpy as np import helper import problem_unittests as tests _, (source_vocab_to_int, target_vocab_to_int), (source_int_to_vocab, target_int_to_vocab) = helper.load_preprocess() load_path = helper.load_params()
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Sentence to Sequence To feed a sentence into the model for translation, you first need to preprocess it. Implement the function sentence_to_seq() to preprocess new sentences. Convert the sentence to lowercase Convert words into ids using vocab_to_int Convert words not in the vocabulary, to the &lt;UNK&gt; word id.
def sentence_to_seq(sentence, vocab_to_int): """ Convert a sentence to a sequence of ids :param sentence: String :param vocab_to_int: Dictionary to go from the words to an id :return: List of word ids """ return [vocab_to_int[word] if word in vocab_to_int else vocab_to_int['<UNK>'] for word in sentence.lower().split()] """ DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE """ tests.test_sentence_to_seq(sentence_to_seq)
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
Translate This will translate translate_sentence from English to French.
translate_sentence = 'he saw a old yellow truck .' """ DON'T MODIFY ANYTHING IN THIS CELL """ translate_sentence = sentence_to_seq(translate_sentence, source_vocab_to_int) loaded_graph = tf.Graph() with tf.Session(graph=loaded_graph) as sess: # Load saved model loader = tf.train.import_meta_graph(load_path + '.meta') loader.restore(sess, load_path) input_data = loaded_graph.get_tensor_by_name('input:0') logits = loaded_graph.get_tensor_by_name('logits:0') keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0') translate_logits = sess.run(logits, {input_data: [translate_sentence], keep_prob: 1.0})[0] print('Input') print(' Word Ids: {}'.format([i for i in translate_sentence])) print(' English Words: {}'.format([source_int_to_vocab[i] for i in translate_sentence])) print('\nPrediction') print(' Word Ids: {}'.format([i for i in np.argmax(translate_logits, 1)])) print(' French Words: {}'.format([target_int_to_vocab[i] for i in np.argmax(translate_logits, 1)]))
py3/project-4/dlnd_language_translation.ipynb
jjonte/udacity-deeplearning-nd
unlicense
TensorFlow example Note: Change the action from update to create if you are deploying the model for the first time.
kfserving_op = components.load_component_from_url( 'https://raw.githubusercontent.com/kubeflow/pipelines/65bed9b6d1d676ef2d541a970d3edc0aee12400d/components/kubeflow/kfserving/component.yaml' ) @dsl.pipeline( name='kfserving pipeline', description='A pipeline for kfserving.' ) def kfservingPipeline( action = 'update', model_name='tf-sample', default_model_uri='gs://kfserving-samples/models/tensorflow/flowers', canary_model_uri='gs://kfserving-samples/models/tensorflow/flowers-2', canary_model_traffic_percentage='10', namespace='your_namespace', framework='tensorflow', default_custom_model_spec='{}', canary_custom_model_spec='{}', autoscaling_target='0', kfserving_endpoint='' ): # define workflow kfserving = kfserving_op(action = action, model_name=model_name, default_model_uri=default_model_uri, canary_model_uri=canary_model_uri, canary_model_traffic_percentage=canary_model_traffic_percentage, namespace=namespace, framework=framework, default_custom_model_spec=default_custom_model_spec, canary_custom_model_spec=canary_custom_model_spec, autoscaling_target=autoscaling_target, kfserving_endpoint=kfserving_endpoint).set_image_pull_policy('Always') # Compile pipeline import kfp.compiler as compiler compiler.Compiler().compile(kfservingPipeline, 'tf-flower.tar.gz') # Execute pipeline run = client.run_pipeline(experiment.id, 'tf-flower', 'tf-flower.tar.gz')
docs/samples/pipelines/kfs-pipeline-v1alpha2.ipynb
kubeflow/kfserving-lts
apache-2.0
Custom model example
kfserving_op = components.load_component_from_url( 'https://raw.githubusercontent.com/kubeflow/pipelines/65bed9b6d1d676ef2d541a970d3edc0aee12400d/components/kubeflow/kfserving/component.yaml' ) @dsl.pipeline( name='kfserving pipeline', description='A pipeline for kfserving.' ) def kfservingPipeline( action = 'update', model_name='custom-sample', default_model_uri='', canary_model_uri='', canary_model_traffic_percentage='0', namespace='kubeflow', framework='custom', default_custom_model_spec='{"name": "image-segmenter", "image": "codait/max-image-segmenter:latest", "port": "5000"}', canary_custom_model_spec='{}', autoscaling_target='0', kfserving_endpoint='' ): # define workflow kfserving = kfserving_op(action = action, model_name=model_name, default_model_uri=default_model_uri, canary_model_uri=canary_model_uri, canary_model_traffic_percentage=canary_model_traffic_percentage, namespace=namespace, framework=framework, default_custom_model_spec=default_custom_model_spec, canary_custom_model_spec=canary_custom_model_spec, autoscaling_target=autoscaling_target, kfserving_endpoint=kfserving_endpoint).set_image_pull_policy('Always') # Compile pipeline import kfp.compiler as compiler compiler.Compiler().compile(kfservingPipeline, 'custom.tar.gz') # Execute pipeline run = client.run_pipeline(experiment.id, 'custom-model', 'custom.tar.gz')
docs/samples/pipelines/kfs-pipeline-v1alpha2.ipynb
kubeflow/kfserving-lts
apache-2.0
First we want to download a video, so that we can compare the algorithmic result against the original video. The file is downloaded, if it does not already exist in the working directory. Next, it will create a directory of the same name, and unzip the file contents (Campus.zip to Campus/filename).
import numpy as np import matplotlib.pyplot as plt %matplotlib inline try: from urllib2 import urlopen except ImportError: from urllib.request import urlopen from scipy.io import loadmat, savemat import os ext = {"water":'WaterSurface.zip', "fountain":'Fountain.zip', "campus":'Campus.zip', "escalator": 'Escalator.zip', "curtain": 'Curtain.zip', "lobby": 'Lobby.zip', "mall": 'ShoppingMall.zip', "hall": 'hall.zip', "bootstrap": 'Bootstrap.zip'} example = "mall" def progress_bar_downloader(url, fname, progress_update_every=5): #from http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python/22776#22776 u = urlopen(url) f = open(fname, 'wb') meta = u.info() file_size = int(meta.get("Content-Length")) print("Downloading: %s Bytes: %s" % (fname, file_size)) file_size_dl = 0 block_sz = 8192 p = 0 while True: buffer = u.read(block_sz) if not buffer: break file_size_dl += len(buffer) f.write(buffer) if (file_size_dl * 100. / file_size) > p: status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) print(status) p += progress_update_every f.close() def get_video_clip(d): #Download files from http://perception.i2r.a-star.edu.sg/bk_model/bk_index.html if os.path.exists('./' + d): print('Video file %s already downloaded, continuing' % d) return else: print('Video file %s not found, downloading' % d) progress_bar_downloader(r'http://perception.i2r.a-star.edu.sg/BK_Model_TestData/' + d, d) def bname(x): return x.split('.')[0] get_video_clip(ext[example]) if not os.path.exists('./' + bname(ext[example])): os.makedirs(bname(ext[example])) os.system('unzip ' + ext[example] + ' -d ' + bname(ext[example]))
blogsite/posts/robust-matrix-decomposition.ipynb
kastnerkyle/kastnerkyle.github.io-nikola
bsd-3-clause
The code below will read in all the .bmp images downloaded and unzipped from the website, as well as converting to grayscale, scaling the result between 0 and 1. Eventually, I plan to do a "full-color" version of this testing, but for now the greyscale will have to suffice.
from scipy import misc import numpy as np from glob import glob def rgb2gray(rgb): r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] gray = 0.2989 * r + 0.5870 * g + 0.1140 * b return gray / 255. fdir = bname(ext[example]) names = sorted(glob(fdir + "/*.bmp")) d1, d2, channels = misc.imread(names[0]).shape d1 = 128 d2 = 160 num = len(names) X = np.zeros((d1, d2, num)) for n, i in enumerate(names): X[:, :, n] = misc.imresize(rgb2gray(misc.imread(i).astype(np.double)) / 255., (d1, d2)) X = X.reshape(d1 * d2, num) clip = 100 print(X.shape) print(d1) print(d2)
blogsite/posts/robust-matrix-decomposition.ipynb
kastnerkyle/kastnerkyle.github.io-nikola
bsd-3-clause
Robust PCA Robust Principal Component Analysis (PCA) is an extension of PCA. Rather than attempting to solve $X = L$, where $L$ is typically a low-rank approximation ($N \times M$, vs. $N \times P$, $M < P$), Robust PCA solves the factorization problem $X = L + S$, where $L$ is a low-rank approximation, and $S$ is a sparse component. By separating the factorization into two separate matrix components, Robust PCA makes a much better low-rank estimate $L$ on many problems. There are a variety of algorithms to solve this optimization problem. The code below is an implementation of the Inexact Augmented Lagrangian Multiplier algorithm for Robust PCA which is identical to the equivalent MATLAB code (download), or as near as I could make it. The functionality seems equivalent, and for relevant details please see the paper. This algorithm was chosen because according to the timing results at the bottom of this page, it was both the fastest and most accurate of the formulas listed. Though it appears to be fairly slow in our testing, it is fully believable that this is an implementation issue, since this code has not been specifically optimized for numpy. Due to this limitation, we clip the algorithm to the first few frames to save time.
import numpy as np from numpy.linalg import norm, svd def inexact_augmented_lagrange_multiplier(X, lmbda=.01, tol=1e-3, maxiter=100, verbose=True): """ Inexact Augmented Lagrange Multiplier """ Y = X norm_two = norm(Y.ravel(), 2) norm_inf = norm(Y.ravel(), np.inf) / lmbda dual_norm = np.max([norm_two, norm_inf]) Y = Y / dual_norm A = np.zeros(Y.shape) E = np.zeros(Y.shape) dnorm = norm(X, 'fro') mu = 1.25 / norm_two rho = 1.5 sv = 10. n = Y.shape[0] itr = 0 while True: Eraw = X - A + (1 / mu) * Y Eupdate = np.maximum(Eraw - lmbda / mu, 0) + np.minimum(Eraw + lmbda / mu, 0) U, S, V = svd(X - Eupdate + (1 / mu) * Y, full_matrices=False) svp = (S > 1 / mu).shape[0] if svp < sv: sv = np.min([svp + 1, n]) else: sv = np.min([svp + round(.05 * n), n]) Aupdate = np.dot(np.dot(U[:, :svp], np.diag(S[:svp] - 1 / mu)), V[:svp, :]) A = Aupdate E = Eupdate Z = X - A - E Y = Y + mu * Z mu = np.min([mu * rho, mu * 1e7]) itr += 1 if ((norm(Z, 'fro') / dnorm) < tol) or (itr >= maxiter): break if verbose: print("Finished at iteration %d" % (itr)) return A, E sz = clip A, E = inexact_augmented_lagrange_multiplier(X[:, :sz]) A = A.reshape(d1, d2, sz) * 255. E = E.reshape(d1, d2, sz) * 255. #Refer to them by position desired for video demo later savemat("./IALM_background_subtraction.mat", {"1": A, "2": E}) print("RPCA complete")
blogsite/posts/robust-matrix-decomposition.ipynb
kastnerkyle/kastnerkyle.github.io-nikola
bsd-3-clause
GoDec The code below contains an implementation of the GoDec algorithm, which attempts to solve the problem $X = L + S + G$, with $L$ low-rank, $S$ sparse, and $G$ as a component of Gaussian noise. By allowing the decomposition to expand to 3 matrix components, the algorithm is able to more effectively differentiate the sparse component from the low-rank.
import numpy as np from numpy.linalg import norm from scipy.linalg import qr def wthresh(a, thresh): #Soft wavelet threshold res = np.abs(a) - thresh return np.sign(a) * ((res > 0) * res) #Default threshold of .03 is assumed to be for input in the range 0-1... #original matlab had 8 out of 255, which is about .03 scaled to 0-1 range def go_dec(X, thresh=.03, rank=2, power=0, tol=1e-3, max_iter=100, random_seed=0, verbose=True): m, n = X.shape if m < n: X = X.T m, n = X.shape L = X S = np.zeros(L.shape) itr = 0 random_state = np.random.RandomState(random_seed) while True: Y2 = random_state.randn(n, rank) for i in range(power + 1): Y1 = np.dot(L, Y2) Y2 = np.dot(L.T, Y1); Q, R = qr(Y2, mode='economic') L_new = np.dot(np.dot(L, Q), Q.T) T = L - L_new + S L = L_new S = wthresh(T, thresh) T -= S err = norm(T.ravel(), 2) if (err < tol) or (itr >= max_iter): break L += T itr += 1 #Is this even useful in soft GoDec? May be a display issue... G = X - L - S if m < n: L = L.T S = S.T G = G.T if verbose: print("Finished at iteration %d" % (itr)) return L, S, G sz = clip L, S, G = go_dec(X[:, :sz]) L = L.reshape(d1, d2, sz) * 255. S = S.reshape(d1, d2, sz) * 255. G = G.reshape(d1, d2, sz) * 255. savemat("./GoDec_background_subtraction.mat", {"1": L, "2": S, "3": G, }) print("GoDec complete")
blogsite/posts/robust-matrix-decomposition.ipynb
kastnerkyle/kastnerkyle.github.io-nikola
bsd-3-clause
A Momentary Lapse of Reason Now it is time to do something a little unreasonable - we can actually take all of this data, reshape it into a series of images, and plot it as a video inside the IPython notebook! The first step is to generate the frames for the video as .png files, as shown below.
import os import sys import matplotlib.pyplot as plt from scipy.io import loadmat import numpy as np from matplotlib import cm import matplotlib #demo inspired by / stolen from @kuantkid on Github - nice work! def mlabdefaults(): matplotlib.rcParams['lines.linewidth'] = 1.5 matplotlib.rcParams['savefig.dpi'] = 300 matplotlib.rcParams['font.size'] = 22 matplotlib.rcParams['font.family'] = "Times New Roman" matplotlib.rcParams['legend.fontsize'] = "small" matplotlib.rcParams['legend.fancybox'] = True matplotlib.rcParams['lines.markersize'] = 10 matplotlib.rcParams['figure.figsize'] = 8, 5.6 matplotlib.rcParams['legend.labelspacing'] = 0.1 matplotlib.rcParams['legend.borderpad'] = 0.1 matplotlib.rcParams['legend.borderaxespad'] = 0.2 matplotlib.rcParams['font.monospace'] = "Courier New" matplotlib.rcParams['savefig.dpi'] = 200 def make_video(alg, cache_path='/tmp/matrix_dec_tmp'): name = alg if not os.path.exists(cache_path): os.mkdir(cache_path) #If you generate a big if not os.path.exists('%s/%s_tmp'%(cache_path, name)): os.mkdir("%s/%s_tmp"%(cache_path, name)) mat = loadmat('./%s_background_subtraction.mat'%(name)) org = X.reshape(d1, d2, X.shape[1]) * 255. fig = plt.figure() ax = fig.add_subplot(111) usable = [x for x in sorted(mat.keys()) if "_" not in x][0] sz = min(org.shape[2], mat[usable].shape[2]) for i in range(sz): ax.cla() ax.axis("off") ax.imshow(np.hstack([mat[x][:, :, i] for x in sorted(mat.keys()) if "_" not in x] + \ [org[:, :, i]]), cm.gray) fname_ = '%s/%s_tmp/_tmp%03d.png'%(cache_path, name, i) if (i % 25) == 0: print('Completed frame', i, 'of', sz, 'for method', name) fig.tight_layout() fig.savefig(fname_, bbox_inches="tight") #Write out an mp4 and webm video from the png files. -r 5 means 5 frames a second #libx264 is h.264 encoding, -s 160x130 is the image size #You may need to sudo apt-get install libavcodec plt.close() num_arrays = na = len([x for x in mat.keys() if "_" not in x]) cdims = (na * d1, d2) cmd_h264 = "ffmpeg -y -r 10 -i '%s/%s_tmp/_tmp%%03d.png' -c:v libx264 " % (cache_path, name) + \ "-s %dx%d -preset ultrafast -pix_fmt yuv420p %s_animation.mp4" % (cdims[0], cdims[1], name) cmd_vp8 = "ffmpeg -y -r 10 -i '%s/%s_tmp/_tmp%%03d.png' -c:v libvpx " % (cache_path, name) + \ "-s %dx%d -preset ultrafast -pix_fmt yuv420p %s_animation.webm" % (cdims[0], cdims[1], name) os.system(cmd_h264) os.system(cmd_vp8) if __name__ == "__main__": mlabdefaults() all_methods = ['IALM', 'GoDec'] for name in all_methods: make_video(name); print("Background is generated from this file:", example)
blogsite/posts/robust-matrix-decomposition.ipynb
kastnerkyle/kastnerkyle.github.io-nikola
bsd-3-clause
Echoes The code below will display HTML5 video for each of the videos generated in the previos step, and embed it in the IPython notebook. There are "echoes" of people, which are much more pronounced in the Robust PCA video than the GoDec version, likely due to the increased flexibility of an independent Gaussian term. Overall, the effect is pretty cool though not mathematically as good as the GoDec result.
from IPython.display import HTML from base64 import b64encode def html5_video(alg, frames): #This *should* support all browsers... framesz = 250 info = {"mp4": {"ext":"mp4", "encoded": '', "size":(frames * framesz, framesz)}} html_output = [] for k in info.keys(): f = open("%s_animation.%s" % (alg, info[k]["ext"]), "rb").read() encoded = b64encode(f).decode('ascii') video_tag = '<video width="500" height="250" autoplay="autoplay" ' + \ 'loop src="data:video/%s;base64,%s">' % (k, encoded) html_output.append(video_tag) return HTML(data=''.join(html_output))
blogsite/posts/robust-matrix-decomposition.ipynb
kastnerkyle/kastnerkyle.github.io-nikola
bsd-3-clause
If these videos freeze for some reason, just hit refresh and they should start playing.
html5_video("IALM", 3) html5_video("GoDec", 4)
blogsite/posts/robust-matrix-decomposition.ipynb
kastnerkyle/kastnerkyle.github.io-nikola
bsd-3-clause
Load CTCF ChIP-seq peaks for HFF from ENCODE This approach makes use of the narrowPeak schema for bioframe.read_table .
ctcf_peaks = bioframe.read_table("https://www.encodeproject.org/files/ENCFF401MQL/@@download/ENCFF401MQL.bed.gz", schema='narrowPeak') ctcf_peaks[0:5]
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
Get CTCF motifs from JASPAR
### CTCF motif: http://jaspar.genereg.net/matrix/MA0139.1/ jaspar_url = 'http://expdata.cmmt.ubc.ca/JASPAR/downloads/UCSC_tracks/2022/hg38/' jaspar_motif_file = 'MA0139.1.tsv.gz' ctcf_motifs = bioframe.read_table(jaspar_url+jaspar_motif_file,schema='jaspar',skiprows=1) ctcf_motifs[0:4]
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
Overlap peaks & motifs
df_peaks_motifs = bioframe.overlap(ctcf_peaks,ctcf_motifs, suffixes=('_1','_2'), return_index=True)
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
There are often multiple motifs overlapping one ChIP-seq peak, and a substantial number of peaks without motifs:
# note that counting motifs per peak can also be handled directly with bioframe.count_overlaps # but since we re-use df_peaks_motifs below we instead use the pandas operations directly motifs_per_peak = df_peaks_motifs.groupby(["index_1"])["index_2"].count().values plt.hist(motifs_per_peak,np.arange(0,np.max(motifs_per_peak))) plt.xlabel('number of overlapping motifs per peak') plt.ylabel('number of peaks') plt.semilogy(); print(f'fraction of peaks without motifs {np.round(np.sum(motifs_per_peak==0)/len(motifs_per_peak),2)}')
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
assign the strongest motif to each peak
# since idxmax does not currently take NA, fill with -1 df_peaks_motifs['pval_2'] = df_peaks_motifs['pval_2'].fillna(-1) idxmax_peaks_motifs = df_peaks_motifs.groupby(["chrom_1", "start_1","end_1"])["pval_2"].idxmax().values df_peaks_maxmotif = df_peaks_motifs.loc[idxmax_peaks_motifs] df_peaks_maxmotif['pval_2'].replace(-1,np.nan,inplace=True)
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
stronger peaks tend to have stronger motifs:
plt.rcParams['font.size']=12 df_peaks_maxmotif['fc_1'] = df_peaks_maxmotif['fc_1'].values.astype('float') plt.scatter(df_peaks_maxmotif['fc_1'].values, df_peaks_maxmotif['pval_2'].values, 5, alpha=0.5,lw=0) plt.xlabel('ENCODE CTCF peak strength, fc') plt.ylabel('JASPAR CTCF motif strength \n (-log10 pval *100)') plt.title('corr: '+str(np.round(df_peaks_maxmotif['fc_1'].corr(df_peaks_maxmotif['pval_2']),2)));
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
We can also ask the reverse question: how many motifs overlap a ChIP-seq peak?
df_motifs_peaks = bioframe.overlap(ctcf_motifs,ctcf_peaks,how='left', suffixes=('_1','_2')) m = df_motifs_peaks.sort_values('pval_1') plt.plot( m['pval_1'].values[::-1] , np.cumsum(pd.isnull(m['chrom_2'].values[::-1])==0)/np.arange(1,len(m)+1)) plt.xlabel('pval') plt.ylabel('probability motif overlaps a peak');
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
filter peaks overlapping blacklisted regions do any of our peaks overlap blacklisted genomic regions?
blacklist = bioframe.read_table('https://www.encodeproject.org/files/ENCFF356LFX/@@download/ENCFF356LFX.bed.gz', schema='bed3') blacklist[0:3]
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
there appears to be a small spike in the number of peaks close to blacklist regions
closest_to_blacklist = bioframe.closest(ctcf_peaks,blacklist) plt.hist(closest_to_blacklist['distance'].astype('Float64').astype('float'),np.arange(0,1e4,100));
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
to be safe, let's remove anything +/- 1kb from a blacklisted region
# first let's select the columns we want for our final dataframe of peaks with motifs df_peaks_maxmotif = df_peaks_maxmotif[ ['chrom_1','start_1','end_1','fc_1', 'chrom_2','start_2','end_2','pval_2','strand_2']] # then rename columns for convenience when subtracting for i in df_peaks_maxmotif.keys(): if '_1' in i: df_peaks_maxmotif.rename(columns={i:i.split('_')[0]},inplace=True) # now subtract, expanding the blacklist by 1kb df_peaks_maxmotif_clean = bioframe.subtract(df_peaks_maxmotif,bioframe.expand(blacklist,1000))
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
there it is! we now have a dataframe containing positions of CTCF ChIP peaks, including the strongest motif underlying that peak, and after conservative filtering for proximity to blacklisted regions
df_peaks_maxmotif_clean.iloc[7:15]
docs/tutorials/tutorial_assign_motifs_to_peaks.ipynb
open2c/bioframe
mit
1. Your first steps with Python 1.1 Introduction Python is a general purpose programming language. It is used extensively for scientific computing, data analytics and visualization, web development and software development. It has a wide user base and excellent library support. There are many ways to use and interact with the Python language. The first way is to access it directly from the command prompt and calling python &lt;script&gt;.py. This runs a script written in Python and does whatever you have programmed the computer to do. But scripts have to be written and how do we actually write Python scripts? Actually Python scripts are just .txt files. So you could just open a .txt file and write a script, saving the file with a .py extension. The downsides of this approach is obvious to anyone working with Windows. Usually, Python source code is written-not with Microsoft Word- but with and Integrated Development Environment. An IDE combines a text editor with a running Python console to test code and actually do work with Python without switching from one program to another. If you learnt the C, or C++ language, you will be familiar with Vim. Other popular IDE's for Python are Pycharm, Spyder and the Jupyter Notebook. In this course, we will use the Jupyter Notebook as our IDE because of its ease of use ability to execute code cell by cell. It integrates with markdown so that one can annotate and document your code on the fly! All in all, it is an excellent tool for teaching and learning Python before one migrates to more advanced tools like Spyder for serious scripting and development work. 1.2 Your best friends. In order to get the most from Python, your best source of reference is the Python documentation. Getting good at Python is a matter using it regularly and familiarizing yourself with the keywords, constructs and commonly used idioms. Learn to use the Shift-Tab when coding. This activates a hovering tooltip that provides documentation for keywords, functions and even variables that you have declared in your environment. This convenient tooltip and be expanded into a pop-up window on your browser for easy reference. Use this often to reference function signatures, documentation and general help. Jupyter notebook comes with Tab completion. This quality of life assists you in typing code by listing possible autocompletion options so that you don't have to type everything out! Use Tab completion as often as you can. This makes coding faster and less tedious. Tab completion also allows you to check out various methods on classes which comes in handy when learning a library for the first time (like matplotlib or seaborn). Finally ask Google. Once you have acquired enough "vocabulary", you can begin to query Google with your problem. And more often that not, somehow has experienced the same conundrum and left a message on Stackexchange. Browsing the solutions listed there is a powerful way to learn programming skills. 1.3 The learning objectives for this unit The learning objectives of this first unit are: Getting around the Jupyter notebook. Learning how to print("Hello world!") Using and coding with basic Python objects: int, str, float and bool. Using the type function. What are variables and valid variable names. Using the list object and list methods. Learning how to access items in list. Slicing and indexing. 2. Getting around the Jupyter notebook 2.1 Cells and colors, just remember, green is for go All code is written in cells. Cells are where code blocks go. You execute a cell by pressing Shift-Enter or pressing the "play" button. Or you could just click on the drop down menu and select "Run cell" but who would want to do that! In general, cells have two uses: One for writing "live" Python code which can be executed and one more to write documentation using markdown. To toggle between the two cell types, press Escape to exit from "edit" mode. The edges of the cell should turn blue. Now you are in "command" mode. Escape actually activates "command" mode. Enter activates "edit" mode. With the cell border coloured blue, press M to enter into markdown mode. You should see the In [ ]: prompt dissappear. Press Enter to change the border to green. This means you can now "edit" markdown. How does one change from markdown to a live coding cell? In "command" mode (remember blue border) press Y. Now the cell is "hot". When you Shift-Enter, you will execute code. If you happen to write markdown when in a "coding" cell, the Python kernel will shout at you. (Means raise an error message) 2.1.1 Practise makes perfect Now its time for you to try. In the cell below, try switching to Markdown. Press Enter to activate "edit" mode and type some text in the cell. Press Shift-Enter and you should see the output rendered in html. Note that this is not coding yet
# change this cell into a Markdown cell. Then type something here and execute it (Shift-Enter)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
2.2 Your first script It is a time honoured tradition that your very first program should be to print "Hello world!" How is this achieved in Python?
'''Make sure you are in "edit" mode and that this cell is for Coding ( You should see the In [ ]:) on the left of the cell. ''' print("Hello world!")
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Notice that Hello world! is printed at the bottom of the cell as an output. In general, this is how output of a python code is displayed to you. print is a special function in Python. It's purpose is to display output to the console. Notice that we pass an argument-in this case a string "Hello world!"- to the function. All arguments passed to the function must be enclosed in round brackets and this signals to the Python interpreter to execute a function named print with the argument "Hello world!". 2.2.1 Self introductions Your next exercise is to print your own name to the console. Remember to enclose your name in " " or ' '
# print your name in this cell.
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
2.3 Commenting Commenting is a way to annotate and document code. There are two ways to do this: Inline using the # character or by using ''' &lt;documentation block&gt; ''', the latter being multi-line and hence used mainly for documenting functions or classes. Comments enclosed using ''' '''' style commenting are actually registed in Jupyter notebook and can be accessed from the Shift-Tab tooltip! One should use # style commenting very sparingly. By right, code should be clear enough that # inline comments are not needed. However, # has a very important function. It is used for debugging and trouble-shooting. This is because commented code sections are never executed when you execute a cell (Shift-Enter) 3. Python's building blocks Python is an Object Oriented Programming language. That means to all of python is made out of objects which are instances of classes. The main point here is that I am going to introduce 4 basic objects of Python which form the backbone of any program or script. Integers or int. Strings or str. You've met one of these: "Hello world!". For those who know about character encoding, it is highly encouraged to code Python with UTF-8 encoding. Float or float. Basically the computer version of real numbers. Booleans or bool. In Python, true and false are indicated by the reserved keywords True and False. Take note of the capitalized first letter. 3.1 Numbers You can't call yourself a scientific computing language without the ability to deal with numbers. The basic arithmetic operations for numbers are exactly as you expect it to be
# Addition 5+3 # Subtraction 8-9 # Multiplication 3*12 # Division 48/12
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Note the floating point answer. In previous versions of Python, / meant floor division. This is no longer the case in Python 3
# Exponentiation. Limited precision though! 16**0.5 # Residue class modulo n 5%2
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
In the above 5%2 means return me the remainder after 5 is divided by 2 (which is indeed 1). 3.1.1 Precedence A note on arithmetic precedence. As one expects, () have the highest precedence, following by * and /. Addition and subtraction have the lowest precedence.
# Guess the output before executing this cell. Come on, don't cheat! 6%(1+3)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
It is interesting to note that the % operator is not distributive. 3.1.2 Variables In general, one does not have to declare variables in python before using it. We merely need to assign numbers to variables. In the computer, this means that a certain place in memory has been allocated to store that particular number. Assignment to variables is executed by the = operator. The equal sign in Python is the binary comparison == operator. Python is case sensitive. So a variable name A is different from a. Variables cannot begin with numbers and cannot have empty spaces between them. So my variable is not a valid variable. Usually what is done is to write my_variable After assigning numbers to variables, the variable can be used to represent the number in any arithmetic operation.
# Assignment x=1 y=2 x+y x/y
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Notice that after assignment, I can access the variables in a different cell. However, if you reassign a variable to a different number, the old values for that variable are overwritten.
x=5 x+y-2
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Now try clicking back to the cell x+y and re-executing it. What do you the answer will be? Even though that cell was above our reassignment cell, nevertheless re-executing that cell means executing that block of code that the latest values for that variable. It is for this reason that one must be very careful with the order of execution of code blocks. In order to help us keep track of the order of execution, each cell has a counter next to it. Notice the In [n]. Higher values of n indicates more recent executions. Variables can also be reassigned
# For example x = x+1 print(x)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
So what happened here? Well, if we recall x originally was assigned 5. Therefore x+1 would give us 6. This value is then reassigned to the exact same location in memory represented by the variable x. So now that piece of memory contains the value 6. We then use the print function to display the content of x. As this is a often used pattern, Python has a convenience syntax for this kind assignment
# reset x to 5 x=5 x += 1 print(x) x = 5 #What do you think the values of x will be for x -= 1, x *= 2 or x /= 2? # Test it out in the space below print(x)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
3.1.3 Floating point precision All of the above applies equally to floating point numbers (or real numbers). However, we must be mindful of floating point precision.
0.1+0.2
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
The following exerpt from the Python documentation explains what is happening quite clearly. To be fair, even our decimal system is inadequate to represent rational numbers like 1/3, 1/11 and so on. 3.2 Strings Strings are basically text. These are enclosed in ' ' or " ". The reason for having two ways of denoting strings is because we may need to nest a string within a string like in 'The quick brown fox "jumped" over the lazy old dog'. This is especially useful when setting up database queries and the like.
# Noting the difference between printing quoted variables (strings) and printing the variable itself. x = 5 print(x) print('x')
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
In the second print function, the text 'x' is printed while in the first print function, it is the contents of x which is printed to the console. 3.2.1 String formatting Strings can be assigned to variables just like numbers. And these can be recalled in a print function.
my_name = 'Tang U-Liang' print(my_name) # String formatting: Using the % age = 35 print('Hello doctor, my name is %s. I am %d years old. I weigh %.1f kg' % (my_name, age, 70.25)) # or using .format method print("Hi, I'm {name}. Please register {name} for this conference".format(name=my_name))
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
When using % to indicate string substitution, take note of the common formatting "placeholders" %s to substitue strings. %d for printing integer substitutions %.1f means to print a floating point number up to 1 decimal place. Note that there is no rounding The utility of the .format method arises when the same string needs to printed in various places in a larger body of text. This avoids duplicating code. Also did you notice I used double quotation. Why? More about string formats can be found in this excellent blog post 3.2.2 Weaving strings into one beautiful tapestry of text Besides the .format and % operation on text, we can concatenate strings using + operator. However, strings cannot be changed once declared and assigned to variables. This property is called immutability
fruit = 'Apple' drink = 'juice' print(fruit+drink) # concatenation #Don't like the lack of spacing between words? print(fruit+' '+drink)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Use [] to access specific letters in the string. Python uses 0 indexing. So the first letter is accessed by my_string[0] while my_string[1] accesses the second letter.
print(fruit[0]) print(fruit[1])
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Slicing is a way of get specific subsets of the string. If you let $x_n$ denote the $n+1$-th letter (note zero indexing) in a string (and by letter this includes whitespace characters as well!) then writing my_string[i:j] returns a subset $$x_i, x_{i+1}, \ldots, x_{j-1}$$ of letters in a string. That means the slice [i:j] takes all subsets of letters starting from index i and stops one index before the index indicated by j. 0 indexing and stopping point convention frequently trips up first time users. So take special note of this convention. 0 indexing is used throughout Python especially in matplotlib and pandas.
favourite_drink = fruit+' '+drink print("Printing the first to 3rd letter.") print(favourite_drink[0:3]) print("\nNow I want to print the second to seventh letter:") print(favourite_drink[1:7])
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Notice the use of \n in the second print function. This is called a newline character which does exactly what its name says. Also in the third print function notice the seperation between e and j. It is actually not seperated. The sixth letter is a whitespace character ' '. Slicing also utilizes arithmetic progressions to return even more specific subsets of strings. So [i:j:k] means that the slice will return $$ x_{i}, x_{i+k}, x_{i+2k}, \ldots, x_{i+mk}$$ where $m$ is the largest (resp. smallest) integer such that $i+mk \leq j-1$ (resp $1+mk \geq j+1$ if $i\geq j$)
print(favourite_drink[0:7:2]) # Here's a trick, try this out print(favourite_drink[3:0:-1])
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
So what happened above? Well [3:0:-1] means that starting from the 4-th letter $x_3$ which is 'l' return a subtring including $x_{2}, x_{1}$ as well. Note that the progression does not include $x_0 =$ 'A' because the stopping point is non-inclusive of j. The slice [:j] or [i:] means take substrings starting from the beginning up to the $j$-th letter (i.e. the $x_{j-1}$ letter) and substring starting from the $i+1$-th (i.e. the $x_{i}$) letter to the end of the string. 3.2.3 A mini challenge Print the string favourite_drink in reverse order. How would you do it?
# Write your answer here and check it with the output below
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Answer: eciuj elppA 3.3 The type function All objects in python are instances of classes. It is useful sometimes to find out what type of object we are looking at, especially if it has been assigned to a variable. For this we use the type function.
x = 5.0 type(x) type(favourite_drink) type(True) type(500)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
4. list, here's where the magic begins list are the fundamental data structure in Python. These are analogous to arrays in C or Java. If you use R, lists are analogous to vectors (and not R list) Declaring a list is as simple as using square brackets [ ] to enclose a list of objects (or variables) seperated by commas.
# Here's a list called staff containing his name, his age and current renumeration staff = ['Andy', 28, 980.15]
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
4.1 Properties of list objects and indexing One of the fundamental properties we can ask about lists is how many objects they contain. We use the len (short for length) function to do that.
len(staff)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Perhaps you want to recover that staff's name. It's in the first position of the list.
staff[0]
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Notice that Python still outputs to console even though we did not use the print function. Actually the print function prints a particularly "nice" string representation of the object, which is why Andy is printed without the quotation marks if print was used. Can you find me Andy's age now?
# type your answer here and run the cell
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
The same slicing rules for strings apply to lists as well. If we wanted Andy's age and wage, we would type staff[1:3]
staff[1:3]
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
This returns us a sub-list containing Andy's age and renumeration. 4.2 Nested lists Lists can also contain other lists. This ability to have a nested structure in lists gives it flexibility.
nested_list = ['apples', 'banana', [1.50, 0.40]]
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Notice that if I type nested_list[2], Python will return me the list [1.50, .40]. This can be accessed again using indexing (or slicing notation) [ ].
# Accesing items from within a nested list structure. print(nested_list[2]) # Assigning nested_list[2] to a variable. The variable price represents a list price = nested_list[2] print(type(price)) # Getting the smaller of the two floats print(nested_list[2][1])
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
4.3 List methods Right now, let us look at four very useful list methods. Methods are basically operations which modify lists. These are: pop which allows us to remove an item in a list. So for example if $x_0, x_1, \ldots, x_n$ are items in a list, calling my_list.pop(r) will modify the list so that it contains only $$x_0, \ldots, x_{r-1}, x_{r+1},\ldots, x_n$$ while returning the element $x_r$. append which adds items to the end of the list. Let's say $x_{n+1}$ is the new object you wish to append to the end of the list. Calling the method my_list.append(x_n+1) will modify the list inplace so that the list will now contain $$x_0, \ldots, x_n, x_{n+1}$$ Note that append does not return any output! insert which as the name suggests, allows us to add items to a list in a particular index location When using this, type my_list.insert(r, x_{n+1}) with the second argument to the method the object you wish to insert and r the position (still 0 indexed) where this object ought to go in that list. This method modifies the list inplace and does not return any output. After calling the insert method, the list now contains $$x_0,\ldots, x_{r-1}, x_{n+1}, x_{r}, \ldots, x_n$$ This means that my_list[r] = $x_{n+1}$ while my_list[r+1] = $x_{r}$ + is used to concatenate two lists. If you have two lists and want to join them together producing a union of two (or more lists), use this binary operator. This works by returning a union of two lists. So $$[ x_1,\ldots, x_n] + [y_1,\ldots, y_m]$$ is the list containing $$ x_1,\ldots, x_n,y_1, \ldots, y_m$$ This change is not permanent unless you assign the result of the operation to another variable.
# append staff.append('Finance') print(staff) # pop away the information about his salary andys_salary = staff.pop(2) print(andys_salary) print(staff) # oops, made a mistake, I want to reinsert information about his salary staff.insert(3, andys_salary) print(staff) contacts = [99993535, "[email protected]"] staff = staff+contacts # reassignment of the concatenated list back to staff print(staff)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
4.3.1 Your first programming challenge Move information for Andy's email to the second position (i.e. index 1) in the list staff in one line of code
staff = ['Andy', 28, 'Finance', 980.15, 99993535, '[email protected]'] staff # type your answer here print(staff)
Day 1 - Unit 1.1.ipynb
uliang/First-steps-with-the-Python-language
mit
Unit Test The following unit test is expected to fail until you solve the challenge.
# %load test_sort_stack.py from random import randint from nose.tools import assert_equal class TestSortStack(object): def get_sorted_stack(self, numbers): stack = MyStack() for x in numbers: stack.push(x) sorted_stack = stack.sort() return sorted_stack def test_sort_stack(self): print('Test: Empty stack') sorted_stack = self.get_sorted_stack([]) assert_equal(sorted_stack.pop(), None) print('Test: One element stack') sorted_stack = self.get_sorted_stack([1]) assert_equal(sorted_stack.pop(), 1) print('Test: Two or more element stack (general case)') num_items = 10 numbers = [randint(0, 10) for x in range(num_items)] sorted_stack = self.get_sorted_stack(numbers) sorted_numbers = [] for _ in range(num_items): sorted_numbers.append(sorted_stack.pop()) assert_equal(sorted_numbers, sorted(numbers, reverse=True)) print('Success: test_sort_stack') def main(): test = TestSortStack() test.test_sort_stack() if __name__ == '__main__': main()
interactive-coding-challenges/stacks_queues/sort_stack/sort_stack_challenge.ipynb
saashimi/code_guild
mit
Using PCA to extract features Now we'll take a look at unsupervised learning on a facial recognition example. This uses a dataset available within scikit-learn consisting of a subset of the Labeled Faces in the Wild data. Note that this is a relatively large download (~200MB) so it may take a while to execute.
from sklearn import datasets lfw_people = datasets.fetch_lfw_people(min_faces_per_person=70, resize=0.4, data_home='datasets') lfw_people.data.shape
notebooks/03.2 Methods - Unsupervised Preprocessing.ipynb
rhiever/scipy_2015_sklearn_tutorial
cc0-1.0
Let's visualize these faces to see what we're working with:
fig = plt.figure(figsize=(8, 6)) # plot several images for i in range(15): ax = fig.add_subplot(3, 5, i + 1, xticks=[], yticks=[]) ax.imshow(lfw_people.images[i], cmap=plt.cm.bone)
notebooks/03.2 Methods - Unsupervised Preprocessing.ipynb
rhiever/scipy_2015_sklearn_tutorial
cc0-1.0
We'll do a typical train-test split on the images before performing unsupervised learning:
from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(lfw_people.data, lfw_people.target, random_state=0) print(X_train.shape, X_test.shape)
notebooks/03.2 Methods - Unsupervised Preprocessing.ipynb
rhiever/scipy_2015_sklearn_tutorial
cc0-1.0
Feature Reduction Using Principal Component Analysis We can use PCA to reduce the original 1850 features of the face images to a manageable size, while maintaining most of the information in the dataset. Here it is useful to use a variant of PCA called RandomizedPCA, which is an approximation of PCA that can be much faster for large datasets.
from sklearn import decomposition pca = decomposition.RandomizedPCA(n_components=150, whiten=True) pca.fit(X_train)
notebooks/03.2 Methods - Unsupervised Preprocessing.ipynb
rhiever/scipy_2015_sklearn_tutorial
cc0-1.0
One interesting part of PCA is that it computes the "mean" face, which can be interesting to examine:
plt.imshow(pca.mean_.reshape((50, 37)), cmap=plt.cm.bone)
notebooks/03.2 Methods - Unsupervised Preprocessing.ipynb
rhiever/scipy_2015_sklearn_tutorial
cc0-1.0
The principal components measure deviations about this mean along orthogonal axes. It is also interesting to visualize these principal components:
print(pca.components_.shape) fig = plt.figure(figsize=(16, 6)) for i in range(30): ax = fig.add_subplot(3, 10, i + 1, xticks=[], yticks=[]) ax.imshow(pca.components_[i].reshape((50, 37)), cmap=plt.cm.bone)
notebooks/03.2 Methods - Unsupervised Preprocessing.ipynb
rhiever/scipy_2015_sklearn_tutorial
cc0-1.0
The components ("eigenfaces") are ordered by their importance from top-left to bottom-right. We see that the first few components seem to primarily take care of lighting conditions; the remaining components pull out certain identifying features: the nose, eyes, eyebrows, etc. With this projection computed, we can now project our original training and test data onto the PCA basis:
X_train_pca = pca.transform(X_train) X_test_pca = pca.transform(X_test) print(X_train_pca.shape) print(X_test_pca.shape)
notebooks/03.2 Methods - Unsupervised Preprocessing.ipynb
rhiever/scipy_2015_sklearn_tutorial
cc0-1.0
Compute MxNE with time-frequency sparse prior The TF-MxNE solver is a distributed inverse method (like dSPM or sLORETA) that promotes focal (sparse) sources (such as dipole fitting techniques) [1] [2]. The benefit of this approach is that: it is spatio-temporal without assuming stationarity (sources properties can vary over time) activations are localized in space, time and frequency in one step. with a built-in filtering process based on a short time Fourier transform (STFT), data does not need to be low passed (just high pass to make the signals zero mean). the solver solves a convex optimization problem, hence cannot be trapped in local minima. References .. [1] A. Gramfort, D. Strohmeier, J. Haueisen, M. Hamalainen, M. Kowalski "Time-Frequency Mixed-Norm Estimates: Sparse M/EEG imaging with non-stationary source activations", Neuroimage, Volume 70, pp. 410-422, 15 April 2013. DOI: 10.1016/j.neuroimage.2012.12.051 .. [2] A. Gramfort, D. Strohmeier, J. Haueisen, M. Hamalainen, M. Kowalski "Functional Brain Imaging with M/EEG Using Structured Sparsity in Time-Frequency Dictionaries", Proceedings Information Processing in Medical Imaging Lecture Notes in Computer Science, Volume 6801/2011, pp. 600-611, 2011. DOI: 10.1007/978-3-642-22092-0_49
# Author: Alexandre Gramfort <[email protected]> # Daniel Strohmeier <[email protected]> # # License: BSD (3-clause) import numpy as np import mne from mne.datasets import sample from mne.minimum_norm import make_inverse_operator, apply_inverse from mne.inverse_sparse import tf_mixed_norm, make_stc_from_dipoles from mne.viz import (plot_sparse_source_estimates, plot_dipole_locations, plot_dipole_amplitudes) print(__doc__) data_path = sample.data_path() subjects_dir = data_path + '/subjects' fwd_fname = data_path + '/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif' ave_fname = data_path + '/MEG/sample/sample_audvis-no-filter-ave.fif' cov_fname = data_path + '/MEG/sample/sample_audvis-shrunk-cov.fif' # Read noise covariance matrix cov = mne.read_cov(cov_fname) # Handling average file condition = 'Left visual' evoked = mne.read_evokeds(ave_fname, condition=condition, baseline=(None, 0)) evoked = mne.pick_channels_evoked(evoked) # We make the window slightly larger than what you'll eventually be interested # in ([-0.05, 0.3]) to avoid edge effects. evoked.crop(tmin=-0.1, tmax=0.4) # Handling forward solution forward = mne.read_forward_solution(fwd_fname)
0.18/_downloads/a35e576fa66929a73782579dc334f91a/plot_time_frequency_mixed_norm_inverse.ipynb
mne-tools/mne-tools.github.io
bsd-3-clause
Run solver
# alpha parameter is between 0 and 100 (100 gives 0 active source) alpha = 40. # general regularization parameter # l1_ratio parameter between 0 and 1 promotes temporal smoothness # (0 means no temporal regularization) l1_ratio = 0.03 # temporal regularization parameter loose, depth = 0.2, 0.9 # loose orientation & depth weighting # Compute dSPM solution to be used as weights in MxNE inverse_operator = make_inverse_operator(evoked.info, forward, cov, loose=loose, depth=depth) stc_dspm = apply_inverse(evoked, inverse_operator, lambda2=1. / 9., method='dSPM') # Compute TF-MxNE inverse solution with dipole output dipoles, residual = tf_mixed_norm( evoked, forward, cov, alpha=alpha, l1_ratio=l1_ratio, loose=loose, depth=depth, maxit=200, tol=1e-6, weights=stc_dspm, weights_min=8., debias=True, wsize=16, tstep=4, window=0.05, return_as_dipoles=True, return_residual=True) # Crop to remove edges for dip in dipoles: dip.crop(tmin=-0.05, tmax=0.3) evoked.crop(tmin=-0.05, tmax=0.3) residual.crop(tmin=-0.05, tmax=0.3)
0.18/_downloads/a35e576fa66929a73782579dc334f91a/plot_time_frequency_mixed_norm_inverse.ipynb
mne-tools/mne-tools.github.io
bsd-3-clause
Plot dipole activations
plot_dipole_amplitudes(dipoles) # Plot dipole location of the strongest dipole with MRI slices idx = np.argmax([np.max(np.abs(dip.amplitude)) for dip in dipoles]) plot_dipole_locations(dipoles[idx], forward['mri_head_t'], 'sample', subjects_dir=subjects_dir, mode='orthoview', idx='amplitude') # # Plot dipole locations of all dipoles with MRI slices # for dip in dipoles: # plot_dipole_locations(dip, forward['mri_head_t'], 'sample', # subjects_dir=subjects_dir, mode='orthoview', # idx='amplitude')
0.18/_downloads/a35e576fa66929a73782579dc334f91a/plot_time_frequency_mixed_norm_inverse.ipynb
mne-tools/mne-tools.github.io
bsd-3-clause
Show the evoked response and the residual for gradiometers
ylim = dict(grad=[-120, 120]) evoked.pick_types(meg='grad', exclude='bads') evoked.plot(titles=dict(grad='Evoked Response: Gradiometers'), ylim=ylim, proj=True, time_unit='s') residual.pick_types(meg='grad', exclude='bads') residual.plot(titles=dict(grad='Residuals: Gradiometers'), ylim=ylim, proj=True, time_unit='s')
0.18/_downloads/a35e576fa66929a73782579dc334f91a/plot_time_frequency_mixed_norm_inverse.ipynb
mne-tools/mne-tools.github.io
bsd-3-clause
Generate stc from dipoles
stc = make_stc_from_dipoles(dipoles, forward['src'])
0.18/_downloads/a35e576fa66929a73782579dc334f91a/plot_time_frequency_mixed_norm_inverse.ipynb
mne-tools/mne-tools.github.io
bsd-3-clause
View in 2D and 3D ("glass" brain like 3D plot)
plot_sparse_source_estimates(forward['src'], stc, bgcolor=(1, 1, 1), opacity=0.1, fig_name="TF-MxNE (cond %s)" % condition, modes=['sphere'], scale_factors=[1.]) time_label = 'TF-MxNE time=%0.2f ms' clim = dict(kind='value', lims=[10e-9, 15e-9, 20e-9]) brain = stc.plot('sample', 'inflated', 'rh', views='medial', clim=clim, time_label=time_label, smoothing_steps=5, subjects_dir=subjects_dir, initial_time=150, time_unit='ms') brain.add_label("V1", color="yellow", scalar_thresh=.5, borders=True) brain.add_label("V2", color="red", scalar_thresh=.5, borders=True)
0.18/_downloads/a35e576fa66929a73782579dc334f91a/plot_time_frequency_mixed_norm_inverse.ipynb
mne-tools/mne-tools.github.io
bsd-3-clause
As before, we can use a native Python function to organize the definition of our TFF into a reusable component.
from mantle import DFF class TFF(m.Circuit): IO = ['O', m.Out(m.Bit)] + m.ClockInterface() @classmethod def definition(io): # instance a dff to hold the state of the toggle flip-flop - this needs to be done first dff = DFF() # compute the next state as the not of the old state ff.O io.O <= dff(~dff.O) def tff(): return TFF()()
notebooks/tutorial/icestick/TFF.ipynb
phanrahan/magmathon
mit
Then we simply call this function inside our definition of the IceStick main.
from loam.boards.icestick import IceStick icestick = IceStick() icestick.Clock.on() icestick.J3[0].rename('J3').output().on() main = icestick.DefineMain() main.J3 <= tff() m.EndDefine()
notebooks/tutorial/icestick/TFF.ipynb
phanrahan/magmathon
mit
We'll compile and build our program using the standard flow.
m.compile("build/tff", main) %%bash cd build yosys -q -p 'synth_ice40 -top main -blif tff.blif' tff.v arachne-pnr -q -d 1k -o tff.txt -p tff.pcf tff.blif icepack tff.txt tff.bin #iceprog tff.bin
notebooks/tutorial/icestick/TFF.ipynb
phanrahan/magmathon
mit
Let's inspect the generated verilog.
%cat build/tff.v
notebooks/tutorial/icestick/TFF.ipynb
phanrahan/magmathon
mit
We can verify our implementation is function correctly by using a logic analyzer.
%cat build/tff.pcf
notebooks/tutorial/icestick/TFF.ipynb
phanrahan/magmathon
mit
=================================================================== Support Vector Regression (SVR) using linear and non-linear kernels =================================================================== Toy example of 1D regression using linear, polynomial and RBF kernels.
print(__doc__) import numpy as np from sklearn.svm import SVR import matplotlib.pyplot as plt
labwork/lab2/sci-learn/non_linear_regression.ipynb
chaitra8/ml_lab_ecsc_306
apache-2.0
Generate sample data
X = np.sort(5 * np.random.rand(40, 1), axis=0) y = np.sin(X).ravel()
labwork/lab2/sci-learn/non_linear_regression.ipynb
chaitra8/ml_lab_ecsc_306
apache-2.0
Add noise to targets
y[::5] += 3 * (0.5 - np.random.rand(8))
labwork/lab2/sci-learn/non_linear_regression.ipynb
chaitra8/ml_lab_ecsc_306
apache-2.0
Fit regression model
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1) svr_lin = SVR(kernel='linear', C=1e3) svr_poly = SVR(kernel='poly', C=1e3, degree=2) y_rbf = svr_rbf.fit(X, y).predict(X) y_lin = svr_lin.fit(X, y).predict(X) y_poly = svr_poly.fit(X, y).predict(X)
labwork/lab2/sci-learn/non_linear_regression.ipynb
chaitra8/ml_lab_ecsc_306
apache-2.0
look at the results
lw = 2 plt.scatter(X, y, color='darkorange', label='data') plt.hold('on') plt.plot(X, y_rbf, color='navy', lw=lw, label='RBF model') plt.plot(X, y_lin, color='c', lw=lw, label='Linear model') plt.plot(X, y_poly, color='cornflowerblue', lw=lw, label='Polynomial model') plt.xlabel('data') plt.ylabel('target') plt.title('Support Vector Regression') plt.legend() plt.show()
labwork/lab2/sci-learn/non_linear_regression.ipynb
chaitra8/ml_lab_ecsc_306
apache-2.0
De code is georganiseerd in cellen en als je wil kan je kan de code in de cellen aanpassen en opnieuw uitvoeren. Pas hierboven de som aan en voer ze opnieuw uit. ... doe maar, ik wacht hier even ... "Pffff, dat kan ik ook met eender welke rekenmachine" Klopt, maar dit is nog maar het begin; laat ons eens iets anders proberen:
print("Hallo allemaal!")
notebooks/nl-be/101 - Intro - Python leren kennen en IPython gebruiken.ipynb
RaspberryJamBe/ipython-notebooks
cc0-1.0
IPython zal, als het commando een resultaat geeft, deze "output" onder de cell uitprinten. En als je iets vergeet of fout typt, wordt-ie boos:
print("Dit lukt dus niet"
notebooks/nl-be/101 - Intro - Python leren kennen en IPython gebruiken.ipynb
RaspberryJamBe/ipython-notebooks
cc0-1.0
Er wordt dan door Python geprobeerd om uit te leggen wat er mis gaat, maar dat is niet altijd 100% duidelijk. Kan je uitvissen wat er hierboven misloopt? -Tip: het "Hallo allemaal" commando kan misschien helpen, het is maar een kleine vergetelheid, maar een computer kan van iets dergelijks helemaal in de war raken- OK, wat kan Python nog? Dingen onthouden daar zijn computers goed in (zolang je de stekker niet uittrekt...) Een variabele noemen we dat. Die moet je wel een naam geven, anders vind je ze niet meer terug:
a = 'Dit is een tekst' # tekst moet je tussen aanhalingstekens '...' zetten a = "Dit is een tekst" # maar het mogen ook dubbele aanhalingstekens "..." zijn (als je ze maar niet door mekaar haalt) # oh, ja en alles wat achter een # staat is commentaar, dat slaat Python gewoon over b = 13 c = 273.15 # voor decimale cijfers, geen komma's, maar punten!
notebooks/nl-be/101 - Intro - Python leren kennen en IPython gebruiken.ipynb
RaspberryJamBe/ipython-notebooks
cc0-1.0
Zie, je geen resultaat, dus IPython print niets uit, maar de variabelen zitten wel in het geheugen, kijk maar:
print(a, b, c)
notebooks/nl-be/101 - Intro - Python leren kennen en IPython gebruiken.ipynb
RaspberryJamBe/ipython-notebooks
cc0-1.0
Methodes en "dot notation" (punt notatie) sommige "dingen" of objecten die je in Python gebruikt krijgen een soort superpowers mee in de vorm van methodes die je kan aanroepen. Dit doe je door een punt achter het object te zetten en dan de methode te typen (opgelet, voor het aanroepen van een functie moet je altijd haakjes achter de functienaam zetten, desnoods zonder iets tussen):
# bvb een tekst in hoofdletters omzetten: a.upper()
notebooks/nl-be/101 - Intro - Python leren kennen en IPython gebruiken.ipynb
RaspberryJamBe/ipython-notebooks
cc0-1.0
Door na het punt op de <TAB> toets te drukken, zal IPython een lijst van beschikbare methodes laten zien; zet je cursor achter het punt en type <TAB> om het uit te proberen:
a.
notebooks/nl-be/101 - Intro - Python leren kennen en IPython gebruiken.ipynb
RaspberryJamBe/ipython-notebooks
cc0-1.0