% 导入必要的库
import tensorflow.keras.layers.*
import tensorflow.keras.models.*
import tensorflow.keras.optimizers.*
import tensorflow.keras.callbacks.*
% 加载数据集
data = readtable(‘solar_data.csv’);
X = data(:, 1:end-1); % 输入特征
y = data(:, end); % 输出目标
% 定义 Transformer 模型
inputLayer = InputLayer(InputShape=[size(X, 2), 1]);
encoderLayer = TransformerEncoder(HeadCount=8, DimensionSize=64, FeedForwardDimensionSize=256, Dropout=0.1);
flattenLayer = Flatten;
denseLayer = Dense(1, Activation=‘linear’);
model = Sequential([
inputLayer,
encoderLayer,
flattenLayer,
denseLayer
]);
% 编译模型
model.compile(Optimizer=Adam(LearningRate=0.001), Loss=‘mse’, Metrics={‘mse’});