2

I have code that trains a DNN network. I don't want to train this network every time, because it uses too much time. How can I save the model?

def train_model(filename, validation_ratio=0.):
    # define model to be trained
    columns = [tf.contrib.layers.real_valued_column(str(col),
                                                    dtype=tf.int8)
               for col in FEATURE_COLS]
    classifier = tf.contrib.learn.DNNClassifier(
        feature_columns=columns,
        hidden_units=[100, 100],
        n_classes=N_LABELS,
        dropout=0.3)

    # load and split data
    print( 'Loading training data.')
    data = load_batch(filename)
    overall_size = data.shape[0]
    learn_size = int(overall_size * (1 - validation_ratio))
    learn, validation = np.array_split(data, [learn_size])
    print( 'Finished loading data. Samples count = {}'.format(overall_size))

    # learning
    print( 'Training using batch of size {}'.format(learn_size))
    classifier.fit(input_fn=lambda: pipeline(learn),
                   steps=learn_size)

    if validation_ratio > 0:
        validate_model(classifier, learn, validation)

    return classifier

After running this function, I get a DNNClassifier which I want to save.

Justin
  • 24,288
  • 12
  • 92
  • 142

1 Answers1

1

I believe this has already been answered here: Tensorflow: how to save/restore a model?

saver = tf.train.Saver()
saver.save(sess, 'my_test_model',global_step=1000)

(code copied from that question's answer)

J.D
  • 425
  • 4
  • 8
  • 19