In order to compensate for the errors caused by disturbing factors such as temperature, humidity, vibration and pressure in the environment during the measurement of grating sensor, a measurement algorithm based on an improved temporal convolutional network (TCN) is proposed. The environmental signals are first collected to construct the data set, and then the improved TCN model with S-shaped rectified linear activation unit activation function is used to subdivide the grating sensor signals and compensate the environmental errors. Experimental results show that in the training of the data set with the least error reduction, the error of the original TCN after compensation is about 4.52 nm, the error of the improved TCN after compensation is about 3.46 nm. Therefore, the improved TCN reduces the error by at least about 23.5%. Compared with the other same type of algorithm, the improved TCN can reduce the error in the verification set by at least 44.3%, which proves the feasibility and effectiveness of the proposed error compensation algorithm, and lays a certain foundation for the realization of ultra-precision measurement of gratings.