<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ana Solaguren-Beascoa, PhD on Medium]]></title>
        <description><![CDATA[Stories by Ana Solaguren-Beascoa, PhD on Medium]]></description>
        <link>https://medium.com/@ana.solagurenbeascoa?source=rss-eb06b41f4c3e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VeLqn-xqg0stLasA</url>
            <title>Stories by Ana Solaguren-Beascoa, PhD on Medium</title>
            <link>https://medium.com/@ana.solagurenbeascoa?source=rss-eb06b41f4c3e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 02:55:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ana.solagurenbeascoa/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Social housing as a possible solution to Barcelona’s rental crisis]]></title>
            <link>https://medium.com/@ana.solagurenbeascoa/social-housing-as-a-possible-solution-to-barcelonas-rental-crisis-58fd41e146fc?source=rss-eb06b41f4c3e------2</link>
            <guid isPermaLink="false">https://medium.com/p/58fd41e146fc</guid>
            <dc:creator><![CDATA[Ana Solaguren-Beascoa, PhD]]></dc:creator>
            <pubDate>Tue, 26 Nov 2024 17:08:18 GMT</pubDate>
            <atom:updated>2024-11-27T07:28:36.627Z</atom:updated>
            <content:encoded><![CDATA[<p>On Saturday 23 November 2024, there was a large demonstration in Barcelona due to the unpayable rent prices in the city with the current salaries. We can all agree this has become a large problem in the city.</p><p>However, many of the solutions being proposed focus on short-term fixes that do not address the root of the problem, and in some cases it could even make it worse. For instance, the city recently implemented rent control laws, which has generated shift from long-term rentals to mid-term rentals [<a href="https://www.idealista.com/en/news/property-for-rent-in-spain/2024/04/26/816612-the-stock-of-permanent-rentals-in-catalonia-falls-by-13-after-the-entry-into">1</a>], as the latter are not affected by the law. Even if applied with good intentions, this has actually worsened the rental situation in the city [<a href="https://www.anderson.ucla.edu/sites/default/files/document/2024-04/CARRER_Abel%20Carrer%20Luque%202024.pdf">2</a>].</p><h3>What are people currently asking for?</h3><p>The demonstration highlighted the impact of skyrocketing rents on the city. These were the main demands [<a href="https://elpais.com/espana/catalunya/2024-11-23/miles-de-personas-exigen-en-la-manifestacion-por-la-vivienda-en-barcelona-que-bajen-los-precios-del-alquiler.html#:~:text=La%20manifestaci%C3%B3n%20apunta%20a%20cuatro,que%20no%20sean%20para%20vivir">3</a>]:</p><p><strong>1Rent Reductions</strong>: People are asking for a reduction in rents of 50%, as the current prices have systematically impoverished families over the past decade</p><p>2 <strong>Indefinite Rental Contracts</strong>: Activists want indefinite contracts to provide tenants with greater security and minimise the risks of landlords taking advantage of tenants when renewing the contract, or tenants fearing asking for basic maintenance of the properties to minimise the risk of being evicted or not getting a contract renewal.</p><p>3 <strong>Action on Vacant and Tourist Housing</strong>: Demonstrators are asking authorities to take measures against vacant properties and tourist or seasonal rentals, so these units are made available to local residents.</p><p>4 <strong>Proposed Rent Strike</strong>: People proposed a rent strike if landlords do not address tenant demands. This would involve tenants collectively refusing to pay rent as a form of protest.</p><h3>But which of these measures are actually effective?</h3><p>1 Studies have shown that <strong>rent control</strong> can have some benefits, but usually ends up generating several contrary to the original goal, such as reducing rental supply, decreasing the quality of properties in cities and generating higher rental prices on uncontrolled properties [<a href="https://www.sciencedirect.com/science/article/pii/S1051137724000020">4</a>]. Also, based on the previous experience on rent control in the city of Barcelona (and other cities such as Cambridge MA or San Francisco [<a href="https://www.brookings.edu/articles/what-does-economic-evidence-tell-us-about-the-effects-of-rent-control/">5</a>]), we could already assume that this is unlikely to help, as there are always loopholes in the system for people, companies or funds to take advantage of and speculate further on rent.</p><p>2 <strong>Indefinite leases</strong> provide tenants with the security of long-term contracts, but may make landlords think twice about renting out their property under such conditions, due to having less flexibility and more legal uncertainty [<a href="https://www.seag.es/guia-alquiler/mercado-de-alquiler/motivos-por-los-cuales-los-propietarios-optan-por-alquilar-una-vivienda-de-forma-temporal/">6</a>]. It’s therefore unclear whether the outcome of this measure could actually benefit tenants in the city. <em>As someone renting in Barcelona, I can personally relate to the desire for greater certainty</em> <em>about how long I can continue living in the place I currently call home.</em></p><p>3 <strong>Restricting short-term rentals</strong> (such as limiting the amount of Airbnbs in the city) has already shown to help return properties to the long-term rental market. However, this measure alone is unlikely to solve the crisis unless paired with more changes.</p><p>4 <strong>Rent strikes</strong> will be able to raise awareness of the rental issues in the cities to the government, but will not make any structural changes unless they lead to other longer term measures [<a href="https://www.tandfonline.com/doi/full/10.1080/08854300.2022.2170681#d1e337">7</a>].</p><p><strong><em>Is there anything more effective governments should be doing?</em></strong></p><h3>Increasing social housing</h3><p>Embarrassingly, Barcelona has a share of public rental housing as low as 1.7% [<a href="https://www.elnacional.cat/oneconomia/es/mercados/vivienda-alquiler-social-catalunya-17-debajo-9-media-europea_1041726_102.html#:~:text=El%20porcentaje%20de%20viviendas,del%20global%20europeo%20del%209%25">8</a>]. Actually, Spain has one of the lowest shares of public housing in Europe 1.13% far from the average of OECD (7.75%) [<a href="https://www.esade.edu/ecpol/es/publicaciones/el-mercado-de-alquiler-en-espana-alquiler-vacacional-de-temporada-y-largo-plazo-ante-la-regulacion/">9</a>].</p><p>Social housing can reduce the impact of rents on low income families, as they are more likely to spend less than 30% of their income on rent compared to renting privately [<a href="https://pmc.ncbi.nlm.nih.gov/articles/PMC7331941/">10</a>]. This type of housing, not only allows the government to regulate a share of the prices in the city, but also indirectly sets a threshold on the price that private owners put on their properties, as they have to offer their properties at similar prices to be competitive. But for this to happen, there has to be enough social housing.</p><p>On top of this, affordable housing has further indirect positive impact in society. For instance, it has been show to be the most effective measure to reduce child poverty [<a href="https://www.urban.org/apps/pursuing-housing-justice-interventions-impact/increasing-housing-supply">11</a>].</p><p><strong>Comparing Barcelona to Vienna</strong></p><p>Vienna is one of the leaders of public housing in Europe and across the globe (80% of the population today rents, with more than half of the population paying social-rent [<a href="https://assets.ctfassets.net/6sxvmndnpn0s/1byrK8fEQut88x9kk4w6cP/0d3452ab160befa27c832f1e1ae83f8e/International_examples_of_affordable_housing_-_Shelter_URBED_Trust.pdf">12</a>]) and has been able to keep rents affordable across income levels.</p><p>To give a quick comparison between Vienna and Barcelona in terms of income and rent [<a href="https://www.numbeo.com/cost-of-living/compare_cities.jsp?country1=Spain&amp;country2=Austria&amp;city1=Barcelona&amp;city2=Vienna&amp;tracking=getDispatchComparison">13</a>]:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dLZf6FyQvG7AnHaD7eNdXw.png" /></figure><p>We can see that even if the average monthly net salary in Vienna is 35.4% higher than in Barcelona, some rents can be up to ~23% cheaper! Hence, whilst Barcelona faces challenges with rising rents, limited rental supply, and affordability issues, Vienna demonstrates how consistent investment in social housing can create a more equitable housing market.</p><h3>To wrap up</h3><p>This post presents high-level numbers to offer a snapshot of the current state of Barcelona’s rental market, highlighting the challenges many residents face in securing affordable and stable housing. Although the goal here is not to resolve the issue, we aim to critically evaluate some of the proposals presented, and whether other solutions, such as increasing the amount of social housing in the city, could be more effective than the ones currently being proposed.</p><p>While comparisons with cities like Vienna offer valuable insights into alternative approaches, it’s important to recognise that every city has its unique context and challenges. We think<strong> </strong>individuals should inform themselves about these issues, understand the root causes and consequences, and advocate for meaningful change. Governments play a critical role in addressing these issues. However, real progress can only come if citizens actively push for policies that prioritise housing as a basic right rather than a market commodity.</p><h3>References</h3><p>[1] <a href="https://www.idealista.com/en/news/property-for-rent-in-spain/2024/04/26/816612-the-stock-of-permanent-rentals-in-catalonia-falls-by-13-after-the-entry-into">https://www.idealista.com/en/news/property-for-rent-in-spain/2024/04/26/816612-the-stock-of-permanent-rentals-in-catalonia-falls-by-13-after-the-entry-into</a></p><p>[2] <a href="https://www.anderson.ucla.edu/sites/default/files/document/2024-04/CARRER_Abel%20Carrer%20Luque%202024.pdf">The Perverse Effects of Rent Control: Evidence From a Large-scale Stringent Regulation in Catalonia, Abel et al.</a></p><p>[3] <a href="https://elpais.com/espana/catalunya/2024-11-23/miles-de-personas-exigen-en-la-manifestacion-por-la-vivienda-en-barcelona-que-bajen-los-precios-del-alquiler.html#:~:text=La%20manifestaci%C3%B3n%20apunta%20a%20cuatro,que%20no%20sean%20para%20vivir">https://elpais.com/espana/catalunya/2024-11-23/miles-de-personas-exigen-en-la-manifestacion-por-la-vivienda-en-barcelona-que-bajen-los-precios-del-alquiler.html#:~:text=La%20manifestaci%C3%B3n%20apunta%20a%20cuatro,que%20no%20sean%20para%20vivir</a>.</p><p>[4] <a href="https://www.sciencedirect.com/science/article/pii/S1051137724000020">Rent control effects through the lens of empirical research: An almost complete review of the literature, Konstantin A. Kholodilin</a></p><p>[5] <a href="https://www.brookings.edu/articles/what-does-economic-evidence-tell-us-about-the-effects-of-rent-control/">https://www.brookings.edu/articles/what-does-economic-evidence-tell-us-about-the-effects-of-rent-control/</a></p><p>[6] <a href="https://www.seag.es/guia-alquiler/mercado-de-alquiler/motivos-por-los-cuales-los-propietarios-optan-por-alquilar-una-vivienda-de-forma-temporal/">https://www.seag.es/guia-alquiler/mercado-de-alquiler/motivos-por-los-cuales-los-propietarios-optan-por-alquilar-una-vivienda-de-forma-temporal/</a></p><p>[7] <a href="https://www.tandfonline.com/doi/full/10.1080/08854300.2022.2170681#d1e468">Rent Strikes: Revolutions at Point Zero, Guzman et al.</a></p><p>[8] <a href="https://www.elnacional.cat/oneconomia/es/mercados/vivienda-alquiler-social-catalunya-17-debajo-9-media-europea_1041726_102.html#:~:text=El%20porcentaje%20de%20viviendas,del%20global%20europeo%20del%209%25">https://www.elnacional.cat/oneconomia/es/mercados/vivienda-alquiler-social-catalunya-17-debajo-9-media-europea_1041726_102.html#:~:text=El%20porcentaje%20de%20viviendas,del%20global%20europeo%20del%209%25</a>.</p><p>[9] <a href="https://www.esade.edu/ecpol/es/publicaciones/el-mercado-de-alquiler-en-espana-alquiler-vacacional-de-temporada-y-largo-plazo-ante-la-regulacion/">https://www.esade.edu/ecpol/es/publicaciones/el-mercado-de-alquiler-en-espana-alquiler-vacacional-de-temporada-y-largo-plazo-ante-la-regulacion/</a></p><p>[10] <a href="https://pmc.ncbi.nlm.nih.gov/articles/PMC7331941/">https://pmc.ncbi.nlm.nih.gov/articles/PMC7331941/</a></p><p>[11] <a href="https://www.urban.org/apps/pursuing-housing-justice-interventions-impact/increasing-housing-supply">https://www.urban.org/apps/pursuing-housing-justice-interventions-impact/increasing-housing-supply</a></p><p>[12] <a href="https://assets.ctfassets.net/6sxvmndnpn0s/1byrK8fEQut88x9kk4w6cP/0d3452ab160befa27c832f1e1ae83f8e/International_examples_of_affordable_housing_-_Shelter_URBED_Trust.pdf">https://assets.ctfassets.net/6sxvmndnpn0s/1byrK8fEQut88x9kk4w6cP/0d3452ab160befa27c832f1e1ae83f8e/International_examples_of_affordable_housing_-_Shelter_URBED_Trust.pdf</a></p><p>[13] <a href="https://www.numbeo.com/cost-of-living/compare_cities.jsp?country1=Spain&amp;country2=Austria&amp;city1=Barcelona&amp;city2=Vienna&amp;tracking=getDispatchComparison">https://www.numbeo.com/cost-of-living/compare_cities.jsp?country1=Spain&amp;country2=Austria&amp;city1=Barcelona&amp;city2=Vienna&amp;tracking=getDispatchComparison</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58fd41e146fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Attention is all you need]]></title>
            <link>https://medium.com/@ana.solagurenbeascoa/attention-is-all-you-need-18f717b347b?source=rss-eb06b41f4c3e------2</link>
            <guid isPermaLink="false">https://medium.com/p/18f717b347b</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[attention]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Ana Solaguren-Beascoa, PhD]]></dc:creator>
            <pubDate>Sat, 25 Feb 2023 18:13:38 GMT</pubDate>
            <atom:updated>2023-02-25T18:13:38.149Z</atom:updated>
            <content:encoded><![CDATA[<p>Transformers and attention mechanism have revolutionised the field of natural language processing (NLP) and brought about significant improvements in tasks such as language modelling, machine translation, and text classification. The transformer model, introduced in 2017 by Vaswani et al., replaced traditional recurrent neural network (RNN) architectures with a self-attention mechanism that allows for more efficient processing of sequential data. The attention mechanism, which allows the model to selectively focus on different parts of the input sequence, has become a fundamental building block for modern NLP systems. In this context, attention can be seen as a mechanism that enables the model to learn to weight input elements differently based on their relative importance to the current output. This introduction will provide a brief overview of transformers and attention mechanism and highlight their importance in modern NLP research.</p><h3>Introduction to Attention</h3><p>Attention mechanism is a computational technique that enables a neural network to selectively attend to certain parts of the input sequence while processing it. This mechanism was first introduced in the context of machine translation by Bahdanau et al. in 2014, and has since been widely used in various tasks such as language modelling, text classification, sentiment analysis, or computer vision, becoming widely popular after Vaswani et al. 2017 published <em>Attention is all you need</em>.</p><p>The attention mechanism is inspired by the way humans selectively attend to different parts of a sentence while processing it. Instead of processing the entire input sequence at once, attention allows the model to selectively focus on parts of the sequence that are relevant to the current task.</p><p>The key idea behind attention is to calculate a weight for each input element, which reflects its relevance to the current output. These weights are then used to compute a weighted sum of the input sequence, which is fed into the next layer of the neural network. The weights are typically calculated based on a similarity measure between the current output and each input element.</p><h4>Formal definition</h4><p>Mathematically, it’s defined as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*LQypeESs1JyKiLwCR700YQ.png" /></figure><p>where <em>Q</em>, <em>K</em>, and <em>V</em> are the query, key, and value matrices, respectively, and <em>d</em> is the dimension of the key and query. In this formulation, the query matrix <em>Q</em> represents the current state of the decoder, and the key matrix <em>K</em> and value matrix <em>V</em> represent the input sequence. The dot product between the query and key matrices measures the similarity between the current state and each input element. The softmax function is then applied to the dot product to compute a set of weights that reflect the relative importance of each input element to the current state. The scaling factor of <em>d</em> is used to prevent the dot product from becoming too large, which can cause the softmax function to saturate and make the gradients vanish during training. This is known as the “vanishing gradient” problem, which can hinder the performance of the model. Finally, the weighted sum of the input sequence is computed by multiplying the weight vector with the value matrix <em>V</em>. This produces a context vector, which represents the relevant parts of the input sequence for the current output.</p><p>We can then define our attention weights as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*-DiP1lYx5uk_Q7dd84jnCw.png" /></figure><p>Now, we can rewriting <em>Q</em>, <em>K</em>, and <em>V </em>in terms of our input <em>X</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/175/1*MgBV0ClaSU-v-YGsPCK34Q.png" /></figure><p>where the subindexes <em>i</em> and <em>j</em> indicate that this would be the attention weight of vector <em>i</em> over <em>j</em>.</p><p>Using the definitions above, the attention weights can be redefined as (I will be removing the summatories for simplicity):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*H3C-TaAQuUItEvFc7Gqg8Q.png" /></figure><p>where <em>A</em> is the matrix which will contain the information of how relevant is <em>Xᵢ</em> is to <em>Xⱼ</em>.</p><p>Finally, these attention weights are then multiplied by a linear transformation of <em>X</em>, which is basically <em>V</em>, so that the output will be a weighted linear transformation of <em>X</em>, which takes into account how relevant each feature of <em>X</em> is to each other.</p><h4><strong>Example: </strong>“The big dog”</h4><p>Let’s put an example to make it a bit less formal. Imagine we have the following input:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FmVJqwixQ3sMPNBFMoGitQ.png" /></figure><p>Once we apply the normalisation and softmax, we will get the matrix which indicates the relevance between the different words:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gV_0GIKMCXDtUH9kCQAxJA.png" /></figure><p>In this example, our attention matrix <em>A</em>, indicates that <strong><em>dog</em></strong> has a high relevance on <strong><em>big</em></strong> (and viceversa), whereas the has very little impact on <strong><em>the</em></strong> other two. We can then embed each word using vector <em>V: x’ᵢ = V xᵢ</em>. When multiplying this vector by the attention weight, we will get our final vector, containing the information of the relevance of the words in the sentence between themselves.</p><h4>Conclusion</h4><p>Attention mechanism has proven to be a powerful tool for improving the performance of various deep learning models, especially in the fields of natural language processing and computer vision. By allowing the model to selectively focus on the most relevant information, attention mechanisms have enabled models to achieve state-of-the-art results in tasks such as language translation, image captioning, and speech recognition. Furthermore, attention mechanisms have also opened up new avenues of research, such as the exploration of different types of attention and the development of more efficient and effective attention-based architectures. As the field of deep learning continues to evolve, attention mechanism is sure to remain an important area of study and innovation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18f717b347b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Algebra behind PCA]]></title>
            <link>https://medium.com/data-science/algebra-behind-pca-7d801226f4e6?source=rss-eb06b41f4c3e------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d801226f4e6</guid>
            <dc:creator><![CDATA[Ana Solaguren-Beascoa, PhD]]></dc:creator>
            <pubDate>Fri, 24 Apr 2020 17:01:58 GMT</pubDate>
            <atom:updated>2020-04-24T20:15:43.940Z</atom:updated>
            <content:encoded><![CDATA[<h4>A summary of the algebra which makes PCA great!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IW0IjV_e9VOzOMEg" /><figcaption>Photo by <a href="https://unsplash.com/@roman_lazygeek?utm_source=medium&amp;utm_medium=referral">Roman Mager</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In some of the interviews I have performed, we end up talking about dimensionality reduction techniques. PCA is definitely the most popular one presented by the candidates. I personally don’t like digging too deep into areas which might not be relevant for the role, as I might not even know the answer myself! But I like to ask if they know how PCA works. Although I don’t expect candidates to know the exact algebra, I like to see if they have some intuition, if they just don’t know (which is completely fine with me) or if they try to pretend they know, which is where my brain alerts me with an amber flag.</p><p>Some candidates start talking about how <em>PCA uses the eigenvectors to calculate the final features</em>. And here is where I like asking, <strong>what is an eigenvector and what does PCA calculate the eigenvector of</strong>? And here is where most candidates start shaking. Again, even if this is not something you MUST know to become a data scientist, I personally prefer to get an answer such as <em>I am not sure</em> than something the candidate doesn’t know about.</p><p>In this post, I hope to help people gain some basic intuition on how PCA actually works and feel confident to answer it in an interview if you are ever asked for it.</p><h3>Eigendecomposition: eigenvalues &amp; eigenvectors</h3><p>Eigen comes from German which means <em>own</em>, so one could translate eigenvectors and eigenvalues as own-vectors and own-values of a matrix. But what does this actually mean?</p><p>Let’s take a step backwards and formalise the definition of eigenstates. We define an eigevector<strong> <em>v</em></strong> of a matrix <strong><em>A</em></strong> with eigenvalue <strong><em>a</em></strong> as:</p><p><strong><em>A · v = a · v</em></strong></p><p>In words, the implementation of a linear transformation (a transformation which preserves sum and multiplication) on vector <strong><em>v,</em></strong> which changes <strong><em>v</em></strong> by a scalar <strong><em>a</em></strong>. Hence, applying the dot product of matrix <strong><em>A</em></strong> on <strong><em>v</em></strong> will just change the length of the vector but not it’s direction, if and only if <strong><em>v</em></strong> is an eigenvector of <strong><em>A</em></strong>! Note that <strong><em>A</em></strong> is a <em>n x n</em> matrix, it will have <em>n</em> eigenvectors and<em> n</em> eigenvalues when doing the eigendecomposition.</p><p><em>Just to clarify, not all dot products between a matrix and a vector satisfy this condition.</em></p><p><strong><em>A </em>must be a square matrix (same number of rows than columns) in order for it to be eigendecomposible. On top of this, in order for the eigenvectors to be well defined, all eigenvalues must be different from each other.</strong></p><p>Finally, it is also important to note that all eigenvectors are perpendicular to each other and each one has it’s own eigenvalue assigned to it.</p><h3><strong>PCA’s Algebra</strong></h3><p>To understand how the algebra of PCA works, let’s first think what the goal of PCA is. With PCA we want to reduce the number of features from our full dataset and keep as much information as possible from the original data and remove the highest correlations.</p><p>But wait! How do we measure these correlations to prioritise the direction? Well, we do have a matrix that keeps track of the correlation between the different features, which is the <a href="https://en.wikipedia.org/wiki/Covariance_matrix"><strong>covariance matrix</strong></a>, so let’s use that!</p><p><em>A quick note on this: there are other matrices that can be used for PCA, but the covariance matrix is one of the most popular and intuitive one.</em></p><p>So one can do the eigendecomposition of the covariance matrix to see which are the eigenvectors with higher eigenvalues. The eigenvector corresponding to the highest eigenvalue (<em>x1</em>) will be the one which indicates the highest variance in the data, hence the direction with least correlation between the features. The second one (<em>x2</em>) will be the second-highest variance direction perpendicular to <em>x1</em> and so on.</p><p>In a nutshell, once one has calculated the covariance matrix, if one wants to reduce from <em>n</em> dimesions to <em>m</em> dimensions, it’s as simple as:</p><ul><li><strong>Step 1</strong>: Calculate the eigenvectors of the covariance matrix.</li><li><strong>Step 2</strong>: Set the top <em>m</em> eigenvectors with highest eigenvalues. This will be your rotation matrix <strong><em>W, </em></strong>where each row in <strong><em>W </em></strong>corresponds to the sorted top <em>m</em> eigenvectors. <strong><em>W</em></strong> is an <em>m</em>x<em>n</em> matrix.</li><li><strong>Step 3</strong>: Given a data point <strong><em>x, </em></strong>one can find the projection in the <em>m</em>-dimensional space by performing the linear transformation <strong><em>x’ = W·x. </em></strong>Given that<strong><em> W</em></strong> is an <em>m</em>x<em>n</em> matrix, <strong><em>x’</em></strong> will be an <em>m</em>-dimensional vector, the data point rotated to the new space!</li></ul><h4>Example</h4><p>Let’s see the following example to have a clearer picture of what’s going on:</p><p>Imagine we have two features: the size of a flat in a given city and the price for that given flat. Our task is to reduce our data to a 1D space. The given dataset is shown in the left figure below.</p><p>The steps one should take are (<em>again, there are multiple ways to do this, we are just presenting one as an example!</em>):</p><ul><li><strong>Step 1: </strong>Define the covariance matrix based on your data and calculate the eigenvectors of your covariance matrix.</li><li><strong>Step 2: </strong>The eigenvectors are shown in green in the middle picture. The longest eigenvector is the one which corresponds to the highest eigenvalue (eigenvectors are usually normalised to 1, this is just for visualisation purposes). It is obvious that it is pointing in the direction of highest variance if the data. This is the direction one would like to keep, as it contains the most information.</li><li><strong>Step 3: </strong>Once the eigenvector corresponding to the highest eigenvalue is determined, one should project the data points on the line defined by the latter. This is shown in the right picture.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mNkXuZONUa3BbEDuJJ1OEA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Yb4pmgLRKDpqAzXyonfDcw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*60Q3qNaUTYPRnegq4FkJ3Q.jpeg" /><figcaption>Image 1: (From left to right) a) Original dataset b) Original dataset together with the covariance matrix eigenvectors c) Projection of the original dataset on the line defined by the eigenvector corresponding to the highest eigenvalue.</figcaption></figure><p>I hope this gave you a basic idea on what’s happening when using PCA!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d801226f4e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/algebra-behind-pca-7d801226f4e6">Algebra behind PCA</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Active Learning in Machine Learning]]></title>
            <link>https://medium.com/data-science/active-learning-in-machine-learning-525e61be16e5?source=rss-eb06b41f4c3e------2</link>
            <guid isPermaLink="false">https://medium.com/p/525e61be16e5</guid>
            <dc:creator><![CDATA[Ana Solaguren-Beascoa, PhD]]></dc:creator>
            <pubDate>Fri, 03 Apr 2020 10:30:36 GMT</pubDate>
            <atom:updated>2020-04-04T10:20:19.492Z</atom:updated>
            <content:encoded><![CDATA[<h4>A brief introduction to the implementation of active learning</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wvT88RaaNLyiCLt8" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Most supervised machine learning models require large amounts of data to be trained with good results. And even if this statement sounds naive, most companies struggle to provide their data scientists this data, in particular <em>labelled</em> data. The latter is key to train any supervised model and can become the main bottleneck for any data team.</p><p>In most cases, data scientists are provided with a big, unlabelled data sets and are asked to train well-performing models with them. Generally, the amount of data is too large to manually label it, and it becomes quite challenging for data teams to train good supervised models with that data.</p><h3>Active learning: Motivation</h3><p>Active learning is the name used for the process of prioritising the data which needs to be labelled in order to have the highest impact to training a supervised model. Active learning can be used in situations where the amount of data is too large to be labelled and some priority needs to be made to label the data in a smart way.</p><p><em>But, why don’t we just chose a random subset of data to manually label them?</em></p><p>Let’s look at a very simple example to motivate the discussion. Assume we have millions of data points which need to be classified based on two features. The actual solution is shown in the following plot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*Z_5GyCdFfcz_oVFnUuYczg.png" /><figcaption>Model prediction if all data points were labelled</figcaption></figure><p>As one can see, both classes (red and purple) can quite nicely be separated by a vertical blue line crossing at 0. The problem is that none of the data points are labelled, so the data is given to us as in the following plot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*fmnhkOPVsXNIUiroRg2CfQ.png" /><figcaption>Unlabelled data</figcaption></figure><p>Unfortunately, we don’t have enough time to label all of the data and we randomly chose a subset of the data to label and train a binary classification model on it. The result is not great, as the model prediction deviates quite a lot from the optimal boundary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*2bpj99Fppl2mqLb7Jb98XA.png" /><figcaption>Model trained on a random subset of labelled data points</figcaption></figure><p>This is where active learning can be used to optimise the data points chosen for labelling and training a model based on them. The following plot shows an example of training a binary classification model after choosing the training of the model based on data points labelled after implementing active learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*8eOKeWFNg29ruakj9b1Nzg.png" /><figcaption>Model trained on a subset of data points chosen ho to be labelled using active learning</figcaption></figure><p>Making a smart choice of which data points to prioritise when labelling can save data science teams large amounts of time, computation and headaches!</p><h3>Active learning strategy</h3><h4>Steps for active learning</h4><p>There are multiple approaches studied in the literature on how to prioritise data points when labelling and how to iterate over the approach. We will nevertheless only present the most common and straightforward methods.</p><p>The steps to use active learning on an unlabelled data set are:</p><ol><li>The first thing which needs to happen is that a very small subsample of this data needs to be manually labelled.</li><li>Once there is a small amount of labelled data, the model needs to be trained on it. The model is of course not going to be great but will help us get some insight on which areas of the parameter space need to be labelled first to improve it.</li><li>After the model is trained, the model is used to predict the class of each remaining unlabelled data point.</li><li>A score is chosen on each unlabelled data point based on the prediction of the model. In the next subsection we will present some of the possible scores most commonly used.</li><li>Once the best approach has been chosen to prioritise the labelling, this process can be iteratively repeated: a new model can be trained on a new labelled data set, which has been labelled based on the priority score. Once the new model has been trained on the subset of data, the unlabelled data points can be ran through the model to update the prioritisation scores to continue labelling. In this way, one can keep optimising the labelling strategy as the models become better and better.</li></ol><h4>Prioritisation scores</h4><p>There are several approaches to assign a priority score to each data point. Below we describe the three basic ones.</p><p><strong>Least confidence:</strong></p><p>This is probably the most simple method. It takes the highest probability for each data point’s prediction, and sorts them from smaller to larger. The actual expression to prioritise using least confidence would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*RJ0wYr0LXxpxezaUc_z75A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*7taQkELyPNhYFH6-JgMsGA.png" /></figure><p>Let’s use an example to see how this would work. Assume we have the following data with three possible classes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dUxgoL1aVNSyO1cP7C9riQ.png" /><figcaption>Table 1: Example of probability predictions of a model on three different classes for four different data points.</figcaption></figure><p>In this case, the algorithm would first chose the maximum probability for each data point, hence:</p><ul><li>X1: 0.9</li><li>X2: 0.87</li><li>X3:0.5</li><li>X4:0.99.</li></ul><p>The second step is to sort the data based on this maximum probability (from smaller to bigger), hence X3, X2, X1 and X4.</p><p><strong>Margin sampling:</strong></p><p>This method takes into account the difference between the highest probability and the second highest probability. Formally, the expression to prioritise would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*c-Qqr2TEzaaA-zGH01JalA.png" /></figure><p>The data points with the lower margin sampling score would be the ones labelled the first; these are the data points the model is least certain about between the most probably and the next-to-most probable class.</p><p>Following the example of Table 1, the corresponding scores for each data point are:</p><ul><li>X1: 0.9–0.07 = 0.83</li><li>X2: 0.87–0.1 = 0.86</li><li>X3: 0.5–0.3 = 0.2</li><li>X4: 0.99–0.01 = 0.98</li></ul><p>Hence the data points would be shown to label as follows: X3, X1, X2 and X4. As one can see the priority in this case is slightly different to the least confident one.</p><p><strong>Entropy:</strong></p><p>Finally, the last scoring function that we are gonna present here is the entropy score. Entropy is a concept that comes from thermodynamics; in a simple way, it can be understood as a measure of disorder in a system, for instance a gas in a closed box. The higher the entropy the more disorder there is, whereas if the entropy is low, it means that the gas might be mainly in one particular area such as a corner of the box (maybe when the experiment started, before expanding across the box).</p><p>This concept can be reused to measure the certainty of a model. If a model is highly certain about a class for a given data point, it will probably have a high certainty for a particular class, whereas all the other classes will have low probability. Isn’t this very similar to having a gas in the corner of a box? In this case we have most of the probability assigned to a particular class. In the case of high entropy it would mean that the model distributes equally the probability for all classes as it is not certain at all which class that data point belongs to, similarly to having the gas distributed equally in all parts of the box. It is therefore straightforward to prioritise data points with higher entropy to the ones with lower entropy.</p><p>Formally, we can define the entropy score prioritisation as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*sUuF5qqrW0CpArzejhNTNA.png" /></figure><p>If we apply the entropy score to the example in Table 1:</p><ul><li>X1:<em> -0.9*log(0.9)-0.07*log(0.07)-0.03*log(0.03) </em>= 0.386</li><li>X2: <em>-0.87*log(0.87)-0.03*log(0.03)-0.1*log(0.1) </em>= 0.457</li><li>X3: <em>-0.2*log(0.2)-0.5*log(0.5)-0.3*log(0.3)</em> =1.03</li><li>X4: <em>-0*log(0)-0.01*log(0.01)-0.99*log(0.99)</em> = 0.056</li></ul><p><em>Note that for X4, 0 should be changed for a small epsilon (e.g. 0.00001) for numerical stability.</em></p><p>In this case the data points should be shown in the following order: X3, X2, X1 and X4, which coincides with the order of the least confident scoring method!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=525e61be16e5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/active-learning-in-machine-learning-525e61be16e5">Active Learning in Machine Learning</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning GPU Installation on Ubuntu 18.4]]></title>
            <link>https://medium.com/data-science/deep-learning-gpu-installation-on-ubuntu-18-4-9b12230a1d31?source=rss-eb06b41f4c3e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b12230a1d31</guid>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[nvidia]]></category>
            <category><![CDATA[gpu]]></category>
            <category><![CDATA[installation]]></category>
            <dc:creator><![CDATA[Ana Solaguren-Beascoa, PhD]]></dc:creator>
            <pubDate>Sat, 16 Nov 2019 17:03:20 GMT</pubDate>
            <atom:updated>2020-04-03T14:21:04.459Z</atom:updated>
            <content:encoded><![CDATA[<p>After several times of installing with drivers, CUDA etc… to make my deep learning libraries talk to my GPU, I have decided to write the installation steps that work for Ubuntu18.04.</p><h3>1. NVIDIA Driver Installation</h3><h4><strong>Step 1: Ensure your GPU is an NVIDIA GPU</strong></h4><p>The first thing one needs to do, is ensure that the GPU is an NVIDIA GPU with the following command:</p><pre>$ ubuntu-drivers devices</pre><h4><strong>Step 2: Remove NVIDIA drivers</strong></h4><p>The first thing one needs to do is run the following commands to ensure any preinstalled driver is purged:</p><pre>$ sudo apt-get autoremove</pre><pre>$ sudo apt-get remove nvidia*</pre><pre>$ sudo apt-get purge cuda*</pre><h4>Step 3: Disable Nouveau NVIDIA driver</h4><p>This step is sometimes not necessary after the previous one, but I strongly recommend doing it anyway (doing it doesn’t hurt!).</p><p>First, you need to open the <em>blacklist-nouveau.conf </em>file (I use gedit, but feel free to use any other type of editor). The command I use is the following:</p><pre>$ sudo gedit /etc/modprobe.d/blacklist-nouveau.conf</pre><p>Now, add the following lines to the file.</p><pre>blacklist nouveau<br>blacklist lbm-nouveau<br>options nouveau modeset=0<br>alias nouveau off<br>alias lbm-nouveau off</pre><p>Save and close the file.</p><p>Disable the Kernel nouveau:</p><pre>$ echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf</pre><h4>Step 4: Add PPA drivers respository</h4><p>To add the ppa:graphics-drivers/ppa repository into your system type the following command:</p><pre>$ sudo add-apt-repository ppa:graphics-drivers/ppa</pre><h4>Step 5: Install NVIDIA Drivers</h4><p>In the past, I used to download the drivers from the NVIDIA download site (<a href="https://www.nvidia.co.uk/Download/index.aspx?lang=en-uk">https://www.nvidia.co.uk/Download/index.aspx?lang=en-uk</a>).</p><p>The simplest and most reliable way to do it (this has been the direct recommendation from NVIDIA on one of my posts on the NVIDIA forum) is to install it directly from the terminal. The command to install the drivers would be (ensure you install the right driver!):</p><pre>$ sudo apt install nvidia-driver-435</pre><p>The other option is to install it directly from the Software &amp; Updates settings in Ubuntu:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*xHmEvArInk1O1aji6AlHlQ.png" /></figure><p>Now, chose the correct driver (this is the tricky bit, you might have to come back and test a new driver if this step hasn’t worked). Click on <em>Apply Changes</em>, and you are good to go!</p><h4>Step 6: Reboot your computer</h4><p>Don’t forget this step!</p><h4>Step 7: Check that the drivers are working</h4><p>You will know that the installation has been successful by typing the following command:</p><pre>$ nvidia-smi</pre><p>You should see something similar to</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*9QAMH_7PXW9j91U5eXr2aw.png" /></figure><p>If you get an NVIDIA error here, try all the steps from the beginning, but choosing a new driver this time!</p><h3>2. CUDA Installation</h3><p>When installing CUDA, I usually follow the CUDA installation guide, which is very complete (<a href="https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html">https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html</a>).</p><h4>Step 1: Preinstallation checks</h4><ul><li>First check that your GPU is can deal with CUDA:</li></ul><pre>$ lspci | grep -i nvidia</pre><ul><li>Check you have a supported Linux version:</li></ul><pre>$ uname -m &amp;&amp; cat /etc/*release</pre><p>This should return something like</p><pre>x86_64<br>DISTRIB_ID=Ubuntu<br>DISTRIB_RELEASE=18.04<br>DISTRIB_CODENAME=bionic<br>DISTRIB_DESCRIPTION=”Ubuntu 18.04.3 LTS”<br>NAME=”Ubuntu”<br>VERSION=”18.04.3 LTS (Bionic Beaver)”<br>ID=ubuntu<br>ID_LIKE=debian<br>PRETTY_NAME=”Ubuntu 18.04.3 LTS”<br>VERSION_ID=”18.04&quot;<br>HOME_URL=”<a href="https://www.ubuntu.com/">https://www.ubuntu.com/</a>&quot;<br>SUPPORT_URL=”<a href="https://help.ubuntu.com/">https://help.ubuntu.com/</a>&quot;<br>BUG_REPORT_URL=”<a href="https://bugs.launchpad.net/ubuntu/">https://bugs.launchpad.net/ubuntu/</a>&quot;<br>PRIVACY_POLICY_URL=”<a href="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy">https://www.ubuntu.com/legal/terms-and-policies/privacy-policy</a>&quot;<br>VERSION_CODENAME=bionic<br>UBUNTU_CODENAME=bionic</pre><ul><li>Check gcc is installed</li></ul><pre>$ gcc --version</pre><ul><li>Check your kernel headers are compatible with CUDA (you can find the compatibility table on the CUDA installation guide):</li></ul><pre>$ uname -r</pre><ul><li>Install the kernel headers and packages with the following command:</li></ul><pre>$ sudo apt-get install linux-headers-$(uname -r)</pre><h4>Step 2: Download CUDA</h4><p>You can download the CUDA toolkit through the following link:</p><p><a href="https://developer.nvidia.com/cuda-downloads">CUDA Toolkit 12.1 Downloads</a></p><p>For Ubuntu 18.04 you need to download the following</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jDsLGGqJ0H7OdbbJlp549Q.png" /></figure><p>Now, you just need to wait, this download might take a while!</p><h4>Step 3: Install CUDA</h4><p>The steps to install CUDA are pretty simple. Just type the following commands on your terminal:</p><pre>$ wget <a href="http://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run">http://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run</a><br>$ sudo sh cuda_10.1.243_418.87.00_linux.run</pre><p>You should get the following screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*X61N5mkxeVN9sxRn841NZw.png" /></figure><p><strong>Make sure this installation does not install new drivers!!!!!</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*OdfMrSqGyFi7ISHQVib3dA.png" /><figcaption>Unselect Driver when installing CUDA</figcaption></figure><p>If the installation has been successful, you should get the following screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*JmBs54XWAnUz7DzQxlRXaQ.png" /></figure><h3>3. Install cuDNN</h3><p>These are the last couple of steps before you can start using your GPU for deep learning!</p><h4>Step 1: Download cuDNN</h4><p>The following link takes you to the cuDNN download site:</p><p><a href="https://developer.nvidia.com/rdp/form/cudnn-download-survey">https://developer.nvidia.com/rdp/form/cudnn-download-survey</a></p><p>You have to login first before you can actually download anything. Once you are in, download:</p><ul><li>The cuDNN Library</li><li>The cuDNN Runtime Library (Deb)</li><li>The cuDNN Developer Library (Deb)</li><li>The cuDNN Code Samples (Deb)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*dSAeh9JRpXbfgHqOCNx6qw.png" /></figure><h4>Step 2: Install cuDNN</h4><p>These steps mainly follow the instructions in the cuDNN installation site <a href="https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html">https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html</a></p><ul><li>cd into you <em>Download</em> folder (or wherever the downloaded files are)</li><li>Unpack the archive with the following command:</li></ul><pre>$ tar -zxvf cudnn-10.1-linux-x64-v7.6.5.32.tgz</pre><ul><li>Copy the files into the CUDA Toolkit directory and change the permisions</li></ul><pre>$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include<br>$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64<br>$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*</pre><ul><li>Install the three libraries with the following commands</li></ul><pre>$ sudo dpkg -i libcudnn7_7.6.5.32–1+cuda10.1_amd64.deb<br>$ sudo dpkg -i libcudnn7-dev_7.6.5.32-1+cuda10.1_amd64.deb<br>$ sudo dpkg -i libcudnn7-doc_7.6.5.32-1+cuda10.1_amd64.deb</pre><h3>4. Check that everything is working!</h3><p>In order to do so, cuDNN have created a sample code which will run some computation on the MNIST dataset using your GPU. To do so, you need to follow these steps:</p><pre>$ cp -r /usr/src/cudnn_samples_v7/ $HOME<br>$ cd  $HOME/cudnn_samples_v7/mnistCUDNN<br>$ make clean &amp;&amp; make<br>$ ./mnistCUDNN</pre><p>If everything worked fine, you should see something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/899/1*RgzwmVPsKmlmB7izk2wk-w.png" /></figure><p>And now you are good to go with your GPU!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b12230a1d31" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/deep-learning-gpu-installation-on-ubuntu-18-4-9b12230a1d31">Deep Learning GPU Installation on Ubuntu 18.4</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>