You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

相同拓扑下Matlab与Python(TensorFlow) ANN的精度差异排查

Matlab与TensorFlow神经网络预测精度差异排查

背景

此前使用Matlab开发神经网络,因局限转用Python TensorFlow库,在相同数据集与ANN拓扑结构下,两者预测精度差异极大,需排查原因。

Matlab实现及结果

初始代码

net = feedforwardnet([18 12 23],'trainrp');
rng('default') %Just for repeatability
net.divideParam.trainRatio = 75/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net.performFcn='mae';
net.layers{1}.transferFcn = 'logsig';
net.layers{2}.transferFcn = 'softmax';
net.layers{3}.transferFcn = 'softmax';
net.trainParam.showWindow = 0;
[net,tr] = train(net,input,target);
output=net(input);
f=mean(mean(abs(target-output)./((abs(target)+abs(output))/2)))*100;

初始误差f=8.0164,处于可接受范围。

关闭自动预处理后的结果

添加以下代码关闭Matlab默认的输入输出预处理:

net.Inputs{1}.processFcns={};
net.output.processFcns={};

误差变为f=35.5123,仍远低于TensorFlow版本。

TensorFlow实现及问题

代码

#For repeatability
SEED = 777
random.seed(SEED)
tf.random.set_seed(SEED)
np.random.seed(SEED)
# split the data into train and test set
train_x, test_x, train_y, test_y = train_test_split(
    input_array, output_array, test_size=0.15, random_state=7, shuffle=True)

# split the train into train and validation set
train_x, val_x, train_y, val_y = train_test_split(
    train_x, train_y, test_size=0.17, random_state=7, shuffle=True)

# Model function definition (21 features in every observation, 11 features in output, the same as in Matlab)
model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(21,), name="Input"),
    tf.keras.layers.Dense(18, activation = 'sigmoid', name="Hidden1"),
    tf.keras.layers.Dense(12, activation ='softmax', name="Hidden2"),
    tf.keras.layers.Dense(23, activation ='softmax', name="Hidden3"),
    tf.keras.layers.Dense(11, activation = 'linear', name="Output")])
# Compilation of model
model.compile(optimizer='RMSprop',
              loss='MeanAbsoluteError')

# fitting the model
model.fit(train_x, train_y, epochs=200, batch_size=64, 
          validation_data=(val_x, val_y),verbose=0)

values = model.predict(input_array, verbose=0)
Error=abs(values-output_array)/output_array*100
f=np.mean(Error)

最终误差f=94.4482,调整epochs与batch_size后误差仅波动±5%,无明显改善。

核心排查方向

  • 数据预处理对齐:Matlab默认会对输入输出做归一化(如mapminmax映射到[-1,1]或[0,1]),即使关闭预处理,也要确认TensorFlow数据是否和Matlab关闭预处理后的分布完全一致;若要对齐初始Matlab效果,需手动实现相同的归一化逻辑。
  • 权重初始化对齐:Matlab的feedforwardnet默认用Nguyen-Widrow初始化权重,而TensorFlow Dense层默认是Glorot均匀初始化,可手动设置TensorFlow的权重初始化方式为Nguyen-Widrow,或确认两者初始化后的权重分布差异。
  • 数据划分逻辑对齐:Matlab默认按顺序拆分训练/验证/测试集,而TensorFlow用train_test_split做随机拆分,且两次拆分后的验证集比例约为14.45%(接近15%但有偏差),建议改成和Matlab一致的划分方式(如按顺序拆分,或固定相同随机逻辑)。
  • 训练参数对齐:
    • Matlabtrainrp默认学习率为0.01,TensorFlow的RMSprop默认学习率为0.001,学习率差异会大幅影响收敛效果,建议将TensorFlow的学习率调整为0.01。
    • Matlabtrainrp默认最大迭代次数为1000,而TensorFlow仅设置200epochs,可增加epochs至1000观察效果。
  • 误差计算方式对齐:Matlab用的是对称MAPE,TensorFlow用的是普通MAPE,两种计算方式数值差异明显,先统一误差计算逻辑,再对比精度差异。
  • Softmax层行为确认:确认Matlab与TensorFlow的Softmax层计算逻辑是否完全一致(均为对层内所有神经元输出做归一化),避免因框架实现细节差异导致输出分布不同。

内容的提问来源于stack exchange,提问作者Karls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:54:58