2017-10-02 2 views
1

Je travaillais sur le code suivant dans tensorflow, et son fonctionnement bien, mais je ai décidé de sauvegarder ma session et le restaurer afin de prédire toutes les variables de test, je ne reçois pas d'erreurs, mais le deuxième code où je restaure la session, la sortie est toujours zéro, ce qui signifie hidden_1_layer, hidden_2_layer, hidden_3_layer et output_layer sont zéro et les valeurs sont restaurées note,tensorflow, ne pas restaurer les variables correctement

je ne suis pas en mesure de comprendre ce que je dois changer afin de sauvegarder/restaurer ma session correctement

les éléments suivants sont les codes i écrivaient:

le code de formation pour l'nn:

import os 
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' 

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 

mnist = input_data.read_data_sets("/tmp/data", one_hot=True) 

n_nodes_hl1 = 500 
n_nodes_hl2 = 500 
n_nodes_hl3 = 500 

n_classes = 10 
batch_size = 100 

x = tf.placeholder('float',[None, 784]) 
y = tf.placeholder('float') 
v1 = tf.Variable(3, name='v1') 
v2 = tf.Variable(3, name='v2') 

saver = tf.train.Saver() 

hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])), 
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))} 

hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), 
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))} 

hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])), 
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))} 

output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])), 
'biases':tf.Variable(tf.random_normal([n_classes]))} 


def neural_network_model(data): 

    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']) , hidden_1_layer['biases']) 
    l1 = tf.nn.relu(l1) 

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']) , hidden_2_layer['biases']) 
    l2 = tf.nn.relu(l2) 

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']) , hidden_3_layer['biases']) 
    l3 = tf.nn.relu(l3) 

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases'] 

    return output 

def train_neural_network(x): 
    prediction = neural_network_model(x) 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y)) 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 

    hm_epochs = 10 

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

     for epoch in range(hm_epochs): 
      epoch_loss = 0 
      for _ in range(int(mnist.train.num_examples/batch_size)): 
       epoch_x, epoch_y = mnist.train.next_batch(batch_size) 
       _, c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y}) 
       epoch_loss += c 

      print('Epoch', epoch, 'completed out of', hm_epochs,' loss:',epoch_loss) 
      saver.save(sess, "C:/Users/jack/Desktop/test/model.ckpt") 
     correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1)) 
     accuracy = tf.reduce_mean(tf.cast(correct,'float')) 
     print('Accuracy', accuracy.eval({x:mnist.test.images, y:mnist.test.labels})) 


train_neural_network(x) 

le code i wsed pour restaurer la session et prédire une valeur en utilisant l'nn je l'ai fait: merci

import os 
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' 

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 
import numpy as np 

mnist = input_data.read_data_sets("/tmp/data", one_hot=True) 

n_nodes_hl1 = 500 
n_nodes_hl2 = 500 
n_nodes_hl3 = 500 

n_classes = 10 
batch_size = 100 

x = tf.placeholder('float',[None, 784]) 
y = tf.placeholder('float') 
v1 = tf.Variable(3, name='v1') 

saver = tf.train.Saver() 

hidden_1_layer = {'weights':tf.Variable(tf.zeros([784, n_nodes_hl1])), 
'biases':tf.Variable(tf.zeros([n_nodes_hl1]))} 

hidden_2_layer = {'weights':tf.Variable(tf.zeros([n_nodes_hl1, n_nodes_hl2])), 
'biases':tf.Variable(tf.zeros([n_nodes_hl2]))} 

hidden_3_layer = {'weights':tf.Variable(tf.zeros([n_nodes_hl2, n_nodes_hl3])), 
'biases':tf.Variable(tf.zeros([n_nodes_hl3]))} 

output_layer = {'weights':tf.Variable(tf.zeros([n_nodes_hl3, n_classes])), 
'biases':tf.Variable(tf.zeros([n_classes]))} 


def neural_network_model(data): 

    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']) , hidden_1_layer['biases']) 
    l1 = tf.nn.relu(l1) 

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']) , hidden_2_layer['biases']) 
    l2 = tf.nn.relu(l2) 

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']) , hidden_3_layer['biases']) 
    l3 = tf.nn.relu(l3) 

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases'] 

    return output 

def train_neural_network(data): 
    prediction = neural_network_model(x) 


    hm_epochs = 10 

    with tf.Session() as sess: 
     sess.run(tf.initialize_all_variables()) 
     saver.restore(sess,"C:/Users/jack/Desktop/test/model.ckpt") 
     result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:data}),1))) 
     print(result) 


train_neural_network([mnist.train.images[2]])  

print([mnist.train.labels[2]]) 

pour votre aide

Répondre

1

Lorsque tf.train.Saver() est appelé, il crée un économiseur pour toutes les variables disponibles dans le graphique jusqu'à présent. est donc devrait être créé après tout le réseau est défini:

import os 
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' 

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 

mnist = input_data.read_data_sets("/tmp/data", one_hot=True) 

n_nodes_hl1 = 500 
n_nodes_hl2 = 500 
n_nodes_hl3 = 500 

n_classes = 10 
batch_size = 100 

x = tf.placeholder('float',[None, 784]) 
y = tf.placeholder('float') 
v1 = tf.Variable(3, name='v1') 
v2 = tf.Variable(3, name='v2') 

hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])), 
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))} 

hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), 
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))} 

hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])), 
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))} 

output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])), 
'biases':tf.Variable(tf.random_normal([n_classes]))} 

saver = tf.train.Saver() 

def neural_network_model(data): 

(même pour la restauration)

Vous pouvez vérifier les variables qui seront enregistrées par l'impression print(saver._var_list)

+0

cela a fonctionné, je vous remercie Pour votre aide, ce problème m'a chassé pendant 2 jours lol. mais pour rendre les choses plus claires pour moi, quand je tape saver = tf.train.Saver(), il enregistre les variables déjà déclarées mais quand je tape saver.save (sess, "C:/Users/jack/Desktop/test/model. ckpt ") met-il à jour les variables déjà sauvegardées? –

+0

@JackFarah Lorsque vous appelez 'saver = tf.train.Saver()', il crée uniquement un objet de sauvegarde et n'enregistre pas encore les variables. Toutefois, cet objet d'économiseur contient une liste de variables qui seront enregistrées ultérieurement. Cette liste contiendra toutes les variables créées avant l'appel de 'tf.train.Saver()'. Plus tard, quand 'saver.save' est appelé, toutes les variables qui sont dans la liste de l'économiseur seront sauvegardées. – BlueSun