Data-driven approaches have been proposed as effective strategies for the inverse design and optimization of photonic structures in recent years. In order to assist data-driven methods for the design of topology of photonic devices, we propose a topological encoding method that transforms photonic structures represented by binary images to a continuous sparse representation. This sparse representation can be utilized for dimensionality reduction and dataset generation, enabling effective analysis and optimization of photonic topologies with data-driven approaches. As a proof of principle, we leverage our encoding method for the design of two dimensional non-paraxial diffractive optical elements with various diffraction intensity distributions. We proved that our encoding method is able to assist machine-learning-based inverse design approach for accurate and global optimization.With the fast evolution of machine learning (ML) and deep learning (DL) techniques 12 , data-driven methods are emerging as an alternative way to discover and design photonic structures and devices 13 . Feedforward neural networks are leveraged for the approximation of the photonics systems with tens to hundreds of parameters, and have been utilized to successfully optimize photonic structures such as photonic crystals 14,15 , waveguide 16,17 , chiral metamaterials 18 , and metasurfaces 19 . When the degree of freedom (DOF) of the photonic system grows to thousands and more, convolutional neural networks (CNN) are adopted for the accurate prediction of the physical responses with much lower computational complexity 20,21 . Photonic structures represented in pixelated images, for example, are usually processed by CNNs to reduce the DOF for further optimization. Additionally, generative models, such as variational autoencoder (VAE) 22 and generative adversarial network (GAN) 23,24 , are utilized for the design of high DOF metasurface nanostructures in an expeditious way [25][26][27][28] . The stochastic nature of the generative models enables the exploration of the solution space in a global way. Consolidating with traditional optimization techniques, GAN and VAE are able to discover the topology of nanostructures with improved efficiency and robustness [29][30][31] .In the problems of inverse design of photonic structures, optimizing the topology of a photonic structure with arbitrary shape is a long-sought-after goal. Typically, the topology of photonic structures is represented in binary images. Because of the discretization and the high DOF of binary images, optimization is likely stuck in local minima. Although generative models are able to discover new topologies so as to approach global minimum, the bias of the training dataset and the limited capacity of the network cause incompleteness of the solution space, i.e., the global minimum may not be included in the space defined by the training dataset. Here, we propose an encoding method that is able to transform the binary image to a continuous sparse representation. This encoding appro...