In this work, we propose a channel allocation and power control algorithm for energy harvesting (EH) device-to-device (D2D) communication based on nonorthogonal multiple access (NOMA). The algorithm considers users’ quality of service (QoS) and energy causality constraint to maximize the total capacity of D2D groups. The optimal offline allocation of channel and power is realized firstly. Then, the offline optimization results are taken as the training dataset to train the neural network to obtain the optimal model of the transmission power. The online power allocation optimization algorithm is further proposed. Simulation results show that the offline algorithm can improve the total capacity of D2D groups, and the performance of the online algorithm is close to the offline algorithm.