【问题标题】:Difference between model_weights and optimizer_weights in keraskeras中model_weights和optimizer_weights的区别
【发布时间】:2017-10-11 14:57:25
【问题描述】:

keras 中的 model_weights 和 optimizer_weights 有什么区别。运行以下代码后model.summary 显示总共 9 个参数,这些参数显示在 1.h5 文件中的 model_weight 中。但是 optimizer_weight 显示总共 18 个参数。我只使用了 1 个 epoch。代码如下:

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
batch_size = 128
num_classes = 2
epochs = 1

# input image dimensions
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = mnist.load_data()

#Redistributing data for only two classes
x1_train=x_train[y_train==0]; y1_train=y_train[y_train==0]
x1_test=x_test[y_test==0];y1_test=y_test[y_test==0]
x2_train=x_train[y_train==1];y2_train=y_train[y_train==1]
x2_test=x_test[y_test==1];y2_test=y_test[y_test==1]
X=np.concatenate((x1_train,x2_train,x1_test,x2_test),axis=0)
Y=np.concatenate((y1_train,y2_train,y1_test,y2_test),axis=0)
# the data, shuffled and split between train and test sets
x_train, x_test, y_train, y_test = train_test_split(X,Y)

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(1, kernel_size=(2, 2),
                 activation='relu',
                 input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(16,16)))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
model.summary()
model.save('1.h5')

【问题讨论】:

    标签: keras keras-layer


    【解决方案1】:

    模型权重是作用于实际数据的权重。 它们会影响输出。

    仅一个模型(没有优化器)就足以接受输入并产生(预测)输出。模型的权重越好,输出就越好。

    训练模型的全部目的是调整其权重,使其能够做出良好的预测。

    另一方面,优化器对数据和预测没有影响。
    优化器的作用是决定如何在训练期间改变模型的权重。我纯粹出于培训目的。优化器获取梯度并决定如何将这些梯度应用于模型。 (考虑学习率、动量等)

    优化器权重只是帮助改进模型权重的调整。一旦你认为你的模型做得很好,你就可以扔掉优化器。

    【讨论】:

    • 但是在上面的例子中为什么model_weights是9而optimizer_weights是18。 (我正在使用 epoch=1)。假设我没有使用基于梯度的优化器(使用基于群的),那么我必须更新哪个权重,model_weights 或 optimizer_weights??
    • 我不知道 swarm 优化器是如何工作的......但我认为它会照顾自己的权重......你可以尝试增加你的模型,看看优化器的权重会发生什么。
    • 我明白了。唯一的困惑是,为什么models_weights和optimizer_weight的参数没有不同(对于上面的例子9和18,我从model.save得到)
    • 为什么它们应该相等?看起来这个优化器每个模型的权重有 2 个。 (但请检查此信息)。
    • 来自 keras 文档,我认为 9 个参数用于损失,9 个用于优化器。 github.com/fchollet/keras/blob/master/keras/optimizers.py#L130> 中的第 69 行
    猜你喜欢
    • 1970-01-01
    • 2018-05-05
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    相关资源
    最近更新 更多