KerasRegressor每次运行时都会给出不同的输出(尽管输入和训练集相同)

时间:2018-01-27 14:35:32

标签: machine-learning keras

每当我运行以下代码时,我会不断获得不同的输出。请有人帮我解决这个问题吗?代码:

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.preprocessing import StandardScaler

import numpy as ny

X = ny.array([[1,2], [3,4], [5,6], [7,8], [9,10]])
sc_X=StandardScaler()
X_train = sc_X.fit_transform(X)

Y = ny.array([3, 4, 5, 6, 7])
Y=ny.reshape(Y,(-1,1))
sc_Y=StandardScaler()
Y_train = sc_Y.fit_transform(Y)

N = 5

def brain():
    #Create the brain
    br_model=Sequential()
    br_model.add(Dense(3, input_dim=2, kernel_initializer='normal',activation='relu'))
    br_model.add(Dense(2, kernel_initializer='normal',activation='relu'))
    br_model.add(Dense(1,kernel_initializer='normal'))

    #Compile the brain
    br_model.compile(loss='mean_squared_error',optimizer='adam')
    return br_model


estimator = KerasRegressor(build_fn=brain, epochs=1000, batch_size=5,verbose=0)

estimator.fit(X_train,Y_train)
prediction = estimator.predict(X_train)


print Y
print sc_Y.inverse_transform(prediction)

基本上,我已经宣布了一个数据集,我正在训练神经网络对其进行回归并预测这些值。鉴于所有内容都已经硬编码到代码中,我每次运行时都必须获得相同的输出。然而,这种情况并非如此。我请你帮帮我。

0 个答案:

没有答案