{"data":{"allArticles":{"edges":[{"node":{"excerpt":"","id":"364acb1e-105f-5dc3-b49b-265e7ef8c5a9","fields":{"slug":"/blog/how-to-start-a-neural-network-with-javascript-in-5-minutes/"},"frontmatter":{"title":"How to start a neural network with JavaScript in 5 minutes","order":null,"content":[{"body":"The question is: can a JavaScript developer, aware of the pace of progress and the complexity of this technology, start creating simple or even advanced **neural networks**? Especially if he or she has never had anything to do with them before, specializing instead in web applications?\n\nThe answer is **YES**. In this article I’m going to show you how to create a **simple network using JavaScript in just 5 minutes**.\n\n## A brief theoretical introduction\n\nA little bit of theory will be of use before you begin your adventure with neural networks – this will help you not only to create a working network but also to be familiar with the way it functions.\n\nThe neuron is both the most important and the smallest element in the network structure. It’s a simple input/output function, processing multiple input signals into a single output signal. The way the signal is converted is determined by the activation function (e.g. step, sigmoid, or linear), the signal weight, and the bias.\n\nThe [bias](https://www.quora.com/What-is-bias-in-artificial-neural-network) is a signal whose weight always equals one, which allows the adjustment of the activation function (moving to the left or right).\n\nGraphically, an individual neuron can be presented this way:\n\n![Neural Network 1](/img/neural-network-schema.jpg)\n\nsource: http://www.ece.utep.edu\n\nTo better illustrate the way a neuron functions, I’ll try to determine its output on the example of some example data. Our example neuron has three input signals – one of them is the bias (value: 0.5), the weights of the others are 0.4 and 0.5 respectively. I’m going to use the unit step (for values higher than 0, the value of the signal is 1) as an activation function.\n\nIf both of the inputs (leaving out the bias) have a signal whose value is 1, the output of the neuron can be calculated in the following manner:\n\n_y = f(0.5\\*1 + 0.4\\*1 – 0.5) = f (0.4) = 1_\n\nNow that I have the basic network unit I can take the next step – that is, create a layer from it and adjust the network to a particular problem. A layer is a sequence of connected neurons which can transmit signals from one to another There are three kinds of network layers: input, output, and hidden. The input and output layers only have one level each, while the hidden layer can have multiple levels.\n\nGraphically, our example network can be presented this way:\n\n![Neural Network 2](/img/neural-network-chart.png)\n\nsource: https://commons.wikimedia.org\n\nThese are the basic elements of a simple neural network. In this article, I want to go even further and show you a more interesting and practical example. To do that, the notion of the [Hopfield network](http://mlexplore.org/2017/03/12/hopfield-networks-in-go/) must be introduced. It can be illustrated this way:\n\n![Hopfield Network](/img/the-hopfield-network.jpg)\n\nsource: http://galaxy.agh.edu.pl\n\nThese are the characteristic features of the Hopfield network:\n\n* Feedback (any time you calculate the value of the neuron, you must take the value calculated in the previous cycle into account),\n* The neuron value calculated in one iteration is transmitted to all other neurons,\n* If the initial neuron weights are zeroed out, the network acts as associative memory,\n* There is only one layer of neurons.\n\nIn practice, the network will learn the patterns based on the provided data, adjusting the neuron weights by means of adequate rules. If you’d like to know more on this subject, have a look at this article.\n\nExamples of how a Hopfield network can be used include pattern recognition, classification, and process optimization.\n\n## Implementing a simple neural network\n\nNow that you know the basics, it’s time to get down to work. I’d like to show how easy it is to get from theory to practice. The test project consists in **creating a simple dictionary** containing three words and training the network so that it could recognize the words even if they contain small deformations. To do it, create a .js script and run it with Node. Node is a tool to start JavaScript on the server.\n\nTo begin, let’s create a catalogue for the project – in my case it’s called “Hopfield network”. Open the project in the terminal and initiate it through the command:\n\n_– npm init_\n\nThe command initiates the project by creating the **package.json file** and saving the project details there. To start the project and download packages, you can also use yarn.\n\nNext, download the latest version of **SynapticJS**. This is a library that lets you design, train, and use neural networks; use the command:\n\n_\\-npm install synaptic –save_\n\nNow everything is ready, you can start working with the network. Create the index.js to store the logic of the project.\n\nAfter creating the project, add the library and create some constants:\n\n```\n// Add synaptic library\nconst synaptic = require('synaptic');\n\n// Create dictionary as our source of knowledge\nconst dictionary = [\n'cat',\n'bob',\n'ice',\n//...\n];\n\n// Define binary size for one letter\nconst binarySize = 7;\n\n// Define number of neurons in our network. It's word length multiplied by binarySize\nconst neuronsCount = 21;\n```\n\n**In the code above:\n**\n\n* The dependency of the synapticJS library has been created,\n* A simple dictionary of words used to train the network has been created; in this example, it contains only three words – in practice, the set training neural networks would be much larger,\n* The binary size for one letter has been defined – this refers to the number of binary characters included in one letter after transformation into a binary string, resulting from the size of the binary string according to the ASCII character table (max. 128 characters in the basic version),\n* The number of neurons in the network has been established – it can be defined as the product of the number of binary characters in one letter and the number of letters; for the purposes of this example, let’s assume that the learning set can only contain three-letter words.\n* Then, two auxiliary functions are created to help translate the words into a binary string, which is the form understood by the neural network, and to transform the binary string into a single word.\n\nThen, two auxiliary functions are created to help translate the words into a binary string, which is the form understood by the neural network, and to transform the binary string into a single word.\n\n```\n// Transform word to binary string\nconst wordToBinary = function (word) {\nlet binaryWord = '';\n\nfor (let i = 0; i < word.length; i++) {\nbinaryWord += word.charCodeAt(i).toString(2);\n}\n\nreturn binaryWord;\n};\n\n// Transform binary string to word\nconst binaryToWord = function (binaryWord) {\nconst wordLength = binaryWord.length / binarySize;\nlet word = '';\n\nfor (let i = 0; i < wordLength; i++) {\nword += String.fromCharCode(parseInt(binaryWord.slice(i * binarySize, i * binarySize + binarySize).join(''), 2));\n}\n\nreturn word;\n};\n```\n\nNow you can move on to implementing the **neural network**:\n\n```\n// Create Hopfield Neural Network\nconst hopfieldNetwork = new synaptic.Architect.Hopfield(neuronsCount);\n\n// Create dictionary of binary words\nconst binaryDicitonary = dictionary.map(word => wordToBinary(word));\n\n// Learn our network new patterns\nhopfieldNetwork.learn(binaryDicitonary);\n\n// Let's check if it works\nconsole.log(binaryToWord(hopfieldNetwork.feed(wordToBinary('cob'))));\n```\n\nAt the beginning, create the network using the architect provided by the SynapticJS library – you could design the structure of the network yourself, but that can take a lot of time. Next, transform the dictionary into a binary string and train the network. In the last line, the result of the neural network prediction for the word ‘cob’ is checked.\n\nNow the app is ready to open. This can be done by this call:\n\n_node index.js\n_\n\nYou can see that the terminal shows the word ‘**bob**’. Why is that? The neural network knows three patterns defined in the dictionary. When the word ‘cob’ was entered in the network, it iterated until it encountered one of the patterns it remembered (it happens sometimes that the network recognizes a [spurious](https://www.quora.com/What-are-spurious-states-in-Hopfield-networks) state). The designated pattern was then transformed from the binary form – readable for the network – into a form understandable for you: a word.\n\nIf you want to become more familiar with the algorithms of learning, teaching, and finding patterns, **take a look** [here](https://www.eriksmistad.no/hopfield-network/).\n\n## Summary\n\nJust like I promised at the beginning, **you need no more than 5 minutes** to create a functioning neural network that can guess words with small typos. It must be mentioned, however, that the Hopfield network has some disadvantages and is not able to guarantee the recognition of a proper pattern each time. This is caused by the fact that sometimes an erroneous local minimum is found which reflects a false pattern.\n\nSo it turns out that the basic issues related to neural networks are not as overwhelming as you might expect. The Hopfield network is just one of many that can be used nowadays. In practice of course, more complicated structures are employed, sometimes combining more than one algorithm.\n\nLast but not least, I’d like to encourage you to take a closer look at the [SynapticJS](https://github.com/cazala/synaptic) library as well as at other libraries that enable you to create and use neural networks by means of JavaScript:\n\n* [deeplearnJS](https://deeplearnjs.org/)\n* [ConvNetJS](https://cs.stanford.edu/people/karpathy/convnetjs/)\n* [brainJS](https://github.com/harthur/brain)"}],"job":null,"photo":null,"cover":"/img/barbara-strak.jpg","lead":"Software development is advancing incredibly fast these days and neural networks are becoming increasingly important. You may not even be aware of the role they already play in everyday life; in [houses](https://www.huffingtonpost.co.uk/kris-bondi/the-house-that-learns-how_b_17987040.html), [cars](https://futurism.com/teslas-neural-network-is-receiving-a-massive-amount-of-data-from-cars/), and workplaces.","templateKey":"article-page","settings":{"date":"June 05, 2018","slug":"neural-network-with-javascript","type":"blog","category":"Tech"},"contributor":"Kamil Mikosz","box":null}}},{"node":{"excerpt":"","id":"3ddb8338-24b7-5e73-8490-339437d999a7","fields":{"slug":"/blog/building-digital-products-based-on-machine-learning-the-cost-perspective/"},"frontmatter":{"title":"Building digital products based on machine learning - the cost perspective","order":null,"content":[{"body":"Any company wanting to cash on the machine learning wave will have to act fast. But to do so, specific know-how is needed in the shape of **IT professionals** capable of developing and implementing solutions based on machine learning. And this is where the problems start because those specialists are not cheap. \n\n## The need for AI and machine learning specialists\n\nIn my last piece about the [current state of the machine learning market](https://xsolve.software/blog/current-state-machine-learning-market-3-charts/), I broached the topic of the skyrocketing **demand for machine learning specialists**, and ML engineers in particular. This hunger for specialists has resulted in an increase in ML and AI professionals’ salaries. This trend manifests itself in two ways:\n\n* Lack of specialists – there are not enough people with the necessary skills because they are quickly harvested from the market by the bigger players with huge budgets offering fat paychecks.\n* High employment costs – this results from the first point; those specialists still on the market have high earnings expectations, knowing that there is an enormous demand for their skills.\n\nBecause of that, companies which plan projects involving artificial intelligence, machine learning, and other linked technologies are facing the problem of **high recruitment and salary costs**. How high are they actually?\n\n## Salary of a machine learning engineer\n\nThere are numerous job positions related to AI and machine learning, but for this article, I focus on machine learning engineers because the market demand for this role has increased significantly in the past few years. **How high are the earnings of ML engineers?**\n\nFinding a simple answer to this question is difficult. There are various sources and data available and I’ve used three separate sources: Glassdoor, PayScale and Indeed. Summing up my findings in just three words: **through the roof!**\n\nAccording to [Glassdoor](https://www.glassdoor.com/Salaries/machine-learning-engineer-salary-SRCH_KO0,25.htm), an ML engineer can expect a salary ranging from **$87,000 to $158,000** (on average **$121,000**), depending on experience and skills. On [PayScale ](https://www.payscale.com/research/US/Job=Machine_Learning_Engineer/Salary)the range is: **$78,000 – $112,000 – $161,000**; [Indeed](https://www.indeed.com/salaries/Machine-Learning-Engineer-Salaries), in turn, provides a range of $51,000 – $135,000 – $259,000. This data only applies to the U.S. market.\n\nBy averaging from just these three sources, we can estimate that the average salary of a machine learning engineer is around** $123,000 per year** which is consistent with findings from TechRepublic, and IBM reports.\n\n![Salary in U.S. dollars per year](/img/13_machine-learning-engineer-salary-in-u.s.-dollars.png)\n\nHowever, the above chart doesn’t give the whole picture – it’s just **the tip of the cost iceberg**. For example, an AI specialist with experience and a Ph.D. can expect a salary as high as $ 300,000 – $500,000 per year and the average salaries of Google’s DeepMind staff in Great Britain is $345,000, according to a [NYTimes feature article](https://www.nytimes.com/2017/10/22/technology/artificial-intelligence-experts-salaries.html).\n\nFor businesses with **“slightly” smaller budgets** than Google, these costs can be overwhelming.\n\n## ML engineers – the market demand\n\nThere is no conclusive data which can provide an answer to the question how big is the market demand for machine learning jobs, particularly for ML engineers. According to an IBM report [cited by Forbes](https://www.forbes.com/forbes/welcome/?toURL=https://www.forbes.com/sites/louiscolumbus/2017/05/13/ibm-predicts-demand-for-data-scientists-will-soar-28-by-2020/&refURL=https://xsolve.software/blog/outsourcing-machine-learning-projects-shouldnt-cost-perspective/&referrer=https://xsolve.software/blog/outsourcing-machine-learning-projects-shouldnt-cost-perspective/#3496a1b57e3b), by 2020, in the U.S. alone, there will be 700,000 openings for data scientist-type jobs, including ML engineers. At the same time, Tencent [estimates](https://www.theverge.com/2017/12/5/16737224/global-ai-talent-shortfall-tencent-report) that there are only 300,000 AI engineers worldwide when millions are required.\n\nI decided to **search LinkedIn**, using specific keywords to shed some light on this matter (with the reservation that data obtained this way may not be representative nor necessarily reflect reality). According to LinkedIn, there are **45,307 job offers** related to machine learning worldwide, of which **3,439** directly refer to machine learning engineers (**2,239** are in the U.S.):\n\n![Machine Learning jobs](/img/14_machine-learning-related-job-offers-linkedin-.png)\n\nEven if this data is not precise, one thing is for sure; the demand is increasing. According to the LinkedIn report, there are almost **ten times more jobs for machine learning engineers** than there were five years ago, and this figure will only rise in the future.\n\n## Can outsourcing be a solution?\n\nSkyrocketing earnings and huge market demand for ML engineers might be challenging for companies considering machine learning projects. For some companies, outsourcing ML projects to an external provider with trained and experienced ML engineers might be a solution. Of course, this approach has pros and cons, but that’s a topic for an entirely different article.\n\nNevertheless, in the face of extremely high costs related to the employment of machine learning engineers (and other AI-related specialists), the price and time factors are gaining a whole new dimension, and soon most companies wanting to take a **cruise on the ship called artificial intelligence** will face the dilemma: **outsource or not**?"}],"job":null,"photo":null,"cover":"/img/team-meeting-discussion.jpg","lead":"We are standing at the beginning of a new business era shaped by artificial intelligence and [machine learning](https://www.boldare.com/machine-learning/). In 2016 alone, revenue from the [artificial intelligence market worldwide](https://www.statista.com/statistics/607716/worldwide-artificial-intelligence-market-revenues/) reached more than **$1.3 billion** and by the end of 2025, that figure will likely reach a **$59 billion**. To maintain their competitiveness, businesses are embracing this new technology. But being at the vanguard of AI revolution can be costly, big time.","templateKey":"article-page","settings":{"date":"June 05, 2018","slug":"machine-learning-digital-product-costs","type":"blog","category":"Strategy"},"contributor":"Kamil Mizera","box":{"content":{"title":"Building digital products based on machine learning - the cost perspective","tags":null,"clientLogo":null,"coverImage":"/img/team-meeting-discussion.jpg"},"settings":null,"type":null}}}},{"node":{"excerpt":"","id":"3d384b14-f104-5965-86f7-224829f22c9d","fields":{"slug":"/blog/speeding-up-e-commerce-content-moderation-with-machine-learning-based-on-amazon-web-services/"},"frontmatter":{"title":"Speeding up e-commerce content moderation with Machine Learning based on Amazon Web Services","order":null,"content":[{"body":"Machine learning is used, for instance, for better product recommendations and better personalized contact with customers, as well as for analyzing user behaviors or user-created content. What’s more, it supports decision making thanks to [data visualization](http://www.boostlabs.com/machine-learning-benefits-data-visualization/). Before long, machine-learned systems will be employed in all kinds of business, from services to industry, creating advances from which there is no going back.\n\nAt Boldare, we’ve already had the chance to apply machine learning in the context of content moderation, preparing a complex solution for one of our clients. How did it go? Read on to find out.\n\n## The problem: content moderation\n\nNobody really likes manual moderation. It’s a very repetitive and quite tedious activity, requiring a system administrator to accept or reject:\n\n• forum posts,\n\n• profile pictures,\n\n• personal data,\n\n• and many, many other forms of content.\n\nIn small-scale IT systems involving a smaller number of users or products this process may not be so tiresome, but as every business aims to expand, it can quickly become a serious challenge for a growing company. Sometimes a single moderator may not be enough, so more people get engaged in the process, which in turn generates more cost. In fact, the very act of moderation is just a quick evaluation of a given piece of content, something quite trivial and almost mechanical for the moderator.\n\nIn such cases, it’s worth **considering automation**. Most issues can be detected on the level of user data validation and there are many ways to accelerate the process of moderation.\n\nFor example, profile picture moderation regarding the occurrence of faces with correct proportions, with no glasses and hats, can be done by means of a [face validator](https://xsolve.software/blog/face-detection-open-source-symfony3/). There are also dedicated SaaS solutions that will detect offensive content. However, the problem gets more complicated when a less structured sort of content must be moderated, e.g. new user data. In this situation, machine learning algorithms provide an answer.\n\n## Our client’s needs\n\nIn one of our current long-term projects, consisting of the creation and maintenance of an integrated system for tire wholesalers and workshops, our client has faced an ongoing issue of moderation relating to new products added to the system by wholesalers.\n\nWhen carried out by an administrator, the **process is painstaking and time-consuming** and the verification of the product (e.g. a tire, a rim, or a chain) cannot be programmed as a classic validator. Product data contains a lot of special fields, various codes, descriptions, and technical parameters, so only an expert is able identify whether it’s right or wrong. Frequently, some parameters influence the correctness of others, for example, the loudness rate may determine if the name of the tire is correct. There are plenty of cases like this and the logic behind it cannot be reproduced using ordinary algorithms.\n\nAfter consulting our client, we decided to implement a solution which would collect data concerning the products and the results of manual moderation in an external database. This data would then be used to train a machine learning model and create a **prediction mechanism** which could express in percentage terms the probability of the data correctness of an unverified product. In the final stage, having confirmed the compatibility of the predictions with reality, we would be able to define the minimum level of probability, allowing fully automated moderation.\n\n## Technologies\n\nThe app itself is written in **PHP**, using the **Symfony2** framework (2.8 LTS is the current version). The database is **PostgreSQL** (formerly MySQL); and asynchronous processing, employing the **Beanstalkd** queuing system, was also used. We planned to reach our goal with minimum workload, reducing the costs of creating and maintaining the solution.\n\nThe use of an already implemented system of queuing, for both the data collection and fetching prediction results, meant that we would not slow the app down in any way.\n\n## Data collection\n\nTo gather the data, we used [DynamoDB](https://aws.amazon.com/dynamodb/), which was filled with product data during manual moderation. It’s a NoSQL from **Amazon**, extremely efficient and easily scalable. Even though to obtain satisfactory productivity when it comes to complex queries, it’s necessary to create dedicated (paid) indexes on the fields, the system was a rational choice that suited our needs.\n\nWe implemented product data transfer to the external database in an asynchronous way. When an unverified product was accepted, rejected, removed, or edited (in the case of insignificant data errors, the admin can edit the product manually and then accept it) its identifier was added to the queue. The queue consumer downloaded its data from the database and sent it along with the moderation result to DynamoDB, using the **SDK provided by Amazon**.\n\nThanks to this approach, data collection did not significantly affect the speed of the app, in particular moderation. In the case of increased traffic in the app– for instance, the beginning of the tire-changing season – the consumer can be temporarily switched off to reduce the load on the database. It is also easily scalable, e.g. by setting a selected number of processes in Supervisor. This, however, turned out to be unnecessary, as the efficiency of the single process was sufficient for simple data transfer to DynamoDB.\n\n**_Pro-Tip_**: We used dynamic scaling in our DynamoDB tables. It was a very useful solution for us, because we needed a high record capacity in the process of data collection, but that was no longer important during data export and the capacity of table reading had to be increased.\n\n## Target data format\n\n**Amazon Machine Learning** accepts two data formats when creating the so-called datasource: data saved in the [Redshift](https://aws.amazon.com/redshift/) data warehouse and CSV files previously exported to [Amazon S3](https://aws.amazon.com/s3/) storage. Due to the fact that the data warehouse was not a cost-efficient solution as far as the scale of our app was concerned, we chose the other format.\n\nIt turned out that exporting data from DynamoDB to CSV is quite complicated. One way out was to implement it ourselves, but that didn’t seem too efficient. We could also use the ETL [Amazon Glue](https://aws.amazon.com/glue/) service, but we decided on [Amazon Data Pipelines](https://aws.amazon.com/datapipeline/), which also enabled us to set the schedule of data dumping from DynamoDB. There is no need for real-time data used in the ML model; using historical data in a proper scale doesn’t have any particular impact on prediction effectiveness. That’s why exporting data to CSV once every couple of days was just fine and helped us save on resources.\n\n![machine learning case study data pipeline schema for data export](/img/machine-learning-schema-for-data-export.jpg)\n\nThe pipeline we configured operates on another service delivered by Amazon – [Elastic Map Reduce](https://aws.amazon.com/emr/) – which is an extension to the [Apache Hadoop](http://hadoop.apache.org/) framework (it’s possible to use other frameworks too, though). When starting the process, a new EMR cluster of the required size is set ad hoc, which can parallelize the process of data exporting.\n\nInternally, a cluster like that uses [Apache Hive](https://hive.apache.org/) for queries in the HiveQL language, similar to SQL, which makes it possible to both access and process the data from DynamoDB as well as to save as CSV on Amazon S3. This software stack allows working on a large set of data, just like the one containing our products.\n\nThis is a **sample HiveQL query** exporting the contents of a table in DynamoDB to a CSV file on S3:\n\n```\nDROP TABLE IF EXISTS ddb_my_table;\n```\n\n```\nCREATE EXTERNAL TABLE ddb_my_table (first_column boolean, second_column string)\n```\n\n```\nSTORED BY 'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler'\n```\n\n```\nTBLPROPERTIES (\n```\n\n```\n    \"dynamodb.table.name\"=\"#{input.tableName}\",\n```\n\n```\n    \"dynamodb.column.mapping\"=\"first_column:first_column, second_column:second_column\"\n```\n\n```\n);\n```\n\n```\n\n```\n\n```\nDROP TABLE IF EXISTS s3_my_table;\n```\n\n```\nCREATE TABLE s3_my_table (first_column boolean, second_column string)\n```\n\n```\nROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'\n```\n\n```\nLOCATION '#{output.directoryPath}';\n```\n\n```\n\n```\n\n```\nINSERT OVERWRITE TABLE s3_my_table\n```\n\n```\nSELECT * FROM (\n```\n\n```\n    SELECT '1st_column_header' AS first_column, ‘2nd_column_header’ AS second_column\n```\n\n```\n    UNION ALL\n```\n\n```\n    SELECT CAST(first_column AS string) AS first_column, second_column\n```\n\n```\n    FROM ddb_my_table\n```\n\n```\n) res;\n```\n\n```\n\n```\n\nIn the first part of the query, the **_“ddb_my_table”_** is created in Hive, indicating a DynamoDB table configured in the DynamoDBData element. It is necessary to manually define the columns and their types and to manually set the mapping between the Hive and Dynamo tables.\n\nThe next stage is to create the **_“s3_my_table”_** corresponding to the CSV file which is going to be created on S3. Unfortunately, the OpenSCVSerde driver (“Serde” is an acronym of SERialize and DEserialize) has many flaws, and one of them is the lack of support for column names in CSV files, which is available in Amazon Machine Learning’s datasources. This is why we came up with the idea of using the **UNION ALL** expression and putting the column names in the first file line. Another limitation which proved quite problematic is the lack of support for any type other than string, which requires mapping all the columns on the string type by use of the CAST expression.\n\n_**Pro-Tip**_: In the case of array columns, the **_CONCAT_WS()_** function may be of use.\n\nWe placed the query in a HiveActivity element as HiveScript and so, after configuring the cluster, the element representing the DynamoDB table, and the CSV file, we were able to execute the pipeline and the data appeared on S3 after no more than an hour.\n\n## The model\n\nBefore creating the model, it was necessary to configure a new datasource.\n\n![machine learning case study boldare datasource configuration for an ML model](/img/amazon-machine-learning-datasource-dashboard.jpg)\n\nAfter indicating the bucket where the data was saved and selecting the “Verify” option, we were informed that the files were correct. Remember that [Amazon Machine Learning](https://aws.amazon.com/machine-learning/) only accepts data formats where columns are separated by commas, records by new lines, and values containing a comma should be included in double quotation marks.\n\nThe next step was the configuration of the datasource. The data types (binary, categorical, numeric, or text) for features should be selected.\n\n_**Pro-Tip**:_\\*\\* \\*\\*Remember that not all kinds of data that can be expressed in numbers should be tagged as “numeric”: the number of the day of the week or a producer ID are good examples of features which should be classified as “categorical” even though they are represented by numbers.\n\nHaving chosen the types for every feature, you must decide which column is a label, i.e. a column whose value is eventually going to be the prediction result. Next – optionally – you can pick a column containing a unique record identifier.\n\nWhen you’ve confirmed the configuration, the datasource is created and the first statistics are calculated. Additionally, any text values are split into tokens and analyzed. The data will also be scanned for missing or incorrect values.\n\n**_Pro-Tip_**: If you’ve gone through the process of datasource configuration for the first time, it’s a good idea to save the generated JSON metadata (“View input schema”) in a .schema file in a catalogue with the CSV files. This way, the following imports will not require manual setting of the feature types, label, and identifier.\n\nWhen the **datasource is correctly created**, you can configure a machine learning model based on it. Choose the datasource and the model and evaluation name. In the beginning, you can use the default settings for the algorithm of training and evaluation: random data split into 70% training data and 30% data used to evaluate the effectiveness of the model, and several others. If you find these settings insufficient, try your own configuration.\n\nDepending on the column type selected as a label (target), an adequate learning algorithm will be matched: logistic regression for binary values, multinomial logistic regression for multiclass classification (categorical values), and linear regression for numeric values.\n\nAfter configuring and training the model, you can have a look at the evaluation and, if necessary, improve the quality of data. If you don’t find anything wrong with the evaluation, you can start generating first predictions.\n\n## Predictions\n\nAmazon provides the possibility of predicting the value of the target attribute (label) for a particular observation (feature set) in two ways:\n\n• larger batches for the data gathered in the CSV format on **Amazon S3**,\n\n• real-time predictions via the REST endpoint for single observations.\n\nIn our case, the prediction data should have initially been sent for all the unverified products, so that later on only individual products would need to fetch the result. For this reason, we decided to use real-time endpoints.\n\n![machine learning case study boldare the solution’s architecture](/img/machine-learning-architecture.jpg)\n\nOwing to the fact that Amazon charges both for the number of observations sent to the endpoint and for the time of its operation, our implementation was based on creating a real-time endpoint in the cron task, sending the data of all the products queued in Beanstalkd, saving the prediction results in the PostgreSQL database, and finally removing the created endpoint. All the operations on the prediction endpoint can be realized through the Amazon Machine Learning REST API .\n\nIf we decided to use the batch predictions, we would have to – just as in the case of datasources for the ML model – prepare CSV files of appropriate format and send them to Amazon S3. Generating the prediction results is then asynchronous, which is a convenient solution in the case of a large number of observations. However, the real-time endpoint was sufficient for our needs.\n\n## Results and plans for the future\n\nCurrently, we’re going to release the solution in production environment. Data collection will likely take many weeks and as soon as we have a sufficient amount of data, we’ll be able to evaluate the effectiveness of the predictions and, if necessary, improve their quality.\n\nThe results were pretty promising during the development environment testing, but we had difficulties with collecting data about incorrect products. This was caused by the fact that in the app, all the rejected products are removed from the system, so historical data is missing that could have been used to create a better datasource.\n\nThe current implementation serves only to present the prediction results to the admin and is focused on data gathering and model training. In the future, if predictions reach a satisfactory level of accuracy, we’re planning to extend our solution by automatic acceptance of correct products.\n\nThe technology stack that we used will also serve us in future implementations of [machine learning](https://www.boldare.com/machine-learning/) in the new apps that we create."}],"job":null,"photo":null,"cover":"/img/human-eye.jpg","lead":"Machine learning (ML) has gone mainstream. The use of this technology – a consequence of AI development – is becoming more and more common in the world of business. Self-learning systems are ideally suited to analyzing large volumes data and making predictions.","templateKey":"article-page","settings":{"date":"June 05, 2018","slug":"machine-learning-content-moderation","type":"blog","category":"Case Study"},"contributor":"Paweł Krynicki","box":{"content":{"title":"Speeding up e-commerce content moderation with Machine Learning based on Amazon Web Services","tags":null,"clientLogo":null,"coverImage":"/img/human-eye.jpg"},"settings":null,"type":"BLOG"}}}}]}},"pageContext":{}}