Continuous model evaluation with BigQuery ML, Stored Procedures, and Cloud Scheduler

Continuous evaluation – the process of ensuring a production machine learning model is still performing well on new data – is an essential part in any ML workflow. Performing continuous evaluation can help you catch model drift, a phenomenon that occurs when the data used to train your model no longer reflects the current environment. For example, with a model classifying news articles, new vocabulary may emerge that were not included in the original training data. In a tabular model predicting flight delays, airlines may update their routes, leading to lower model accuracy if the model isn’t retrained on new data. Continuous evaluation helps you understand when to retrain your model to ensure performance remains above a predefined threshold. In this post, we’ll show you how to implement continuous evaluation using BigQuery ML, Cloud Scheduler, and Cloud Functions. A preview of what we’ll build is shown in the architecture diagram below.To demonstrate continuous evaluation, we’ll be using a flight dataset to build a regression model predicting how much a flight will be delayed.Creating a model with BigQuery MLIn order to implement continuous evaluation, we’ll first need a model deployed in a production environment. The concepts we’ll discuss can work with any environment you’ve used to deploy your model. Here we’ll use BigQuery Machine Learning (BQML) to build the model. BQML lets you train and deploy models on custom data stored in BigQuery using familiar SQL. We can create our model with the following query:Running this will train our model and create the model resource within the BigQuery dataset we specified in the CREATE MODEL query. Within the model resource, we can also see training and evaluation metrics. When training completes, the model is automatically available to use for predictions via a ML.PREDICT query:With a deployed model, we’re ready to start continuous evaluation. The first step is determining how often we’ll evaluate the model, which will largely depend on the prediction task. We could run evaluation on a time interval (i.e. once a month), or whenever we receive a certain number of new prediction requests. In this example, we’ll gather evaluation metrics on our model on a daily basis.Another important consideration for implementing continuous evaluation is understanding when you’ll have ground truth labels available for new data. In our flights example, whenever a new flight lands we’ll know how delayed or early it was. This could be more complex in other scenarios. For example, if we were building a model to predict whether someone will buy a product they add to their shopping cart, we’d need to determine how long we’d wait once an item was added (minutes? hours? days?) before marking it as unpurchased.Evaluating data with ML.EVALUATEWe can monitor how well our ML model(s) performs over time on new data, by evaluating our models regularly and inserting them into a table on BigQuery.Here’s the normal output you would get from using ML.EVALUATE:In addition to these metrics, we will also want to store some metadata, such as the name of the model we evaluated and the timestamp of the model evaluation. But as you can see below, the following code can quickly become difficult to maintain, as every time you execute the query, you would need to replace MY_MODEL_NAME twice (on lines 3 and 6), with the name of the model you created (e.g., “linreg”).Creating a Stored Procedure to evaluate incoming dataYou can use a Stored Procedure, which allows you to save your SQL queries and run them by passing in custom arguments, like a string for the model name. CALL modelevaluation.evaluate(“linreg”); Doesn’t this look cleaner already? To create the stored procedure, you can execute the following code, which you can then call using the CALL code shown above. Notice how it takes in an input string, MODELNAME, which then gets used in the model evaluation query.Another added benefit of stored procedures is that it’s much easier to share the query to CALL a stored procedure with others — which abstracts away from the raw SQL — rather than share the full SQL query. Using the Stored Procedure to insert evaluation metrics into a tableUsing the stored procedure below, in a single step, we can now evaluate the model and insert it to a table, modelevaluation.metrics, which we will first need to create. This table needs to follow the same schema as in the stored procedure. Perhaps the easiest way is to use LIMIT 0, which is a cost-free query returning zero rows, while maintaining the schema.With the table created, now every time you run the stored procedure on your model “linreg”, it will evaluate the model and insert them as a new row into the table:CALL modelevaluation.evaluate_and_insert(“linreg”);Continuous evaluation with Cloud Functions and Cloud SchedulerTo run the stored procedure on a recurring basis, you can create a Cloud Function with the code you want to run, and trigger the Cloud Function with a cron job scheduler like Cloud Scheduler.Navigating to the Cloud Functions page on Google Cloud Platform, create a new Cloud Function that uses a HTTP trigger type:Note the URL, which will be the trigger URL for this Cloud Function. It should look something like:https://<region>-<projectid>.cloudfunctions.net/<functionname>Clicking “Next” on your Cloud Functions gets you to the editor, where you can paste the following code, while setting the Runtime  to “Python” and changing the “Entry point” to “updated_table_metrics”:Under main.py, you can use the following code:Under requirements.txt, you can paste the following code for the required packages:You can then deploy the function, and even test your Cloud Function by clicking on “Test the function” just to make sure it returns a successful response:Next, to trigger the Cloud Function on a regular basis, we will create a new Cloud Scheduler job on Google Cloud Platform.By default, Cloud Functions with HTTP triggers will require authentication, as you probably don’t want anyone to be able to trigger your Cloud Functions. This means you will need to include a service account to your Scheduler job that has IAM permissions for:Cloud Functions InvokerCloud Scheduler Service AgentOnce the job is created, you can try to run the job by clicking “Run now”.Now you can check your BigQuery table and see if it’s been updated! Across multiple days or weeks, you should start to see the table populate, like below:Visualizing our model metricsIf we’re regularly running our stored procedure on new data, analyzing the results of our aggregate query above could get unwieldy. In that case, it would be helpful to visualize our model’s performance over time. To do that we’ll use Data Studio. Data Studio lets us create custom data visualizations, and supports a variety of different data sources, including BigQuery. To start visualizing data from our BigQuery metrics table, we’ll select BigQuery as a data source, choose the correct project, and then write a query capturing the data we’d like to plot:For our first chart, we’ll create a time series to evaluate changes to RMSE. We can do this by selecting “timestamp” as our dimension and “rmse” as our metric:If we wanted more than one metric in our chart, we can add as many as we’d like in the Metric section. With our metrics selected, we can switch from Edit to View mode to see our time series and share the report with others on our team. In View mode, the chart is interactive so we can see the rmse for any day in the time series by hovering over it:We can also download the data from our chart as a csv or export it to a sheet. From this view, it’s easy to see that our model’s error increased quite a bit on November 19th.What’s next?Now that we’ve set up a system for continuous evaluation, we’ll need a way to get alerts when our error goes above a certain threshold. We also need a plan for acting on these alerts, which typically involves retraining and evaluating our model on new data. Ideally, once we have this in place we can build a pipeline to automate the process of continuous evaluation, model retraining, and new model deployment. We’ll cover these topics in future posts – stay tuned!If you’d like to learn more about any of the topics covered in this post, check out these resources:BigQuery Machine Learning quickstartBigQuery Stored ProceduresData Studio + BigQuery quickstart Let us know what you thought of this post, and if you have topics you’d like to see covered in the future! You can find us on Twitter at @polonglin and @SRobTweets.Related ArticleHow to build demand forecasting models with BigQuery MLWith BigQuery ML, you can train and deploy machine learning models using SQL. With the fully managed, scalable infrastructure of BigQuery…Read Article
Quelle: Google Cloud Platform

Compliance with confidence: Introducing Assured Workloads Support

As organizations in regulated industries modernize and adopt cloud technologies, ensuring the security, privacy, and regulatory compliance of their sensitive workloads is an essential part of choosing a cloud provider. Regulated customers have specific compliance needs around data locality and personnel access to customer data. In the US specifically, these are mandated by requirements under the Department of Defense (i.e., IL4), the FBI’s Criminal Justice Information Services Division (CJIS), and the Federal Risk and Authorization Management Program (FedRAMP).Last year, we introduced Assured Workloads (now generally available with additional features in preview) which lets Google Cloud customers easily and quickly create controlled environments in which US data location and US person support controls are enforced. Regulated customers, and the organizations which interact with them, can use this product to support their compliance efforts by:Choosing to store their sensitive workloads in the US only;Ensuring only Google personnel who meet criteria on geographical access location (currently, US only), background checks, and “US Person” status, can support their workload.We understand that these compliance regulations have a significant impact to your organization and safeguarding your business is important to us. Therefore, today, we’re introducing Assured Workloads Supportfor Google Cloud, which is now generally available (GA) to Premium Support customers. Assured Workloads Support is a Value Add Service to Premium Support customers, who will receive Premium Support from a US Person, in a US Location, 24/7. Customers also receive all the key benefits from Premium Support—15-min response time  for P1 cases, issues resolved by customer aware Google Technical Solution Engineers with access to your business systems information, and direct engagement with a named Technical Account Manager (TAM), a trusted technical advisor focused on operational rigor, platform health and architectural stability. We look forward to expanding Assured Workloads Support in other regions beyond the US later this year. Assured Workloads Support for Google Cloud is available for purchase effective January 19th, 2021. Assured Workloads Support is available to customers who purchase the Assured Workloads Premium Subscription and Premium Support. Please connect with your Google Cloud Sales representative to learn more.Related ArticleCompliance without compromise: Introducing Assured Workloads for GovernmentAssured Workloads for Government, currently in Private Beta, can help you serve your government workloads without the compromises of trad…Read Article
Quelle: Google Cloud Platform

Introducing real-time data integration for BigQuery with Cloud Data Fusion

Businesses today have a growing demand for real-time data integration, analysis, and action. More often than not, the valuable data driving these actions—transactional and operational data—is stored either on-prem or in public clouds in traditional relational databases that aren’t suitable for continuous analytics. While old-school migrations or batch ETL loads can achieve the objective of loading data to a data warehouse, these high-latency approaches don’t cut it when it comes to making the accurate decisions based upon the most up-to-date insights. Cloud Data Fusion is a fully managed, cloud-native data integration and ingestion service that helps developers, data engineers, and business analysts alike to efficiently build and manage ETL/ELT jobs. Today we’re announcing the public preview launch of the replication application in Data Fusion that enables low-latency, real-time data replication from transactional and operational databases such as SQL Server and MySQL directly into BigQuery. Let’s take a closer look at the benefits of replication in Data Fusion:Remove technical bottlenecks so even citizen developers can set up replication easilyCloud Data Fusion features a simple, wizard-driven interface that enables even citizen developers such as ETL developers and data analysts to easily set up data replication. This standard, easy-to-use interface eliminates the need for development of complicated, bespoke tools for each type of operational database, thereby enabling self-service, continuous replication of data to BigQuery.Feasibility assessment and actionable recommendationsIt also includes an assessment tool to help identify schema incompatibilities, connectivity issues, and missing features prior to starting replication, then provides corrective actions. This helps users get ahead of potential issues during replication, thereby leading to faster development and iteration. Easily access the latest operational data in real time for analysis within BigQueryChange data capture, or CDC, provides a representation of data that has changed in a stream, allowing computations and processing to focus specifically on only the most recently changed records, thereby minimizing egress toll on sensitive production systems. With this release, Data Fusion now offers log-based replication directly into BigQuery. It integrates with Debezium as the change provider for making CDC logs from various databases available in a common format. It currently includes support for Microsoft SQL Server (which relies upon SQL Server CDC) and MySQL (which relies upon MySQL Binary Log). With support for CDC streams, Google Cloud users have access to the latest data in BigQuery for analysis and action.Enterprise scalability to support high-volume transactional databasesInitial loads of data to BigQuery are supported with zero-downtime snapshot replication to make the data warehouse ready for consuming changes continuously. Once the initial snapshot is done, high-throughput, continuous replication of changes then starts in real-time. End-to-end operational visibilityData Fusion also provides operational dashboards to monitor throughput, latency, and errors in replication jobs. These dashboards provide real-time insights into replication performance. This lets users proactively identify potential bottlenecks, and monitor data delivery SLAs.Take advantage of key Google Cloud features and integrationsReplication is available in all Google Cloud regions supported today for Data Fusion. This launch includes support for Customer-Managed Encryption Keys (CMEK) and VPC-SC. Cloud Data Fusion’s integration within the Google Cloud platform ensures that the highest levels of enterprise security and privacy are observed while making the latest data available in your data warehouse for analytics.Ready to try out replication? Create a new instance of Data Fusion and add the replication app. Don’t forget to bring the getting started guide along for the ride.
Quelle: Google Cloud Platform