我每次都得到不同的结果,如何使我的代码可重现?

I get different results every time, how can I make my code reproducible?

我使用了极限学习机 (ELM) 算法。我有两个文件,一个用于训练数据集,一个用于测试数据集,我对数据进行了标准化。我每次都得到不同的结果,我该如何修正我的结果?

我的代码:

import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from scipy.linalg import pinv2

#import dataset
train = pd.read_excel('INRStrai.xlsx')
test = pd.read_excel('INRStes.xlsx')

#scaler data
scaler = MinMaxScaler()
X_train = scaler.fit_transform(train.values[:,1:])
y_train = scaler.fit_transform(train.values[:,:1])
X_test = scaler.fit_transform(test.values[:,1:])
y_test = scaler.fit_transform(test.values[:,:1])

#input size
input_size = X_train.shape[1]

#Number of neurons
hidden_size = 300

#weights & biases
input_weights = np.random.normal(size=[input_size,hidden_size])
biases = np.random.normal(size=[hidden_size])

#Activation Function
def relu(x):
   return np.maximum(x, 0, x)

#Calculations
def hidden_nodes(X):
    G = np.dot(X, input_weights)
    G = G + biases
    H = relu(G)
    return H

#Output weights 
output_weights = np.dot(pinv2(hidden_nodes(X_train)), y_train)

#Def prediction
def predict(X):
    out = hidden_nodes(X)
    out = np.dot(out, output_weights)
    return out

#PREDICTION
prediction = predict(X_test)
   

您在此处显示的代码中唯一的随机性来源是权重和偏差的初始化:

#weights & biases
input_weights = np.random.normal(size=[input_size,hidden_size])
biases = np.random.normal(size=[hidden_size])

因此,为了使此代码可重现,您唯一应该做的就是在 初始化您的权重和偏差之前显式设置 Numpy 随机种子:

seed = 42 # can be any number, and the exact value does not matter
np.random.seed(seed)
#weights & biases
input_weights = np.random.normal(size=[input_size,hidden_size])
biases = np.random.normal(size=[hidden_size])

与您的问题无关,但我们从不使用fit_transform(或任何包括fit在内的东西)来测试 数据 - 只需使用 transform 即可;您还应该对特征 X 和标签 y:

使用 2 个不同的缩放器
#scale data
scaler_X = MinMaxScaler()
scaler_Y = MinMaxScaler()
# fit_transform for training data:
X_train = scaler_X.fit_transform(train.values[:,1:])
y_train = scaler_Y.fit_transform(train.values[:,:1])
# only transform for test (unseen) data:
X_test = scaler_X.transform(test.values[:,1:])
y_test = scaler_Y.transform(test.values[:,:1])