【问题标题】:Keras for implement convolution neural network用于实现卷积神经网络的 Keras
【发布时间】:2017-08-06 12:12:56
【问题描述】:

我刚刚安装了 tensorflow 和 keras。我有一个简单的演示如下:

from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, nb_epoch=10, batch_size=10)
# evaluate the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

我有这个警告:

/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py:86: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(12, activation="relu", kernel_initializer="uniform", input_dim=8)` '` call to the Keras 2 API: ' + signature)
/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py:86: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(8, activation="relu", kernel_initializer="uniform")` '` call to the Keras 2 API: ' + signature)
/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py:86: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(1, activation="sigmoid", kernel_initializer="uniform")` '` call to the Keras 2 API: ' + signature)
/usr/local/lib/python2.7/dist-packages/keras/models.py:826: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`. warnings.warn('The `nb_epoch` argument in `fit` '

那么,我该如何处理呢?

【问题讨论】:

  • 警告信息的字面意思是您需要更改的内容。

标签: python deep-learning keras


【解决方案1】:

不要使用init,而是使用kernel_initializer,你应该没问题。

【讨论】:

    【解决方案2】:

    在您自己的情况下,问题在于您使用的是旧 API 版本中的参数名称。要消除此警告,在compile() 方法中,您应该使用epochs,而不是使用nb_epochs。现在,警告信息应该消失了。警告消息从字面上描述了该问题。

    Keras 的新 API 通常会自动提示您,因为每次更新都会引入越来越多的更改。但是,此警告对模型的性能没有任何影响。

    【讨论】:

      【解决方案3】:

      正如 Matias 在 cmets 中所说,这非常简单...... Keras 昨天将他们的 API 更新为 2.0 版本。显然您已经下载了那个版本,并且演示仍然使用“旧”API。 他们创建了警告,以便“旧”API 在 2.0 版中仍然可以工作,但说它会改变,所以请从现在开始使用 2.0 API。

      使代码适应 API 2.0 的方法是将所有Dense() 层的“init”参数更改为“kernel_initializer”,并将fit() 函数中的“nb_epoch”更改为“epochs”。

      from keras.models import Sequential
      from keras.layers import Dense
      import numpy
      # fix random seed for reproducibility
      seed = 7
      numpy.random.seed(seed)
      # load pima indians dataset
      dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
      # split into input (X) and output (Y) variables
      X = dataset[:,0:8]
      Y = dataset[:,8]
      # create model
      model = Sequential()
      model.add(Dense(12, input_dim=8, kernel_initializer ='uniform', activation='relu'))
      model.add(Dense(8, kernel_initializer ='uniform', activation='relu'))
      model.add(Dense(1, kernel_initializer ='uniform', activation='sigmoid'))
      # Compile model
      model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
      # Fit the model
      model.fit(X, Y, epochs=10, batch_size=10)
      # evaluate the model
      scores = model.evaluate(X, Y)
      print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
      

      这不应该抛出任何警告,它是 keras 2.0 版本的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-31
        • 2019-08-18
        • 1970-01-01
        • 2020-10-19
        • 2017-01-01
        • 1970-01-01
        • 2018-01-24
        相关资源
        最近更新 更多