Tensorflow 1.3.1 . का उपयोग करना
मैंने एक तंत्रिका नेटवर्क बनाया है और इसे प्रशिक्षित किया है और अब मैं इसे tf.saved_model.simple_save(sess, export_dir, inputs, outputs)
का उपयोग करके सहेजना चाहूंगा ताकि मैं इसका उपयोग भविष्यवाणियां करने के लिए कर सकूं। इसलिए मुझे अपने मॉडल के 'इनपुट' वैरिएबल को खोजने की जरूरत है।
मेरे पास जो मॉडल है उसे इस तरह परिभाषित किया गया है:
def __call__(self, obs, reuse=False):
with tf.variable_scope(self.name) as scope:
if reuse:
scope.reuse_variables()
x = obs
x = tf.layers.dense(x, self.nb_units)
if self.layer_norm:
x = tc.layers.layer_norm(x, center=True, scale=True)
x = tf.nn.relu(x)
x = tf.layers.dense(x, self.nb_units)
if self.layer_norm:
x = tc.layers.layer_norm(x, center=True, scale=True)
x = tf.nn.relu(x)
x = tf.layers.dense(x, self.nb_actions,
kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
x = tf.nn.tanh(x)
return x
जब मैं दायरे में चर को देखता हूं (उपयोग करके):
for i in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='actor'):
print(i.name + ':\t', end=''); print(i)
मुझे समझ आ गया:
actor/dense/kernel:0: <tf.Variable 'actor/dense/kernel:0' shape=(270, 512) dtype=float32_ref>
actor/dense/bias:0: <tf.Variable 'actor/dense/bias:0' shape=(512,) dtype=float32_ref>
actor/LayerNorm/beta:0: <tf.Variable 'actor/LayerNorm/beta:0' shape=(512,) dtype=float32_ref>
actor/LayerNorm/gamma:0: <tf.Variable 'actor/LayerNorm/gamma:0' shape=(512,) dtype=float32_ref>
actor/dense_1/kernel:0: <tf.Variable 'actor/dense_1/kernel:0' shape=(512, 512) dtype=float32_ref>
actor/dense_1/bias:0: <tf.Variable 'actor/dense_1/bias:0' shape=(512,) dtype=float32_ref>
actor/LayerNorm_1/beta:0: <tf.Variable 'actor/LayerNorm_1/beta:0' shape=(512,) dtype=float32_ref>
actor/LayerNorm_1/gamma:0: <tf.Variable 'actor/LayerNorm_1/gamma:0' shape=(512,) dtype=float32_ref>
actor/dense_2/kernel:0: <tf.Variable 'actor/dense_2/kernel:0' shape=(512, 10) dtype=float32_ref>
actor/dense_2/bias:0: <tf.Variable 'actor/dense_2/bias:0' shape=(10,) dtype=float32_ref>
मैंने इसे बचाने की कोशिश की
x = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='actor/dense/kernel:0')[0]
y = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='actor/dense_2/bias:0')[0]
tf.saved_model.simple_save(sess, model_save_name, inputs={"state": x}, outputs={"action": y})
और फिर इसे लोड कर रहा है
predict_fn = predictor.from_saved_model(load_dir)
predictions = predict_fn({"state": np.zeros(270)})
लेकिन मुझे त्रुटि मिलती है
ValueError: Cannot feed value of shape (270,) for Tensor 'actor/dense/kernel:0', which has shape '(270, 512)'
मैंने इनपुट डेटा के लिए प्लेसहोल्डर परिभाषित नहीं किया है। क्या इसका मतलब यह है कि मैं इसे सहेज नहीं सकता और फिर इसे simple_save
दृष्टिकोण के माध्यम से लोड नहीं कर सकता?
1 उत्तर
जब तक मैं गलत नहीं हूँ, तब तक आप TensorFlow
में मॉडल का उपयोग नहीं करते हैं।
सबसे पहले, आपको अपना मॉडल बनाने की आवश्यकता है: इसमें प्रत्येक परत के प्रकार और आकार शामिल हैं, लेकिन इनपुट परत (प्लेसहोल्डर) भी शामिल है।
फिर, आप प्लेसहोल्डर को खिलाकर प्रशिक्षण/पूर्वानुमान कर सकते हैं।
आपके उदाहरण में, मुझे लगता है कि हर बार जब आप इसे कॉल कर रहे हों तो आप मॉडल का पुनर्निर्माण करेंगे।
लेकिन मुझे लगता है कि आपकी त्रुटि predictions = predict_fn({"state": np.zeros(270)})
से आई है। आप अपने मॉडल को एक numpy array खिला रहे हैं, लेकिन आपके मॉडल में डेटा डालने के लिए प्लेसहोल्डर नहीं है।
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।