首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Keras:对于完全相同的数据和架构,训练性能是不同的。唯一的区别是使用.Sequential()或.Model()

Keras:对于完全相同的数据和架构,训练性能是不同的。唯一的区别是使用.Sequential()或.Model()
EN

Stack Overflow用户
提问于 2019-06-19 10:55:17
回答 1查看 401关注 0票数 0

下面的模型来自Keras website,它的行为完全符合预期。它是用keras.models.Sequential()定义的。我希望将其转换为使用keras.models.Model()定义,以使其更灵活以供将来使用。但在我转换后,性能直线下降。

您可以在Keras网站上找到原始模型:

代码语言:javascript
运行
复制
def build_model():
  model = Sequential([
    layers.Dense(64, activation=tf.nn.relu, input_shape=[len(train_dataset.keys())]),
    layers.Dense(64, activation=tf.nn.relu),
    layers.Dense(1)
  ])

  optimizer = keras.optimizers.Adam()
  model.compile(loss='mean_squared_error',
                optimizer=optimizer,
                metrics=['mean_absolute_error', 'mean_squared_error'])
  return model

model = build_model()
model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_22 (Dense)             (None, 64)                640       
_________________________________________________________________
dense_23 (Dense)             (None, 64)                4160      
_________________________________________________________________
dense_24 (Dense)             (None, 1)                 65        
=================================================================
Total params: 4,865
Trainable params: 4,865
Non-trainable params: 0
_________________________________________________________________

下面的代码是我的转换:

代码语言:javascript
运行
复制
def build_model_base():
  input = Input(shape=[len(train_dataset.keys())])
  x = Dense(64, activation='relu', name="dense1")(input)   
  x = Dense(64, activation='relu', name="dense2")(x)
  output = Dense(1, activation='sigmoid', name='output')(x)
  model = Model(input=[input], output=[output])
  optimizer = keras.optimizers.Adam()

  model.compile(loss='mean_squared_error', 
                optimizer=optimizer,
                metrics=['mean_absolute_error', 'mean_squared_error'])
  return model

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_18 (InputLayer)        (None, 9)                 0         
_________________________________________________________________
dense1 (Dense)               (None, 64)                640       
_________________________________________________________________
dense2 (Dense)               (None, 64)                4160      
_________________________________________________________________
output (Dense)               (None, 1)                 65        
=================================================================
Total params: 4,865
Trainable params: 4,865
Non-trainable params: 0

我能看到的唯一区别是.Sequential没有计算input layer,而.Model计算了它,但我不相信它们会使模型结构有所不同。但是,.Sequential的性能是:

而我转换的.Model()的性能是:

谁能告诉我我做错了什么?

其他一些上下文:

我读过这个post,但我的代码都是在谷歌Colab的中央处理器上运行的

print(keras.__version__) # 2.0.4

print(tf.__version__) #1.14.0-rc1

绘制损失的代码:

代码语言:javascript
运行
复制
def plot_history(history):
  hist = pd.DataFrame(history.history)
  hist['epoch'] = history.epoch

  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Abs Error [MPG]')
  plt.plot(hist['epoch'], hist['mean_absolute_error'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mean_absolute_error'],
           label = 'Val Error')
  y_max = max(hist['val_mean_absolute_error'])
  plt.ylim([0,y_max])
  plt.legend()

  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Square Error [$MPG^2$]')
  plt.plot(hist['epoch'], hist['mean_squared_error'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mean_squared_error'],
           label = 'Val Error')
  y_max = max(hist['val_mean_squared_error'])
  plt.ylim([0,y_max])
  plt.legend()
  plt.show() 

训练模型的代码(两个模型完全相同):

代码语言:javascript
运行
复制
his_seq = model.fit(normed_train_data.values, train_labels.values,
          batch_size=128,
          validation_split = 0.1,
          epochs = 100,
          verbose=0)
plot_history(his_seq)

欢迎提出任何建议!

EN

Stack Overflow用户

回答已采纳

发布于 2019-06-19 11:16:05

默认情况下,在keras的密集层以及您构建的顺序模型的输出层中使用“线性”激活函数。

但是您在转换中将激活函数指定为'sigmoid‘,这可能会有所不同。

以下是Keras提供的默认激活函数的说明:

代码语言:javascript
运行
复制
activation: Activation function to use (see activations). If you don't specify anything, no activation is applied (ie. "linear" activation: a(x) = x).
票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56659428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档