2017-03-03 7 views
0

meine Codes ist wie folgt speichern und wiederherzustellen:Ich kann nicht eine Variable mit Platzhalter in tensorflow dank

import tensorflow as tf 
import numpy as np 
def add_layer(input): 
    v2 = tf.Variable(tf.random_normal([2, 2], dtype=tf.float32, name='v2')) 
    tf.add_to_collection('h0_v2',v2) 
    output=tf.matmul(input,v2) 
    return output 
x1=tf.placeholder(tf.float32) 
outputs=add_layer(x) 
tf.add_to_collection('outputs', outputs) 
saver = tf.train.Saver() 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    x1=np.random.random([2, 2]) 
    print(sess.run(outputs,feed_dict={x:x1})) 
    save_path = saver.save(sess, './model.ckpt') 
    print("model saved in file:", save_path) 

und dann wird eine weitere Codes runned:

import tensorflow as tf 
import numpy as np 
sess = tf.Session() 
saver = tf.train.import_meta_graph('./model.ckpt.meta') 
saver.restore(sess, tf.train.latest_checkpoint('./')) 
x2=np.random.random([2, 2]) 
print(sess.run(tf.get_collection('outputs',feed_dict={x:x2}))) 
print('model is loaded') 
sess.close() 

und dann sagt mir der Computer, dass das 'x' nicht definiert ist, ich weiß nicht, was falsch ist.

+0

Aber der erste Code ohne Fehler ausgeführt wird? – CrisH

Antwort

0

würde ich sagen, wie dies zu tun:

import tensorflow as tf 
import numpy as np 
sess = tf.Session() 
saver = tf.train.Saver() 
saver.restore(sess, './model.ckpt') 
x2=np.random.random([2, 2]) 
print(sess.run(tf.get_collection('outputs',feed_dict={x:x2}))) 
print('model is loaded') 
sess.close() 

Ich fand dies auf tensorflow Webseite. Ich hoffe es hilft.

+0

vielen Dank! – quan

+0

Es funktioniert jetzt? :) – CrisH

+0

ja, aber wenn ich es zweimal ausführen, wird ein Fehler ausgelöst werden, sagte: ValueError: Mindestens zwei Variablen haben den gleichen Namen: Variable/Adadelta_1. – quan

0

fand ich eine Methode, das Problem zu lösen:

import tensorflow as tf 
import numpy as np 
def add_layer(input): 
    #v1 = tf.Variable(np.random.random([2, 2]), dtype=tf.float32, name='v1') 
    v2 = tf.Variable(tf.random_normal([2, 2], dtype=tf.float32, name='v2')) 
    tf.add_to_collection('h0_v2',v2) 
    output=tf.matmul(input,v2) 
    return output 
x=tf.placeholder(tf.float32) 
outputs=add_layer(x) 
saver = tf.train.Saver() 
sess = tf.Session() 
saver = tf.train.import_meta_graph('./model.ckpt.meta') 
saver.restore(sess, tf.train.latest_checkpoint('./')) 
x2=np.random.random([2, 2]) 
print(sess.run(outputs,feed_dict={x:x2})) 
print('model is loaded') 
sess.close() 
Verwandte Themen