使用训练集(如测试集)时,模型无法正常运行

时间:2020-06-29 02:53:39

标签: keras deep-learning model

我正在学习Keras的回归问题。我必须构建一个模型以在输入(大小20)和输出(大小10)之间进行映射。训练集有500个样本,并且是一个文件* .mat,具有两个成分“ In.mat”(500x20)和“ Out.mat”(500x10)。矩阵的所有元素都在(0,1)中。我使用下面的代码,它的损失很小([0.000929321744479239,0.000929321744479239])。从理论上讲,这意味着该模型可以在训练集上很好地工作。但是,当我使用模型预测训练集(相同的输入“ In.mat”)时,得到的结果(“ Out_test.mat”)与实际的输出(“ Out.mat”)完全不同。甚至有负数。那我哪里错了?即使我在代码中放入“ from keras.constraints import nonneg”,为什么还有负数?也许原因是我在最后一层使用了“线性”激活。我认为“ Sigmoid”不适用于回归问题。我还认为少量样本只会影响测试集,而不会影响训练集。请帮帮我。

from keras.layers import Dense, Layer, Activation
from keras.models import Model, Sequential,load_model
from keras import optimizers, metrics
from keras.constraints import nonneg
from tensorflow.keras.callbacks import EarlyStopping, History


np.random.seed(0)

mat_contents = sio.loadmat('train_set.mat')

# Input of network
In = mat_contents['In']

# Output of network
Out = mat_contents['Out']

# Initializer
K_initializer = 'random_normal'
B_initializer = 'random_uniform'

# Neural network configuration
model = Sequential()
model.add(Dense(512, activation='relu', name='layer1', input_dim=20, kernel_initializer=K_initializer,bias_initializer=B_initializer))
model.add(Dense(256, activation='relu', name='layer2', kernel_initializer=K_initializer, bias_initializer=B_initializer))
model.add(Dense(128, activation='relu', name='layer3', kernel_initializer=K_initializer, bias_initializer=B_initializer))
model.add(Dense(64, activation='relu', name='layer4', kernel_initializer=K_initializer, bias_initializer=B_initializer))
model.add(Dense(32, activation='relu', name='layer5', kernel_initializer=K_initializer, bias_initializer=B_initializer))
model.add(Dense(10, activation='linear', name='layer6', trainable=False))

print(model.summary())

# Optimizer
adam = optimizers.Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)

# Callback
history = History()

# Compile
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mse']) #mean_squared_logarithmic_error

# Fit
model.fit(In, Out, validation_split=0.1, epochs=100, batch_size=10, shuffle=True, callbacks=[history])

# Evaluate 
print('Evaluate: ',model.evaluate(In,Out))

# Save
model.save('model.h5')

# Predict the training set
mat_test = sio.loadmat('train_set.mat')

In_test = mat_test['In']

Out_test = model.predict(In_test)
sio.savemat('Result.mat', {'Out_test': Out_test})

这里的数字 Out.mat and Out_test.mat

0 个答案:

没有答案