Сохранение прогресса обучения нейронной сети в Tensorflow - PullRequest
0 голосов
/ 10 июня 2019

В Python, Tensorflow: я обучил и применил нейронную сеть, используя Tensorflow, теперь мне нужно сохранить его прогресс, чтобы обучить его в более поздний момент времени.

Я прошел много конфигураций, используя

saver = tf.train.Saver()
saver.restore()
tf.train.import_meta_graph('model/model.ckpt.meta')
tf.train.export_meta_graph('model/model.ckpt.meta')

и т.д ...

но всегда выдает ошибку.

Вот мой код. Он похож на примеры кода Mnist, но использует сгенерированный пользователем вход и имеет один непрерывный выходной нейрон.

x = tf.placeholder('float', [None, 4000]) # 4000 is my structure, just an example
y = tf.placeholder('float') # And I need a single, continuous output

def train_neural_network(x):

    testdata_images, testdata_labels = generate_training_data_batch(size = 10) 
    #Generates test data

    data=[]
    for i in range(how_many_batches):
        data.append(generate_training_data_batch(size = 10))
    #Generates training data


    prediction = neural_network_model(x) 
    # neural_network_model() is defined as a 4000x15x15x10x1 neural network
    cost = tf.reduce_mean( tf.square( tf.subtract(y, prediction) ) )
    optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)

    hm_epochs = 10 
    saver = tf.train.Saver()

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for epoch in range(hm_epochs):
            epoch_loss = 0

            for i in range(how_many_batches):
                epoch_x, epoch_y = data[i]

                _, c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y})
                epoch_loss += c


                accuracy1 = tf.subtract(y, prediction)
                result = sess.run(accuracy1, feed_dict={x: epoch_x, y: epoch_y})

                print(result)
                # This is just here so I can see what is going on

        saver.save(sess, 'model/model.ckpt')
        tf.train.export_meta_graph('model/model.ckpt.meta')

    tf.reset_default_graph()

Позже в том же файле я хочу использовать сохраненную нейронную сеть, чтобы сделать с ней некоторые прогнозы:

train_neural_network(x)

X, Y = generate_training_data_batch(size = 1)

prediction = neural_network_model(x)

with tf.Session() as sess:

    tf.train.import_meta_graph('model/model.ckpt.meta')
    sess.run(tf.global_variables_initializer())

    thought = sess.run(prediction, feed_dict={x: X})

    print(Y, thought)

С этой версией я получаю сообщение об ошибке

ValueError: Tensor("Variable:0", shape=(4000, 15), dtype=float32_ref) must be from the same graph as Tensor("Placeholder_35:0", shape=(?, 4000), dtype=float32).

Я также получил сообщения об ошибках типа

ValueError: At least two variables have the same name: Variable/Adam

Я ищу решение этой проблемы уже несколько недель, поэтому я был бы очень рад наконец разобраться с этим.

...