Harnessing the Power of Matrix Programming for AI and ML with SAS IML

In the world of artificial intelligence (AI) and machine learning (ML), matrix operations form the computational backbone of many algorithms. From neural networks to support vector machines, many AI/ML techniques rely heavily on efficient matrix computation. This is where the SAS Interactive Matrix Language (IML) shines.

SAS IML is a powerful matrix programming language within SAS that makes matrix operations a breeze. With its concise syntax, wide range of built-in functions, and seamless integration with the rest of the SAS ecosystem, SAS IML is a valuable tool in the AI/ML practitioner‘s toolkit. In this article, we‘ll dive into how SAS IML can simplify your AI/ML workflows and solve complex, iterative problems with ease.

Why SAS IML for AI and Machine Learning?

Traditional SAS programming with the DATA step and PROCs is great for many data processing and analysis tasks. However, when it comes to matrix-heavy computations common in AI and ML, the DATA step can be cumbersome and inefficient. This is where SAS IML steps in.

SAS IML provides a concise, matrix-oriented syntax that is much more natural for expressing matrix algorithms. What might take dozens of lines of complex DATA step code can often be accomplished in a few simple lines of IML.

For example, consider a simple neural network with one hidden layer. Implementing the forward propagation step in DATA step code would require multiple PROC TRANSPOSEs, complex array referencing, and custom functions. In SAS IML, it can be expressed succinctly as:

/* Assume X is the input matrix, W1 and W2 are weight matrices */
H = tanh(X * W1);  /* Hidden layer activation */
Y = H * W2;        /* Output layer */

Not only is the IML code cleaner and easier to understand, it‘s also much faster. In one benchmark on a large neural network training task, SAS IML showed a 50x speedup over an equivalent DATA step implementation. [1]

SAS IML also offers a rich library of built-in functions that are useful for AI/ML, including:

  • Matrix decompositions (SVD, Cholesky, etc.)
  • Optimization solvers (LP, QP, NLP, etc.)
  • Random number generators (normal, uniform, binomial, etc.)
  • Statistical functions (covariance, correlation, principal components, etc.)

Having these functions readily available in a matrix-oriented environment makes implementing many AI/ML algorithms much simpler and more efficient.

Applying SAS IML to Common AI/ML Use Cases

To illustrate the power of SAS IML for AI/ML, let‘s walk through a few common use cases and see how they can be implemented.

Neural Networks

Neural networks are a cornerstone of modern AI and deep learning. At their core, neural networks are composed of matrix multiplication and element-wise operations, making them a natural fit for SAS IML.

Here‘s a simple example of implementing a feedforward neural network with one hidden layer in SAS IML:

proc iml;
  /* Input data matrix X and target matrix Y */ 
  use traindata;
  read all var _num_ into X;
  read all var {target} into Y;

  /* Initialize weight matrices W1 and W2 */
  call randseed(1);
  W1 = randnormal(ncol(X), 100); 
  W2 = randnormal(100, ncol(Y));

  /* Training loop */
  do epoch = 1 to 100;
    /* Forward propagation */
    H = tanh(X * W1);
    Yhat = H * W2;

    /* Backpropagation */
    error = Y - Yhat;
    dW2 = t(H) * error;
    dW1 = t(X) * ((error * t(W2)) # (1 - H##2));

    /* Update weights */
    W1 = W1 + 0.01 * dW1;
    W2 = W2 + 0.01 * dW2;
  end;
quit;

In under 30 lines of SAS IML code, we have a fully functional neural network that can learn complex patterns in data. The matrix operations make the code compact and easy to follow. Imagine trying to implement this in DATA step!

Support Vector Machines

Support vector machines (SVMs) are a popular ML method for classification and regression. Training an SVM involves solving a quadratic optimization problem, which can be efficiently done using matrix operations.

SAS IML provides a built-in QUAD function for solving quadratic programming problems. Here‘s how you could train a linear SVM in SAS IML:

proc iml;
  use traindata;
  read all var _num_ into X;
  read all var {target} into y;

  n = nrow(X);
  one = j(n, 1, 1);

  /* Set up QP problem */
  H = block(X`*X, -X`*X, -X`*X, X`*X);
  f = repeat({0}, 1, ncol(H));
  A = block(diag(y), diag(-y), I(n), I(n));
  b = repeat({1}, 1, ncol(A));

  /* Solve QP */
  call qp(alpha, , H, f, A, b);

  /* Extract solution */
  w = (alpha[1:n] - alpha[n+1:2*n])` * X;
  b = alpha[1:n]` * (one - X*w);
quit;

The key steps are setting up the quadratic programming matrices (H, f, A, b) based on the SVM formulation, then calling the QUAD function to solve it. The resulting w and b define the trained SVM model.

Compared to implementing an SVM solver from scratch in DATA step or PROC SQL, the SAS IML code is much more concise and computationally efficient, leveraging the built-in QP optimizer.

Clustering

Clustering is an unsupervised ML technique for partitioning data into groups. Many clustering algorithms, such as k-means and hierarchical clustering, are based on matrix distance computations and updates.

Here‘s a simple example of k-means clustering in SAS IML:

proc iml;
  use mydata;
  read all var _num_ into X;

  k = 3;  /* Number of clusters */
  maxiter = 100;

  /* Initialize cluster centers randomly */
  call randseed(1);
  C = X[uniqueby(sample(1, nrow(X), k)), ];

  do iter = 1 to maxiter;
    /* Assign points to nearest cluster center */
    D = distance(X, C);
    cluster = D[, ><];

    /* Update cluster centers */
    do c = 1 to k;
      if sum(cluster=c) > 0 then
        C[c, ] = mean(X[loc(cluster=c), ]);
    end;
  end;

  print iter C;
quit;

The DISTANCE function computes the matrix of distances between each data point and each cluster center, which is then used to assign points to the nearest cluster. The cluster centers are updated by taking the mean of the points in each cluster.

Performing these matrix operations in SAS IML is much faster than using a DO loop in the DATA step, especially for large, high-dimensional data sets. In one test on a data set with 100,000 rows and 10 features, the SAS IML version was over 200 times faster than a DATA step implementation [2].

Advanced SAS IML Features for AI and ML

Beyond the core matrix programming capabilities, SAS IML offers several advanced features that are particularly useful for AI and ML applications:

  • Integration with R and Python: SAS IML can directly call R and Python code, allowing you to leverage the extensive AI/ML libraries available in those languages (e.g., TensorFlow, PyTorch, scikit-learn). This is a game-changer for incorporating cutting-edge AI/ML techniques into your SAS workflows.

  • Nonlinear optimization: In addition to linear and quadratic programming, SAS IML provides functions for general nonlinear optimization, such as the NLPNRA function for solving nonlinear least squares problems. This is useful for tasks like training deep neural networks and fitting complex ML models.

  • Compiled functions: For computationally intensive tasks, you can compile your SAS IML modules into threaded C code using PROC FCMP. This can provide significant speedups, particularly for problems that don‘t vectorize well. In one case study, using compiled functions accelerated a complex ML model training process by over 10x [3].

  • Parallel processing: SAS IML supports parallel processing through the THREAD and REMOTE compute services. This allows you to speed up your AI/ML workflows by distributing matrix computations across multiple cores or nodes. One researcher reported a 6x speedup in training time for a large neural network model using SAS IML parallel processing [4].

Real-World AI/ML Case Studies with SAS IML

To further illustrate the potential of SAS IML for AI and ML, let‘s look at a few real-world case studies and research papers:

  • Fraud Detection at Bank of America: Bank of America used SAS IML to develop an AI-powered fraud detection system based on neural networks and gradient boosting. The system achieved a 60% reduction in false positives compared to their previous rule-based approach, while detecting 30% more actual fraud cases. The matrix programming capabilities of SAS IML were key to efficiently training and deploying the complex ML models on large transaction data [5].

  • Demand Forecasting at Walmart: Walmart‘s data science team used SAS IML to build a large-scale demand forecasting system based on recurrent neural networks. The system generates daily sales forecasts for over 500,000 SKU-store combinations. SAS IML enabled them to prototype and iterate on different neural network architectures quickly, and the integration with the rest of the SAS platform made operationalization seamless [6].

  • Cancer Research at the NIH: Researchers at the National Institutes of Health (NIH) used SAS IML to develop novel ML methods for analyzing genomic data and identifying cancer subtypes. The matrix operations in SAS IML made it easy to implement custom feature selection and dimensionality reduction techniques that improved the accuracy of the subtype classifiers. The researchers noted that "SAS IML provides an excellent environment for rapid prototyping of novel ML algorithms" [7].

These are just a few examples of how SAS IML is being used to push the boundaries of AI and ML in diverse domains. As data volumes continue to grow and AI/ML techniques become more sophisticated, the matrix programming capabilities of SAS IML will become increasingly valuable.

Conclusion

For SAS users working in AI and ML, SAS IML is an indispensable tool. Its concise matrix syntax, wide range of built-in functions, and seamless integration with the SAS ecosystem make it the ideal environment for developing and deploying AI/ML solutions.

As we‘ve seen in this article, SAS IML simplifies the implementation of core AI/ML techniques like neural networks, SVMs, and clustering compared to traditional SAS programming. It also offers advanced features like integration with R and Python, nonlinear optimization, compiled functions, and parallel processing that extend its capabilities even further.

Real-world case studies from banking, retail, healthcare, and other domains showcase the impact that SAS IML is having on the practice of AI and ML. As organizations seek to harness the power of AI/ML to drive innovation and competitive advantage, SAS IML will undoubtedly play a key role.

If you‘re a SAS user looking to level up your AI/ML skills, diving into SAS IML is a great place to start. With its intuitive matrix programming model and tight integration with the SAS platform you already know, you‘ll be surprised how quickly you can start tackling complex, iterative AI/ML problems. Get ready to simplify your code, speed up your computations, and take your AI/ML projects to new heights with SAS IML.

References

[1] Johnson, T. (2019). Benchmarking SAS IML vs DATA Step for Training Neural Networks. Proceedings of the SAS Global Forum 2019, Paper 3720-2019.

[2] Zhang, Y. (2018). Accelerating K-Means Clustering with SAS IML. SAS Users Group International Conference, Paper 2848-2018.

[3] Garcia, M. (2020). Speeding Up Machine Learning Model Training with SAS IML Compiled Functions. SAS Blogs.

[4] Raghunathan, R. (2017). Parallel Processing for Deep Learning in SAS IML. Proceedings of the SAS Global Forum 2017, Paper 840-2017.

[5] Patel, N. et al. (2019). AI-Powered Fraud Detection at Bank of America. SAS Global Forum Executive Conference.

[6] Zhao, L. et al. (2021). Large-Scale Demand Forecasting with Recurrent Neural Networks in SAS IML. SAS Blogs.

[7] Chen, J. et al. (2016). A SAS IML Framework for Machine Learning in Genomic Studies. Journal of Biomedical Informatics, 63, 456-464.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts