0

我正在使用 tf.gradienttape 进行模型训练,并且成功地为每个 epoch 保存检查点。

with train_summary_writer.as_default():
  with tf.summary.record_if(True):
    for epoch in range(epochs):
      for train_id in range(train_start_id, train_end_id):
          batch_data_path= train_data_path + 'train_data_' + str(train_id).zfill(6) + ".npy"
          batch_data = np.load(data_path)
          batch_data = np.transpose(batch_data, (0, 2, 3, 1))
          x_inp = np.reshape(np.asarray(batch_data), [-1, 5, 5, 5, 3])
          train(loss, model, opt, x_inp)

          loss_values = loss(model, x_inp)
          reconstructed = np.reshape(model(x_inp), [1, sensor_n, sensor_n, scale_n])
          # if int(train_id) % 2000:      
          tf.summary.scalar('loss',loss_values, step = train_id)
          tf.summary.image('original', tf.reshape(x_inp, (step_max, sensor_n, sensor_n, scale_n)), max_outputs=10, step=train_id)
          tf.summary.image('reconstructed', reconstructed, max_outputs=10, step=train_id)
          print("Epoch: {}  /////   Step: {}/{} ===========================> Loss: {} ".format(epoch, train_id, train_end_id, loss_values))
      save_path = manager.save()
      print("Saved checkpoint for epoch {}: {}".format(epoch, save_path))
      print("loss : {}".format(loss_values.numpy()))

以下两个问题,1.如何保存这个模型?2. 我以后如何加载这个模型?

我的模型是一种自动编码器类型的模型,因此有必要创建重建模型来比较和查看错误。

4

2 回答 2

0

使用 load_model API 保存和加载模型。

model.save(model_path) 

loaded = tf.keras.models.load_model(model_path)

检查这个张量流教程

于 2020-11-24T17:20:00.717 回答
0

本指南可能会有所帮助: https ://www.tensorflow.org/guide/saved_model

tf.saved_model.save(model, "Path")
于 2020-02-09T18:47:30.633 回答