IEEE 802.11ah, marketed as Wi-Fi HaLow, operates at Sub 1 GHz spectrum to provide broad coverage, high throughput, energy efficiency and scalability. This makes IEEE 802.11ah a promising candidate for the Internet of Things (IoT). One of the major enhancements in the MAC layer is the Restricted AccessWindow (RAW) mechanism, which focuses on mitigating the channel contention in dense networks. The RAW mechanism reduces the channel contention among the group of devices by restricting their channel access to the allocated RAW slots. Since the standard does not specify the optimal RAW configuration parameters, choosing the number of RAW slots has significant impact on the performance of the RAW mechanism. In this paper, we develop an optimization framework by exploiting the Multilayer Perceptron Artificial Neural Network (MLPANN)to find the optimal number of RAW slots that can maximize the performance of the RAW mechanism in terms of throughput, delay and energy consumption. We train the ANN using the network size, Modulation and Coding Schemes, duration of the RAW period and the optimal number of RAW slots found using the analyticalmodel presented in this paper. Further, we evaluate the performance of the RAW mechanism by choosing the optimal number of RAW slots provided by the ANN-based optimization framework. Results show that the proposed scheme significantly enhances the performance of the RAW mechanism. Finally, the analytical results are corroborated using extensive simulations done in ns-3.