Python 我如何防止每次使用Keras开始新的培训课程时重新启动的丢失和准确性?

Python 我如何防止每次使用Keras开始新的培训课程时重新启动的丢失和准确性?,python,keras,Python,Keras,我正在学习如何通过在Keras中创建CNN来构建图像分类器。如何防止每次开始新的培训课程时重新开始课程。下面的代码包括一些我尝试过的注释掉的代码。谢谢 我曾尝试创建检查点,但准确性和损失似乎仍在重置。我还尝试了model.save()和model.save_weights(),然后将它们加载到新模型中,但我的准确性和损失似乎仍然从一开始就存在 导入 #导入CIFAR10数据 从keras.dataset导入cifar10 #进口keras utils 将keras.utils导入为utils #

我正在学习如何通过在Keras中创建CNN来构建图像分类器。如何防止每次开始新的培训课程时重新开始课程。下面的代码包括一些我尝试过的注释掉的代码。谢谢

我曾尝试创建检查点,但准确性和损失似乎仍在重置。我还尝试了model.save()和model.save_weights(),然后将它们加载到新模型中,但我的准确性和损失似乎仍然从一开始就存在

导入
#导入CIFAR10数据
从keras.dataset导入cifar10
#进口keras utils
将keras.utils导入为utils
#导入序列模型
从keras.models导入顺序
#导入图层
从keras.layers导入致密、脱落、平坦
从keras.layers.Conv2D卷积导入,MaxPoolig2D
#规范化内核中的值
从keras.constraints导入maxnorm
#导入编译器优化器
从keras.optimizers导入新加坡元
#导入keras检查点
从keras.callbacks导入模型检查点
#进口h5py
进口h5py
最终进口
#加载cifar10数据
(x_列,y_列),(x_测试,y_测试)=cifar10.负载数据()
#将序列和测试格式化为float32并除以255.0
x_train=x_train.astype('float32')/255.0
x_test=x_test.astype('float32')/255.0
#将y_列和y_测试更改为utils分类
y\u序列=utils.to\u范畴(y\u序列)
y_检验=utils.to_分类(y_检验)
#创建标签数组
标签=[“飞机”、“汽车”、“鸟”、“猫”、“鹿”、“狗”、“青蛙”、“马”、“船”、“卡车”]
顺序模型1
模型=顺序()
add(Conv2D(filters=32,kernel\u size=(3,3),input\u shape=(32,32,3),activation='relu',padding='same',
核约束=最大范数(3)))
####添加第二个卷积层-MaxPoolig2D####
#将图像大小从32x32减小到16x16
#池大小:在输入的每个2x2部分中查找最大值
add(MaxPooling2D(池大小=(2,2)))
####展平特征####
#将矩阵转换为一维数组
model.add(展平())
####添加第三个卷积层-第一个密集层,并注入其中####
#创建预测网络
#单位:第一层512个神经元
#激活:relu以确保准确性
#核约束:最大范数
添加(密集(单位=512,激活=relu,内核约束=maxnorm(3)))
####再加上第四个卷积-退出-杀死一些神经元-防止过度拟合-仅训练####
#提高可靠性
#速率:0.5意味着杀死一半的神经元
#仅在培训期间使用
模型添加(辍学率=0.5))
####添加第五个卷积层-第二个密集层-创建10个输出,因为我们有10个类别####
#为10个类别中的每一个生成输出
#单位:10个类别=10个输出单位
#激活='softmax',因为我们正在计算10个类别(浮动)中每个类别的概率
添加(密集(单位=10,激活=softmax'))
##############################结束序列模型
##############################编译程序######################################
compile(optimizer=SGD(lr=0.01),loss='classifical\u crossentropy',metrics=['accurity'])
################################终端编译器################################
################################保存数据###################################
#保存培训数据
model.save(filepath='model.h5')
#基于最佳精度创建模型检查点
#文件路径='model.h5'
#checkpoint=ModelCheckpoint(filepath,monitor='val\u accurity',save\u best\u only='True',
#保存权重(仅保存为假,模式为最大,周期为1)
#回调\u列表=[检查点]
#节省重量
model.save_weights('model_weights.h5'))
###############################结束保存数据################################
model.fit(x=x\u序列,y=y\u序列,验证\u分割=0.1,历代=20,批量大小=32,shuffle='True')

在您的案例中,问题是您要在培训模型之前保存它。必须首先拟合进行训练的模型,然后保存模型。还随更改附加代码

from keras.datasets import cifar10

# import keras utils
import keras.utils as utils

# import Sequential model
from keras.models import Sequential

# import layers
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D

# normalizes values in kernal
from keras.constraints import maxnorm

# import compiler optimizers
from keras.optimizers import SGD

# import keras checkpoint
from keras.callbacks import ModelCheckpoint

# import h5py
import h5py



# load cifar10 data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# format trains and tests to float32 and divide by 255.0
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# change y_train and y_test to utils categorical
y_train = utils.to_categorical(y_train)
y_test = utils.to_categorical(y_test)

# create labels array
labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']



model = Sequential()

model.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=(32, 32, 3), activation='relu', padding='same',
                 kernel_constraint=maxnorm(3)))

#### add second convolution layer - MaxPooling2d ####
# decreases image size from 32x32 to 16x16
# pool_size: finds max value in each 2x2 section of input
model.add(MaxPooling2D(pool_size=(2, 2)))

#### flatten features ####
# converts matrix to a 1 dimensional array
model.add(Flatten())

#### add third convolution layer - first Dense and feed into it ####
# creates prediction network
# units: 512 neurons for first layer
# activation: relu for accuracy
# kernal_constraint: maxnorm
model.add(Dense(units=512, activation='relu', kernel_constraint=maxnorm(3)))

#### add fourth convolution later - Dropout - kills some neurons - prevents overfitting - TRAINING ONLY ####
# improves reliability
# rate: 0.5 means kill half the neurons
# only to be used while training
model.add(Dropout(rate=0.5))

#### add fifth convolution layer - Second Dense layer - Creates 10 outputs because we have 10 categories ####
# produces output for each of the 10 categories
# units: 10 categories = 10 output units
# activation = 'softmax' because we are calculating the probabilities of each of the 10 categories (floats)
model.add(Dense(units=10, activation='softmax'))

############################## END SEQUENTIAL MODEL ##########################                 

############################## COMPILER ######################################
model.compile(optimizer=SGD(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy'])


model.fit(x=x_train, y=y_train, validation_split=0.1, epochs=20, batch_size=32, shuffle='True')

################################ END COMPILER ################################

################################ SAVE DATA ###################################

# saves the training data
model.save(filepath='model.h5')

# create model checkpoint based on best accuracy
#filepath = 'model.h5'
#checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', save_best_only='True',
                             #save_weights_only='False', mode='max', period=1)

#callbacks_list = [checkpoint]

# save weights
model.save_weights('model_weights.h5')

############################### END SAVE DATA ################################

让我知道这是否有效

在您的案例中,问题是您在培训模型之前要保存模型。必须首先拟合进行训练的模型,然后保存模型。还随更改附加代码

from keras.datasets import cifar10

# import keras utils
import keras.utils as utils

# import Sequential model
from keras.models import Sequential

# import layers
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D

# normalizes values in kernal
from keras.constraints import maxnorm

# import compiler optimizers
from keras.optimizers import SGD

# import keras checkpoint
from keras.callbacks import ModelCheckpoint

# import h5py
import h5py



# load cifar10 data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# format trains and tests to float32 and divide by 255.0
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# change y_train and y_test to utils categorical
y_train = utils.to_categorical(y_train)
y_test = utils.to_categorical(y_test)

# create labels array
labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']



model = Sequential()

model.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=(32, 32, 3), activation='relu', padding='same',
                 kernel_constraint=maxnorm(3)))

#### add second convolution layer - MaxPooling2d ####
# decreases image size from 32x32 to 16x16
# pool_size: finds max value in each 2x2 section of input
model.add(MaxPooling2D(pool_size=(2, 2)))

#### flatten features ####
# converts matrix to a 1 dimensional array
model.add(Flatten())

#### add third convolution layer - first Dense and feed into it ####
# creates prediction network
# units: 512 neurons for first layer
# activation: relu for accuracy
# kernal_constraint: maxnorm
model.add(Dense(units=512, activation='relu', kernel_constraint=maxnorm(3)))

#### add fourth convolution later - Dropout - kills some neurons - prevents overfitting - TRAINING ONLY ####
# improves reliability
# rate: 0.5 means kill half the neurons
# only to be used while training
model.add(Dropout(rate=0.5))

#### add fifth convolution layer - Second Dense layer - Creates 10 outputs because we have 10 categories ####
# produces output for each of the 10 categories
# units: 10 categories = 10 output units
# activation = 'softmax' because we are calculating the probabilities of each of the 10 categories (floats)
model.add(Dense(units=10, activation='softmax'))

############################## END SEQUENTIAL MODEL ##########################                 

############################## COMPILER ######################################
model.compile(optimizer=SGD(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy'])


model.fit(x=x_train, y=y_train, validation_split=0.1, epochs=20, batch_size=32, shuffle='True')

################################ END COMPILER ################################

################################ SAVE DATA ###################################

# saves the training data
model.save(filepath='model.h5')

# create model checkpoint based on best accuracy
#filepath = 'model.h5'
#checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', save_best_only='True',
                             #save_weights_only='False', mode='max', period=1)

#callbacks_list = [checkpoint]

# save weights
model.save_weights('model_weights.h5')

############################### END SAVE DATA ################################

让我知道这是否有效

您应该在训练后保存模型,并使用
keras.models.load\u model
加载模型

请参阅以下代码段

# import CIFAR10 data
from keras.datasets import cifar10

# import keras utils
import keras.utils as utils

# import Sequential model
from keras.models import Sequential

# import layers
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D

# normalizes values in kernal
from keras.constraints import maxnorm

# import compiler optimizers
from keras.optimizers import SGD

# import keras checkpoint
from keras.callbacks import ModelCheckpoint

# import h5py
import h5py


from keras.models import load_model

# load cifar10 data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# format trains and tests to float32 and divide by 255.0
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# change y_train and y_test to utils categorical
y_train = utils.to_categorical(y_train)
y_test = utils.to_categorical(y_test)

# create labels array
labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']


model = Sequential()

model.add(Conv2D(filters=32, kernel_size=(3, 3), input_shape=(32, 32, 3), activation='relu', padding='same',
                 kernel_constraint=maxnorm(3)))

#### add second convolution layer - MaxPooling2d ####
# decreases image size from 32x32 to 16x16
# pool_size: finds max value in each 2x2 section of input
model.add(MaxPooling2D(pool_size=(2, 2)))

#### flatten features ####
# converts matrix to a 1 dimensional array
model.add(Flatten())

#### add third convolution layer - first Dense and feed into it ####
# creates prediction network
# units: 512 neurons for first layer
# activation: relu for accuracy
# kernal_constraint: maxnorm
model.add(Dense(units=512, activation='relu', kernel_constraint=maxnorm(3)))

#### add fourth convolution later - Dropout - kills some neurons - prevents overfitting - TRAINING ONLY ####
# improves reliability
# rate: 0.5 means kill half the neurons
# only to be used while training
model.add(Dropout(rate=0.5))

#### add fifth convolution layer - Second Dense layer - Creates 10 outputs because we have 10 categories ####
# produces output for each of the 10 categories
# units: 10 categories = 10 output units
# activation = 'softmax' because we are calculating the probabilities of each of the 10 categories (floats)
model.add(Dense(units=10, activation='softmax'))

############################## END SEQUENTIAL MODEL ##########################                 

############################## COMPILER ######################################
model.compile(optimizer=SGD(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy'])

################################ END COMPILER ################################

################################ SAVE DATA ###################################

model = load_model('model.h5')

# create model checkpoint based on best accuracy
#filepath = 'model.h5'
#checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', save_best_only='True',
                             #save_weights_only='False', mode='max', period=1)

#callbacks_list = [checkpoint]

# # save weights
# model.save_weights('model_weights.h5')

############################### END SAVE DATA ################################
model.fit(x=x_train, y=y_train, validation_split=0.1, epochs=1, batch_size=32, shuffle='True')

# saves the training data
model.save(filepath='model.h5')
加载保存的模型并重新训练后,损失和精度将从先前停止的值开始

44800/45000[=========================>.]-预计到达时间:0s-损失:1.9399 -acc:0.3044832/45000[===============================>预计到达时间:0s-损失:1.9398-acc:0.3044864/45000[=====================================>]- 预计到达时间:0s-损失:1.9397-附件:0.3044896/45000 [============================>。]-预计到达时间:0s-损失:1.9396-附件: 0.3044928/45000[=====================================>预计到达时间:0s-损失:1.9397-acc:0.3044960/45000[==================================>预计到达时间:0s-损失:1.9397-acc:0.3044992/45000 [============================>。]-预计到达时间:0s-损失:1.9395-附件: 0.3045000/45000[==================================================================-82s 2ms/步长-损耗:1.9395-附件:0.3030-val\ U损耗:1.7316-val\ U附件:0.3852

下一轮,

纪元1/1 32/45000[……]-预计到达时间:3:13- 损失:1.7473-会计科目:0。64/45000 [..............................] - 预计到达时间:2:15-损失:1.7321-附件:0。96/45000 [预计到达时间:1:58-损失:1.6830-附件:0。 128/45000[预计到达时间:1:48-损失:1.6729 -行政协调会:0。160/45000[预计到达时间:1:41-损失:1.6876-附件:0

但是,请注意,在