微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在TensorFlow中保存模型以用于将来的培训时遇到问题

如何解决在TensorFlow中保存模型以用于将来的培训时遇到问题

我要训练几个模型。我无法一次全部加载它们。

我要在每个文件上交替执行10个纪元。

所以我写了这段代码

def train_model(save_model):
   print('\nCreating model...\n')
   model = create_model((WINDOW_SIZE,num_of_features,1))
   model.summary()
   model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE),loss=tf.keras.losses.BinaryCrossentropy(),metrics=['accuracy',tf.keras.metrics.Precision(name='precision'),tf.keras.metrics.Recall(name='recall'),tf.keras.metrics.AUC(name='AUC'),tf.keras.metrics.TruePositives(name='tp'),tf.keras.metrics.TrueNegatives(name='tn')])


   print('\nTraining...\n')
   path,dirs,files = next(os.walk(x_files))
   numOfFiles = len(files)

   load = 0
   currIteration = 0
   callback = tf.keras.callbacks.EarlyStopping(monitor='accuracy',patience=2)

   for currBigEpoch in range(numOfFiles):

     for x_file,y_file in zip(os.listdir(x_files),os.listdir(y_files)):

       x_train = pickle.load(open(x_files+'/'+x_file,'rb'))
       y_train = pickle.load(open(y_files+'/'+y_file,'rb'))


       x_train_tf = tf.convert_to_tensor(x_train)
       x_train2 = tf.reshape(x_train_tf,x_train_tf.shape + (1,))
       y_train_tf = tf.convert_to_tensor(y_train)
    
       if load==1:
         model = tf.keras.models.load_model(PROJECT_PATH + '/tmp/model')

       history = model.fit(x_train2,y_train_tf,epochs=1,callbacks=[callback],use_multiprocessing=True,validation_split=VAL_SPLIT)#batch_size=BATCH_SIZE
      
       model.save(PROJECT_PATH + '/tmp/model')
       load = 1

但是似乎每次培训都是从头开始的。以下是一些时期的数据:

enter image description here

有什么我应该做不同的事情吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。