是否可以在Keras进行多类分类问题进行持续培训

  • 本文关键字:问题 分类 培训 Keras 是否 keras
  • 更新时间 :
  • 英文 :


我被试图继续在Keras进行培训。因为我有了新的标签和值,因为我是在构建KERAS多类分类模型的。因此,我想在不进行重新培训的情况下建立一个新模型。这就是为什么我尝试在Keras进行连续火车的原因。

model.add(Dense(10, activation='sigmoid'))
model.compile(optimizer='rmsprop',
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy'])

model.fit(training_data, labels, epochs=20, batch_size=1)
model.save("keras_model.h5")

完成保存模型后,我想继续培训。所以我尝试了,

model1 = load_model("keras_model.h5")
model1.fit(new_input, new_label, epochs=20, batch_size=1)
model1.save("keras_model.h5")

我尝试过。但这是错误的。像以前的10堂课一样。但是现在我们添加新类意味着发生错误。

所以我的问题是什么,是否有可能继续在凯拉斯继续培训新课程的多类分类?

tensorflow.python.framework.errors_impl.invalidargumenterror:接收 标签值10,超出[0,9(的有效范围。标签 值:10 [[{{node 损失/dense_7_loss/sparsesoftmaxcrossentropywithlogits/sparsesoftmaxcrossentropywithlogits}]]

这种类型的情况的典型方法是定义一个包含大多数内部层的通用模型,并且可以重复使用;然后是定义输出层的第二个模型,从而定义了类的数量。内部模型可以在随后的外部模型中重复使用。

未经测试的示例:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
def make_inner_model():
  """ An example model that takes 42 features and outputs a
  transformation vector.
  """
  inp = Input(shape=(42,), name='in')
  h1 = Dense(80, activation='relu')(inp)
  h2 = Dense(40)(h1)
  h3 = Dense(60, activation='relu')(h2)
  out = Dense(32)(h3)
  return Model(inp, out)
def make_outer_model(inner_model, n_classes):
  inp = Input(shape=(42,), name='inp')
  hidden = inner_model(inp)
  out = Dense(n_classes, activation='softmax')(hidden)
  model = Model(inp, out)
  model.compile('adam', 'categorical_crossentropy')
  return model
inner_model = make_inner_model()
inner_model.save('inner_model_untrained.h5')
model1 = make_outer_model(inner_model, 10)
model1.summary()
# model1.fit()
# inner_model.save_weights('inner_model_weights_1.h5')
model2 = make_outer_model(inner_model, 12)
# model2.fit()
# inner_model.save_weights('inner_model_weights_2.h5')

最新更新