An Introduction to Neural Style Transfer for Data Scientists

Text based neural style transfer can alter the style of your text

Photo by h heyerlein on Unsplash

If 2Pac was only allowed to release music under the pretence that his style was to match the Queen’s English, the world would have been a significantly worse place.

The advent of Style transfer (the ability to project a style of one text to another) means that it’s now possible for a Neural Network to change the feel of a text.

As you can probably guess, the application of this technology would make it useful in a number of different settings. A simple first application would be to make an article sound more formal:

Informal : I’d say it is punk though.

Formal : However, I do believe it to be punk.

but even further from this, this technology could be used to help people with problems like dyslexia.

More recently, the news that Microsoft was laying off journalists wasn’t groundbreaking news: advertising revenues are down across the board and newspapers are generally struggling to be as profitable as they were before (which was already a bit of a struggle). However, the news that they were to replace this team with AI is what startled people.

I’ve always loved writing but I’ve always sucked. My english teacher refused to let me answer questions, because undoubtedly, my answer would be wrong.

Fast forward 15 years and I’m building machine learning tools to solve just about any problem I can think of. More importantly, Neural Networks have recently found a new domain to better. Microsoft Word now incorporates a new AI that can offer to rewrite a suggestion in full, rather than simple spelling and grammatical fixes.

Have you ever been unable to express something in a given way?

Being unable to phrase something in a certain tone or to give of a certain impression is something that many writers struggle with. To preserve time, focus and energy, this tool will help writers to be able to more effectively captivate their audience by tilting the wording better. That’s what Microsoft aimed to fix here, and in what follows i’ll explain how. Microsoft have said:

“In internal evaluations, it was nearly 15 percent more effective than previous approaches in catching mistakes commonly made by people who have dyslexia.”

Neural Style Transfer

The updates that Microsoft have recently incorporated are broadly similar to the product that grammarly are well known for [can reference this]. Both sets of researchers are taking advantages of recent developments in the field of Style Transfer.

Neural Style Transfer was initially used between images, whereby, a certain composition of an image could be projected onto something similar.

Neural Style Transfer between Images [Source]

However, this technique has recently been adapted for the use case of text style transfer. To do this, researchers took advantage of neural machine translations models to serve the purpose of style transferring. Think about it: a certain ‘tone’ or ‘style’ could be seen as another language and therefore:

“We create the largest corpus for a particular stylistic transfer (formality) and show that techniques from the machine translation community can serve as strong baselines for future work”

The baseline model in the theory of neural machine translation is based on Yoshua Bengio’s paper here, building upon Sutskevers work on Sequence to Sequence learning. A neural network is formed as a RNN Encoder Decoder which works as follows.

[Source]

Here, a phrase is passed into the encoder which coverts the string into a vector. This vector effectively contains a latent representation of the phrase, which is then translated using a decoder. This is called an ‘encoder-decoder architecture’ and in this manner, Neural Machine Translation (NMT) can translate local translation problems.

An example of Neural Machine Translation from Sutskever et al (2014). We can see that all the japanese text is encoded into h values, which is then decoded into English.

For neural machine translation, it uses a bidirectional RNN to process the source sentence into vectors (encoding) along with a second RNN to predict words in the target language (decoding). This process, while differing from phrase-based models in method, prove to be comparable in speed and accuracy.

Creating a model

To create a neural style transfer model, we generally have 3 key steps that we have to take:

1) Embedding

Words are categorical in nature so the model must first be able to embed the words, finding an alternative representation that can be used in the network. A vocabulary (size V) is selected with only frequent words treated as unique, all other words are converted to an “unknown” token and get the same embedding. The embedding weights, one set per language, are usually learned during training.

# Embedding
embedding_encoder = variable_scope.get_variable("embedding_encoder", [src_vocab_size, embedding_size], ...)
encoder_emb_inp = embedding_ops.embedding_lookup(embedding_encoder, encoder_inputs)

2) Encoding

Once the word embedding are retrieved, they are fed as the input into the main model which consists of two multi-layer RNNs, where one of these is an encoder for the source language and the other is a decoder for the target language. In practice, these two RNN’s are trained to have different parameters (such models do a better job when fitting large training datasets).

# Build RNN cell
encoder_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)
# Run Dynamic RNN
encoder_outputs, encoder_state = tf.nn.dynamic_rnn(encoder_cell, encoder_emb_inp, sequence_length=source_sequence_length, time_major=True)

The reader who’s paying attention to the code will see that sentences can have different lengths and to avoid wasting computation here, we tell dynamic_rnn the exact source sentence lengths through source_sequence_length and since our input is time major, we set time_major=True.

3) Decoding

The decoder needs to have access to source information. A simple way to achieve this is to initialise it with the last hidden state of the encoder, encoder_state.

# Build RNN cell
decoder_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)
# Helper
helper = tf.contrib.seq2seq.TrainingHelper(decoder_emb_inp, decoder_lengths, time_major=True)
# Decoder
decoder = tf.contrib.seq2seq.BasicDecoder(decoder_cell, helper, encoder_state, output_layer=projection_layer)
# Dynamic decoding
outputs, _ = tf.contrib.seq2seq.dynamic_decode(decoder, ...)
logits = outputs.rnn_output

Lastly, we haven’t mentioned projection_layer which is a dense matrix to turn the top hidden states to logit vectors of dimension V. We illustrate this process at the top of Figure 2.

projection_layer = layers_core.Dense(tgt_vocab_size, use_bias=False)

and finally, given the logits above, we are now ready to compute our training loss:

crossent = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=decoder_outputs, logits=logits)
train_loss = (tf.reduce_sum(crossent * target_weights)/batch_size)

We have now defined the forward pass of our NMT model. Computing the back propagation pass is just a matter of a few lines of code:

# Calculate and clip gradients
params = tf.trainable_variables()
gradients = tf.gradients(train_loss, params)
clipped_gradients, _ = tf.clip_by_global_norm(gradients, max_gradient_norm)

Now from here, you’re ready to begin the optimisation procedures behind creating your own neural style transfer model!

Note: the code above was largely taken from the tensorflow github documentation and more information about this procedure can be found online.


The theory of Neural Machine Style Transfer is quite an extensive history and it’s taken a while for academia to reach the current perch it’s sat upon. Translations are a notoriously difficult task because of grammatical problems, but also, interpretability for text to sound somewhat human: somewhat colloquial.

The progress that’s been made is fantastic and it’s something that will be great if it keeps on developing.


Thanks for reading! If you have any messages, please let me know!

Keep up to date with my latest articles here!


Code Support:

  1. https://www.tensorflow.org/tutorials/generative/style_transfer
  2. https://github.com/fuzhenxin/Style-Transfer-in-Text
  3. https://www.groundai.com/project/what-is-wrong-with-style-transfer-for-texts/1

Using Docker for Deep Learning

Struggling to recreate your results? So was I

Photo by Maria Teneva on Unsplash

Deep Learning projects are always pretty big. They involve huge datasets, huge amounts of processing power but annoyingly, they also require a huge amount of investigation as more often then not, they just don’t work in the beginning. It’s after a bunch of feature selectionmodel fitting and parameter tuning do you get something that looks half decent.

In saying that though, running this all in the form of a container can be a bit daunting because not only do you need to deploy a server somewhere, you need to have tests in place to make sure that what’s happening inside your cordoned off container is expected. You need to make sure that your machine learning algorithm is converging in the right way, and you need to be sure that any errors are picked up.

Despite these (and many more) difficulties, using containers for Deep Learning comes with great rewards. The below are but a few of the main difficulties that are overcome by using containers.


Reason 1: Isolation

Just like how multiple containers can exist on a ship — computerised containers can exist in harmony on a server. The benefit of this is that resources are predefined, so machine learning algorithms can run in parallel on the same machine without competing for resources.

The only thing more demanding than one machine learning project is two, so if these are set up correctly, isolated projects can co-exist pretty well in a highly distributed environment.

Reason 2: Avoiding Dependency Conflicts

Machine Learning projects are often pretty large projects that use a wide variety of underlying tech. However, the problem comes when your classification algorithm uses the latest version of Numpy and your regression algorithm uses a more recent version — which should exist on your system?

In reality, these libraries can exist in parallel but require isolated systems. Just how your deep learning system may rely on conflicting libraries, containers allow for isolation so through time, you’re less likely to run into these really annoying problems.

Reason 3: Portability

In the process of creating a container, you generally predefine everything that goes into it (code and all). Given that, you can save the ‘image’ of the container and share it easily. It’s so useful that companies exist in this space (Note DockerHub).

Reason 4: Microservices

The awesome part of containers is that you’re able to expose certain parts of them. This means that if you want to create something like an API, then you can expose parts of the containers that allow you to run an API in a more controlled way. Specifically, this is great if your API involves a large computational process, or, something with a lot of moving parts. By separating workflows, you can identify where systems are failing much quicker.

Check the following reference to learn a bit more:3 reasons to always use containers for microservices-based applications
Microservices are the emerging application platform: It is the architecture that will serve as the basis for many…techbeacon.com

Reason 5: Reproducibility

I think the ultimate reason why containers are useful for Deep Learning is because they aid in reproduction. It’s pretty common for researchers to make ground-breaking discoveries when in research mode. However, once the algorithm is re-trained, they may find that a bug had caused the amazing results or they can’t recreate the results!

This is obviously an issue as you don’t know what gave you the great results so it makes sense to have some form of framework that allows you to robustly reproduce results. Code tracking engines obviously work (like GitHub) but its worth going that one step further to really push forward.


Code for Dockerising Deep Learning

If you want some simple hands on code to implement a Dockerised Deep Learning project, then this link here is great for just that.

If you find it a bit confusing, then this following project (using an example from the world of fashion) may be a little bit easier to digest.


The above were 5 quick reasons why containers are great for Deep Learning, and two references were given to help you learn how to implement it.

Docker is pretty powerful and despite being a couple of years old now (and a number of great iterations/improvements being developed), it’s still awesome to be able to get the functionality that one desires: that being isolation and reproducibility.

Thanks for reading! If you have any messages, please let me know!

Keep up to date with my latest articles here!

R vs Python: 8 Reasons Why Python Wins

Data Scientists using R should have switched a while ago

Photo by Kvistholt Photography on Unsplash

Every now and then, I get into a discussion with a Data Scientist who I just disagree with.

Be it something methodological or be it something thematic — different people have different approaches and sometimes, just sometimes, I’m definitely right.

R is a fantastic piece of software, there’s no denying that. However it’s limited by a lack of features and things that don’t evolve generally get replaced through time. If they do improve, the libraries utilised are often specialised and fragmented which can also result with dependency issues.

In what follows, I’ll go over 8 reasons why I chose to learn Python in the beginning, how it’s helped me in my career and smaller technical differences that can lead to larger problems down the line.


1: Python Is Everywhere

If you’re thinking about a programming language to learn then something which is being used widely should be a starting point. There’s no escaping this and it’s good thing. Python is being used by everyone: Data Scientists and not.

If you look at Google or StackOverflow trendsPython is super hot. More jobs also require Python coders than R coders.

If you want to be in with the crowd, Python is a good place to start.

2: Massive Online Community

If you’re ever stuck on a problem or you can’t figure something out, the online community of coders has to be one of the nicest places out there. Knowledge sharing in the coding community is altruistic and frequent, with many blogging and coding websites specifically targeting coders who are learning or are struggling to complete a task.

This community makes it so much easier to tackle harder problems and therefore, a bigger community allows coders to solve their problems quicker. I’ve relied heavily on the community and you will too.

3: Easy Deployment

When you make a piece of software or a nifty little tool, what do you do with it then? Do you want to keep it locally to look at or do you want to deploy it to fix the problem?

As a coder you should always be looking for problems and moreover, you should always be thinking about solutions. Is my code too slow? Is my code too clunky? Are my data tasks too bulky?

Regardless of what it is, you should be able to come up with a solution and integrate it easily. As Python can essentially do the whole vertical when it comes to coding, if you’ve made some statistical code, it’s easier to integrate it with Python than it is with R.

4: Python is the Whole Tech Stack

You have to wear multiple hats when you’re a data scientist. You have to be able to manage large data sets, you have to be able to clean them. Then you have to be able to analyse and deduce something important. After which you have to be able to integrate your findings into the business to improve an object (sales, efficiency etc).

Being able to do all of this in R just isn’t possible. However w

5: Not as hard to learn

With a larger online community meaning more resources to learn from, Python has one other advantage over RActually on UDacity, they say that R is easier to pick up if you’ve learned something like C++ or Java before.

Whilst I’m not fully convinced to the full extent of this logic, I can say for sure that R is less obvious in its coding syntax. Having more commands and more libraries is part of the problem, but having operators like <- and if functions like:

if (test_expression) {statement}

It definitely isn’t as easy as Python.

6: Better Documentation

Python has a few key libraries that really do the bulk of the work:

  • Numpy
  • Pandas
  • SKLearn
  • Scipy

Now these four libraries are open-source but they’ve been developed with a solid group of volunteers. They’ve been developed in a professional way so that all the documentation is complete, most functions come with tests (to ensure functionality) and also, documentation links to the academic references for which they follow.

On the other hand, R packages are generally made by academics/specialists who have little time and therefore, may not be able to make as high quality documentation as we’d like.

You can read the following discussion on Reddit for more information:

Or you can look at common R libraries and see what you think.

7: Python is Faster than R

Quick answer => Python!Is Python faster than R?
R vs Python Speed Benchmark on a simple Machine Learning Pipelinetowardsdatascience.com

But in reality, you shouldn’t use either of these languages for speed.

8: More Jobs

Empirical data is often the most informative and looking at the website indeed.com, as of right now (04/02/2020), there are 4,100 Python Developer jobs and only 291 jobs under the category of R Developer.

That means there’s almost 30x more Python jobs going than R jobs on this one website.

In light of that, a statistician would tell you that you’re also more likely to get a higher paid job, though you could argue R is more specialised so requires a higher salary. According to Business Insider, R has a slightly higher average salary.

From my personal experience though, every company I’ve worked with has generally opted for a Python based infrastructure and if they haven’t got it, they’re working towards it.


R is a great language but it’s just limited. From a Data Science perspective, it does match up to what Python has to offer but it’s all very fragmented. Python libraries like NumpyPandas and Sklearn carry so much responsibility, so much so that they allow developers to be lazy and live within a small bubble of possibility.

Maybe that’s a bad thing, maybe it’s a good thing. I’ve always said that if you want to be a good Data Scientist, you have to be know your scope from first principles — so if you can’t write out the formulae to your models, you shouldn’t be using them in the first place.


Thanks for reading! If you have any messages, please let me know!

Keep up to date with my latest articles here!

If 2Pac Spoke the Queen’s English

Machine Learning, Programming

What is Neural Text-Style Transfer?

Photo by Jan Střecha on Unsplash

If 2Pac was only allowed to release music under the pretence that his style was to match the Queen’s English, the world would have been a significantly worse place.

The advent of Style transfer (the ability to project a style of one text to another) means that it’s now possible for a Neural Network to change the feel of a text.

As you can probably guess, the application of this technology would make it useful in a number of different settings. A simple first application would be to make an article sound more formal:

Informal : I’d say it is punk though.

Formal : However, I do believe it to be punk.

but even further from this, this technology could be used to help people with problems like dyslexia.

More recently, the news that Microsoft was laying off journalists wasn’t groundbreaking news: advertising revenues are down across the board and newspapers are generally struggling to be as profitable as they were before (which was already a bit of a struggle). However, the news that they were to replace this team with AI is what startled people.

I’ve always loved writing but I’ve always sucked. My english teacher refused to let me answer questions, because undoubtedly, my answer would be wrong.

Fast forward 15 years and I’m building machine learning tools to solve just about any problem I can think of. More importantly, Neural Networks have recently found a new domain to better. Microsoft Word now incorporates a new AI that can offer to rewrite a suggestion in full, rather than simple spelling and grammatical fixes.

Have you ever been unable to express something in a given way?

Being unable to phrase something in a certain tone or to give of a certain impression is something that many writers struggle with. To preserve time, focus and energy, this tool will help writers to be able to more effectively captivate their audience by tilting the wording better. That’s what Microsoft aimed to fix here, and in what follows i’ll explain how. Microsoft have said:

“In internal evaluations, it was nearly 15 percent more effective than previous approaches in catching mistakes commonly made by people who have dyslexia.”

Neural Style Transfer

The updates that Microsoft have recently incorporated are broadly similar to the product that grammarly are well known for [can reference this]. Both sets of researchers are taking advantages of recent developments in the field of Style Transfer.

Neural Style Transfer was initially used between images, whereby, a certain composition of an image could be projected onto something similar.

Neural Style Transfer between Images [Source]

However, this technique has recently been adapted for the use case of text style transfer. To do this, researchers took advantage of neural machine translations models to serve the purpose of style transferring. Think about it: a certain ‘tone’ or ‘style’ could be seen as another language and therefore:

“We create the largest corpus for a particular stylistic transfer (formality) and show that techniques from the machine translation community can serve as strong baselines for future work”

The baseline model in the theory of neural machine translation is based on Yoshua Bengio’s paper here, building upon Sutskevers work on Sequence to Sequence learning. A neural network is formed as a RNN Encoder Decoder which works as follows.

[Source]

Here, a phrase is passed into the encoder which coverts the string into a vector. This vector effectively contains a latent representation of the phrase, which is then translated using a decoder. This is called an ‘encoder-decoder architecture’ and in this manner, Neural Machine Translation (NMT) can translate local translation problems.

An example of Neural Machine Translation from Sutskever et al (2014). We can see that all the japanese text is encoded into h values, which is then decoded into English.

For neural machine translation, it uses a bidirectional RNN to process the source sentence into vectors (encoding) along with a second RNN to predict words in the target language (decoding). This process, while differing from phrase-based models in method, prove to be comparable in speed and accuracy.

Creating a model

To create a neural style transfer model, we generally have 3 key steps that we have to take:

1) Embedding

Words are categorical in nature so the model must first be able to embed the words, finding an alternative representation that can be used in the network. A vocabulary (size V) is selected with only frequent words treated as unique, all other words are converted to an “unknown” token and get the same embedding. The embedding weights, one set per language, are usually learned during training.

# Embedding
embedding_encoder = variable_scope.get_variable("embedding_encoder", [src_vocab_size, embedding_size], ...)
encoder_emb_inp = embedding_ops.embedding_lookup(embedding_encoder, encoder_inputs)

2) Encoding

Once the word embedding are retrieved, they are fed as the input into the main model which consists of two multi-layer RNNs, where one of these is an encoder for the source language and the other is a decoder for the target language. In practice, these two RNN’s are trained to have different parameters (such models do a better job when fitting large training datasets).

# Build RNN cell
encoder_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)

# Run Dynamic RNN
encoder_outputs, encoder_state = tf.nn.dynamic_rnn(encoder_cell, encoder_emb_inp, sequence_length=source_sequence_length, time_major=True)

The reader who’s paying attention to the code will see that sentences can have different lengths and to avoid wasting computation here, we tell dynamic_rnn the exact source sentence lengths through source_sequence_length and since our input is time major, we set time_major=True.

3) Decoding

The decoder needs to have access to source information. A simple way to achieve this is to initialise it with the last hidden state of the encoder, encoder_state.

# Build RNN cell
decoder_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)
# Helper
helper = tf.contrib.seq2seq.TrainingHelper(decoder_emb_inp, decoder_lengths, time_major=True)
# Decoder
decoder = tf.contrib.seq2seq.BasicDecoder(decoder_cell, helper, encoder_state, output_layer=projection_layer)
# Dynamic decoding
outputs, _ = tf.contrib.seq2seq.dynamic_decode(decoder, ...)
logits = outputs.rnn_output

Lastly, we haven’t mentioned projection_layer which is a dense matrix to turn the top hidden states to logit vectors of dimension V. We illustrate this process at the top of Figure 2.

projection_layer = layers_core.Dense(tgt_vocab_size, use_bias=False)

and finally, given the logits above, we are now ready to compute our training loss:

crossent = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=decoder_outputs, logits=logits)
train_loss = (tf.reduce_sum(crossent * target_weights)/batch_size)

We have now defined the forward pass of our NMT model. Computing the back propagation pass is just a matter of a few lines of code:

# Calculate and clip gradients
params = tf.trainable_variables()
gradients = tf.gradients(train_loss, params)
clipped_gradients, _ = tf.clip_by_global_norm(gradients, max_gradient_norm)

Now from here, you’re ready to begin the optimisation procedures behind creating your own neural style transfer model!

Note: the code above was largely taken from the tensorflow github documentation and more information about this procedure can be found online.


The theory of Neural Machine Style Transfer is quite an extensive history and it’s taken a while for academia to reach the current perch it’s sat upon. Translations are a notoriously difficult task because of grammatical problems, but also, interpretability for text to sound somewhat human: somewhat colloquial.

The progress that’s been made is fantastic and it’s something that will be great if it keeps on developing.


Thanks for reading! If you have any messages, please let me know!

Keep up to date with my latest articles here!


Code Support:

  1. https://www.tensorflow.org/tutorials/generative/style_transfer
  2. https://github.com/fuzhenxin/Style-Transfer-in-Text
  3. https://www.groundai.com/project/what-is-wrong-with-style-transfer-for-texts/1

Python’s Raise Keyword

How to manually throw an exception in Python

Photo by Jonathan Daniels on Unsplash

Exception handling in Python can be daunting. I find it particularly difficult because as a researcher, I’m just not very good at thinking like a ‘programmer’ should. I’m thinking more about the speed of my optimisation procedures, rather than ‘is my code handling all edge cases’.

For better or worse, that’s my flaw in coding. However over time, I’ve picked up a few tricks that’ve helped in making life easier in terms of making more robust coding.

Keywords in Python make life a lot easier and I’ve previously been over Ternary Conditional Operators and the keyword Yield. Let’s cover then the keyword ‘raise’.


The keyword raise is used when the coder wants to bring forward an exception conditional on something occurring. As an example, the syntax is as follows:

if test:
raise Exception(Message)

Given this, we could use it as follows:

    #Input:
string = "Ciao"

if string=="Ciao" or string=="Howdy" or string=="Bye":
raise Exception("This word is not allowed")

#Output:
Exception: This word is not allowed

As you can see here — if the condition passes (in this case, the string does indeed say ‘Hello’, then the exception is raised. Likewise:

# Input a positive number and raise an exception 
# if input is a negative value

num = int(input("Enter a positive number: "))

if num<0:
raise Exception("Please input only positive value ")

print("num = ", num)

which will give the result:

First run:
Enter a positive number: 20
num = 20

Second run:
Enter a positive number: -10
Traceback (most recent call last):
File "/home/main.py", line 10, in <module>
raise Exception("Please input only positive value ")
Exception: Please input only positive value

So as you can see, once the user inputs a number that is not a positive integer, the specific exception that we defined has been raised. This is great because now, we have control over a specific common problem that can arise, giving us more control over our code.

The keyword raise is super easy to use. All you need to do is be clear on what you’re trying to test for and what you want to do once you’ve found it.


Python has been made with simplicity in mind and the keyword raise makes it easier for coders to be able to control their handling of exceptions.

Using this method has made my work a lot better defined and in particular, has meant that I’ve had more control over my code. This really helps when you look back on code after a couple of months.


Thanks for reading! If you have any messages, please let me know!

Keep up to date with my latest articles here!

The Difference between “is” and “==” in Python

Equality and Identity

Photo by Mr Xerty on Unsplash

Python is full of neat tips and tricks and something worth noting are the different ways to indicate equality, and how these specific two ways are different.

The == and is command both indicate some form of equality and are often used interchangeably. However, this isn’t exactly correct. To be clear, the == command checks for equality and the is operator, however, compares the identities of the objects.

In what follows, i’ll quickly explain the difference between the both, including code examples.


To understand this better, let’s look at some Python Code. First, let’s create a new list object and name it a, and then define another variable b that points to the same list object:

>>> a = [5, 5, 1]
>>> b = a

Let’s first print out these two variables to visually confirm that they look similar:

>>> a
[5, 5, 1]
>>> b
[5, 5, 1]

As the two objects look the same we’ll get the expected result when we compare them for equality using the == operator:

>>> a == b
True

This is because the == operator is looking for equality. On the other hand, that doesn’t tell us if a and b are pointing to the same object.

Photo by ETA+ on Unsplash

Now, we know they do because we set them as such earlier, but imagine a situation where we didn’t know—how can we find out?

If we simply compare both variables with the is operator, then we can confirm that both variables are in fact pointing to the same object:

>>> a is b
True

Digging deeper with examples, let’s see what happens when we make a copy of our object. We can do this by calling list() on the existing list to create a copy that we’ll name c:

>>> c = list(a)

Now again, you’ll see that the new object we just created looks identical to the list object pointed to by a and b:

>>> c
[5, 5, 1]

Now this is where it gets interesting. When we compare our copy c with the initial list a using the == operator. What answer do you expect to see?

>>> a == c
True

This is expected because the contents of the object are identical, and as such, they’re considered equivalent by Python. However, they are actually pointing to different objects, identified by using the is command:

>>> a is c
False

Being able to differentiate between identity and equality is a simple but important step in learning the complete scope of Python. These neat tips and tricks have helped me as a Data Scientist improve not only my coding skills, but also my analytics.

Thanks again for taking the time to read! Please message me if you have any questions, always happy to help!

Keep up to date with my latest work here!

What is CICD? Where is it in 2020?

Opinion

An in-depth look at the growing market

Photo by Robynne Hu on Unsplash

CICD is a development methodology which has become more important over time. In today’s software driven world, development teams are tasked with delivering applications quickly, consistently, and error-free: every single time.

While the challenges are plentiful, CI/CD is simple at its core.

For many organisations, achieving true continuous delivery is near impossible. Development teams are quickly getting more agile while the rest of the organisation struggles to adapt.

What Is CICD

CICD is an acronym for continuous integration (and) continuous delivery.

The CI portion reflects a consistent and automated way to build, package and test applications. A consistent process here allows teams to commit code changes more frequently, encouraging better collaboration and software.

On the flip side, continuous delivery automates the process of delivering an application to selected infrastructure environments. As teams develop in any number of environments (e.g. dev, test): CD makes sure that there’s an automated way to push changes through.

If you’re heard of the following companies:

  • Jenkins
  • Gitlab
  • CircleCI
  • TravisCI

Then you’re probably a little aware of CICD.

Photo by Arif Riyanto on Unsplash

The Benefits of CICD

CICD improves efficiency and deployment times across the DevOps board — having been original originally designed to increase the speed of software delivery. According to this DZone report, three-quarters of respondents (DevOps) have benefitted: not to mention the shortened development cycle time and the increase in release frequency.

A 75% success rate is very, very good.

What the DevOps Market Feels

Small to Medium sized Enterprises have begun to ramp up their investment in CICD over the last three years and are starting to compete with their larger peers.

According to DZones 2020 Study on CICD, Jenkins remains the dominant CICD platform, but GitLab has been gaining ground over the past couple of years, not to mention CircleCI.

At each stage of the CICD pipeline, the report also indicated that the majority of developers said they have automation built into the process to test code and deploy it to the next stage.

Now despite the importance of automation being built into the CICD pipeline, it’s still possible for teams to get lazy and to rely too heavily on the automation.

As your team’s responsibilities shift and new tasks arise, it is easy to automate processes too soon, just for the sake of time.

Automating poorly designed processes may save time in the short term, but in the long term, it can swell into a major bottleneck that is difficult to fix.

In doing this, teams have to be mindful to properly resolve process bottlenecks before automating and if anything does arise, they need to strip it out and fix it fully.

Moreover, developers should audit their automated protocols regularly to ensure they maintain accuracy, while also testing current processes for efficacy.

This is all part of the problem which takes time to resolve, but the effort is worth it.

Photo by Christopher Gower on Unsplash

The future in CDaas?

We’ve seen the benefits of CICD, but the DZone report highlighted that almost 45% of respondents had environments hosted on site.

Now an emerging solution for organisations is to leverage micro-services and containers to allow customer facing applications to scale.

For this, Continuous Delivery-as-a-service (CDaas) is seen to be an emerging solution of which almost 50% of of those respondents considering moving across.

I’d be interested to hear from any users of CDaas and their experiences thus far.


CICD is a growing market and the past couple of years have seen (a) more market competitors and (b) more solutions in the field. The benefits in this space are blindingly clear and it’s encouraging to see so many respondents in the space moving towards active integration of these technologies.

Trends of questions on stack [Source]

Who knows what the future holds, but it certainly looks to be in CICD.


Thanks for reading again!! Let me know if you have any questions and I’ll be happy to help.

Keep up to date with my latest articles here!

How to Deploy Streamlit on Heroku

Opinion

For Endless Possibilities in Data Science

Photo by Kevin Ku on Unsplash

In a previous post, I predicted that the popularity of Flask would really take a hit once Streamlit comes more into the mainstream. I also made the comment that I would never use Flask again.

I still stand by both of these comments.

In that time, I’ve made four different machine learning projects, all of which are being used by family and friends on a daily basis, including:

  1. A COVID Dashboard for my local friends and family that focuses in on our local area
  2. A simple application for restaurants to take bookings (help out my fathers business)
  3. A small game involving a face mask recognition system for my nephew

None of these are for financial gain, rather, they’re made exactly for what I’ve always enjoyed about artificial intelligence: they’re fun, novel and creative projects that just cool.

I can now develop, code and deploy novel applications in less than a couple of hours.

For those who don’t know, Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. It’s super useful when you want to build something small that scales, but more importantly, it’s really helpful for small pet projects.

The free tier is something that I really value and would really recommend readers to deploy more projects so that they’re free for everyone to play with. What’s the point of making cool stuff if you can’t show anyone?

Photo by Paul Green on Unsplash

In what follows, I’ll take you through the different steps required. The first or second time it may be a bit slow, but after, you’ll fly through it.

Note: the following instructions are only verbose to a degree to help you deploy simple applications, without much thought of scalability etc. My advice is targeted at individuals who want to build small applications, so if you expect to have 100m users, this is not really the tutorial for you

Firstly: make sure you have a Heroku login! Use this link and navigate through making a free tier. If you struggle here: I’ll be very disappointed.

Requirement .txt

Make sure that your terminal is in your project folder. When you launch your project into the cloud, you need to create a requirements.txt file so that the server knows what to download to run your code. The library pipreqs can autogenerate requirements (it’s pretty handy) so I’d recommend installing it as follows:

pip install pipreqs

Then once it’s downloaded, just step out of the folder, run the following command, and in the folder, you should find your requirements.txt file.

pipreqs <directory path>

It should contain libraries and their versions in the following format:

numpy==1.15.0
pandas==0.23.4
streamlit==0.60.0
xlrd==1.2.0

setup.sh and Procfile

Now, the next step is a little bit messy but bear with me. You need to set up two more things: a setup.sh file, and a Procfile.

The setup.sh file contains some commands to set the problem on the Heroku side, so create a setup.sh file (you can use the nano command) and save the following in that file (change the email in the middle of the file to your correct email)

mkdir -p ~/.streamlit/
echo "
[general]n
email = "your@domain.com"n
" > ~/.streamlit/credentials.toml
echo "
[server]n
headless = truen
enableCORS=falsen
port = $PORTn

Nice!

Now, make (and I’m cheating a little bit here), but make a procfile using the command:

nano Procfile

and from there, you want to insert the following piece of code (remember to replace [name-of-app].py to whatever your app is called. Most of mine are just app.py)

web: sh setup.sh && streamlit run [name-of-app].py

Moving the files across with Git

Heroku builds systems using Git and it’s insanely easy to get set up. Git is a version control system and runs as default on a lot of operating systems. Check if you have it, if not, install it.

Once you’re happy with your installation, you’ll need to make a git repository in your project folder. To do this, make sure you are in your project folder and run the following command:

git init

This initialises a git repository in your project folder and you should see something like the following print out:

Initialized empty Git repository in /Users/…

Nice!

The first time you do this you’ll need to click here and install the Heroku CLI.We’re using the free version of Heroku which is great but naturally has drawbacks as it doesn’t have certain desirable features configured. The features are more useful for larger projects (like SSL and scalability, also our machines tend to go to sleep if they are idle for more than 30 minutes) but hey, it’s free!

Once you’ve downloaded the Heroku CLI, run the following login command:

C:Users...> heroku login

This opens up a browser window, from which you can log in.

Once you’re in, it’s time to create your cloud instance. Run the following command

C:Users...> heroku create

and you’ll see that Heroku will create some oddly named instance (don’t worry, it’s just how it is):

Creating app… done, ⬢ true-poppy-XXXX
https://true-poppy-XXXXX.herokuapp.com/ | https://git.heroku.com/true-poppy-XXXXX.git

So Heroku created an app called ‘true-poppy’ for me. Not sure why, but I’ll take it. Now all that’s remaining is to move the code across, so in our project folder we run the following commands:

git add .
git commit -m "Enter your message here"
git push heroku master

Once it’s merged, the Heroku application will start downloading and installing everything on the server side. This takes a couple of minutes but if all is good, then you should something like:

remote: Verifying deploy… done.

Now if you run the following:

heroku ps:scale web=1

Your job is done! If you copy the url it gives you in the command line into your browser, you’ll see that you can now run your application online. You can even check it on your mobile phone, it’s perfect!


Streamlit and Heroku make a phenomenal combination. Once you’ve done the actual hard-work of creating a machine learning model that makes sense and generates sensible results, the difficult part should never be the deployment. You should want people to play with a working product. Streamlit takes care of so much of the aesthetic and Heroku takes care of the rest.

Yes, there are several drawbacks to the above methodology in that it’s limited in scope, design and scalability but I challenge you to find a quicker way of deploying a half-decent and fully functional MVP that users can interact with. Even better, I’ll even do a race!

I really recommend these pragmatic approaches because without users interacting with your product — you’ll just never know if it’s any good.

Give it a go. Surprise yourself.


Thanks for reading! If you have any messages, please let me know!

Keep up to date with my latest articles here!

Ternary Conditional Operators in Python

Mastering Efficient List and Dictionary Comprehension

Photo by Belinda Fewings on Unsplash

Python is versatile to use and its goal is to make development easier for the user. Compared to C# or Java which are notoriously cumbersome to master, Python is relatively easy to get good at. Moreover, it’s relatively easy to get pretty damn good at.

List and Dictionary Comprehension are widely used methods but something I find that’s a bit less used (especially by beginners) is the Ternary Conditional Operator. The method really streamlines your code and makes it both visually and economically better to run and deal with. Just don’t make it too complicated!

They’re pretty easy to get your head around, so let’s get into it.


Ternary Conditional Operators

The rationale behind these operators is quite simple. Beforehand, if you had an if-else type of problem, you would have to write the following pretty chunky piece of code:

if np.random.rand()>0.5:
x = a
else:
x = b

However, we’re all so familiar with this paradigm and it’s used so often, surely there must be a quicker way? With Ternary Conditional Operators, we can condense the code to the following:

x = 1 if np.random.rand()>0.5 else 0

Which, as in the first statement, computes that if the condition is True, then x=1, otherwise, x=0.

Pretty easy right?


List (and Dictionary) Comprehension with Ternary Conditional Operators

Most people who use Python are familiar with List comprehension. List comprehension is an elegant way to define and create lists based on existing lists.

Say we wanted to do a function to every item in a list (very common problem!). An un-elegant way to do it would be as follows:

new_array = []
for x in array:
new_array.append(custom_function(x))

With a list comprehension, this can be made a lot more efficient as follows:

new_array = [custom_function(x) for x in array]

But quite often, the problem may be a little bit more complicated, so for that, we could look towards the Ternary Conditional Operator.

So take the following problem where we loop through a list of items in an array and given a condition, we set a new value for each item:

new_array = [] # Define a new array
for x in random_array: # Loop over the list
if x > 0.5: # Condition
new_array.append(1) # Result One
else: # Otherwise...
new_array.append(0) # Result Two

This can then be efficiently condensed to the following:

new_array = [1 if x>0.5 else 0 for x in random_array]

See how much easier and efficient that is?

Likewise (and for added bonus points), I now provide the example code for a Dictionary Comprehension with a Ternary Conditional Operator. Here we are simply creating some form of a mapping dictionary which is 1 if an item in Z is in Y, and a 0 otherwise:

Y = ['h','e','l','l','o']
Z = ['a','e','i','o','u']
new_dict = {x:1 if x in Y else 0 for x in Z}

where new_dict now looks like the following:

{‘a’: 0, ‘e’: 1, ‘i’: 0, ‘o’: 1, ‘u’: 0}

Ta da! Super quick, easy, and clean!

You can see that the code is so much tidier as you contain the problem into a pretty elegant one liner, not to mention the reduced amount of headaches and comments you would have to have done before.

Photo by Pineapple Supply Co. on Unsplash

Coding efficiently is the whole point of Python. It’s not the fastest language (44 times slower than C#) or the most widely used (debatably JavaScript), so with this design ethos in mind, it’s worthwhile to make your code as readable as possible.

Methods like Ternary Conditional Operators and List Comprehensions make your job as a Researcher or Scientist 100x easier because you can just fly through your research.

For me, my research process usually involves a few steps and each one can be small and significant. Ensuring that your code is airtight and efficient is by far the best method in being able to focus on the bigger picture, and getting bogged down in massive amounts of code.

I would definitely encourage you to use this method!


Thanks again! Please message me if you have any questions, always happy to help!

Keep up to date with my latest work here!

What does “__name__” mean in Python?

How and why we use __name__=='__main__'

Photo by Nick Fewings on Unsplash

Python is such a popular language because it’s easy to use and it’s comprehensive. Previously, I covered what the keyword yield was useful for and with some great feedback, I have decided to tackle another key feature: __name__ .

Let’s get straight to it.

Now quite often you’ll see a file of the following format

import library
def main():
# some functionality...
return library.function()
if __name__ == ‘__main__’:
main()

At first, I struggled to understand why the file didn’t compile in one straight line but after learning about it, I began to appreciate it’s simplicity.

It comes down to a few things:

  1. You have some functions that people can use elsewhere
  2. You also want to run that file by itself at times

So it’s a bit of a quasi-library type of file. To appreciate it fully, first you should understand what values __name__ can take.

What values can __name__ take? How does it work?

Let’s say that we have a file called foo.py and the only thing in the file is the following:

print(__name__)

Now if we run the following line on the command line:

python foo.py

We will get the following result:

__main__

But on the other hand, say we have another file bar.py that contains:

import foo
print(foo.__name__)

And if you run python bar.py you will get the following output:

foo

See the difference?

If you run the file itself, then __name__ == ‘__main__’ but if you call __name__ from another file, then the name of the file is returned.

So __name__ gets its value depending on how we execute the containing script. Only the file that you are running has a __name__ set to ‘__main__’.

Photo by Keith Luke on Unsplash

So what’s the purpose of __name__?

This feature allows you to both create a script which runs itself but also lends other scripts it’s functionality, so to minimise duplicating any coding.

So when you write a script containing a library of functions which can be useful in other places, this feature comes really handy.

Moreover, it allows you to create code that’s more efficient and dynamic for multiple users. You definitely want code that can easily scale as your team grows.


Python is such a popular language because features like this are well thought out and make life for the coder significantly easier. As more languages streamline their functionality to allow the coder to do less work, hopefully, coders have to carry less of the code and the language/compiler can do more.

Given that, features like I just explained can make life a hell of a lot easier, so I encourage you to use them!


Thanks again for reading! If you have any questions, please message =]

Keep up to date with my latest work here!

Design a site like this with WordPress.com
Get started