我正在尝试从google colab中保存我的tensorflow模型,但这给了我一个错误,我不知道为什么它给出了与'无法序列化tensorflow.GraphDef类型的协议缓冲区作为序列化大小的错误有关的错误( 2897149641字节)将大于限制(2147483647字节)' 我在附上我用过的代码 另外,在下面我附上了弹出的错误
x = tf.placeholder(tf.float32, shape = [None, 4])
y_true = tf.placeholder(tf.float32, shape = [None, 4])
hidden_layer_1 = tf.layers.dense(x, 100, activation = tf.nn.relu)
hidden_layer_2 = tf.layers.dense(hidden_layer_1, 100, activation = tf.nn.relu)
output = tf.layers.dense(hidden_layer_2, 4, activation = tf.nn.sigmoid)
cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true,logits=output))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train = optimizer.minimize(cost_func)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
steps = 100
cost_train = []
cost_test = []
accu_train = []
accu_test = []
with tf.Session() as sess:
sess.run(init)
for i in range(steps):
_, c_train, pred_train = sess.run([train, cost_func, output],feed_dict={x:X_train,y_true:y_train})
_, c_test, pred_test = sess.run([train, cost_func, output],feed_dict={x:X_test,y_true:y_test})
matches_train = tf.equal(tf.argmax(pred_train,1),tf.argmax(y_train,1))
matches_test = tf.equal(tf.argmax(pred_test,1),tf.argmax(y_test,1))
acc_train = tf.reduce_mean(tf.cast(matches_train,tf.float32))
acc_test = tf.reduce_mean(tf.cast(matches_test,tf.float32))
a_train = sess.run(acc_train,feed_dict={x:X_train,y_true:y_train,})
a_test = sess.run(acc_test,feed_dict={x:X_test,y_true:y_test,})
cost_train.append(c_train)
cost_test.append(c_test)
accu_train.append(a_train)
accu_test.append(a_test)
print('Currently on step {}'.format(i))
print('TRAIN ERROR =', c_train, '\t', 'TEST ERROR =', c_test)
print('TRAIN ACCURACY =', a_train, '\t', 'TEST ACCURACY =', a_test)
print('---------------------------------------------------------------------------------------------------------------------------------------------------------')
save_path = saver.save(sess, "/content/drive/My Drive/data/model/model.ckpt")
final_pred = sess.run(output,feed_dict={x:test})
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-56-f10b5e5ffc9f> in <module>()
29 accu_test.append(a_test)
30
---> 31 save_path = saver.save(sess, "model.ckpt")
32
33 print('Currently on step {}'.format(i))
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/saver.py in save(self, sess, save_path, global_step, latest_filename, meta_graph_suffix, write_meta_graph, write_state, strip_default_attrs, save_debug_info)
1198 meta_graph_filename,
1199 strip_default_attrs=strip_default_attrs,
-> 1200 save_debug_info=save_debug_info)
1201
1202 if self._is_empty:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/saver.py in export_meta_graph(self, filename, collection_list, as_text, export_scope, clear_devices, clear_extraneous_savers, strip_default_attrs, save_debug_info)
1241 return export_meta_graph(
1242 filename=filename,
-> 1243 graph_def=ops.get_default_graph().as_graph_def(add_shapes=True),
1244 saver_def=self.saver_def,
1245 collection_list=collection_list,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in as_graph_def(self, from_version, add_shapes)
3463 """
3464 # pylint: enable=line-too-long
-> 3465 result, _ = self._as_graph_def(from_version, add_shapes)
3466 return result
3467
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _as_graph_def(self, from_version, add_shapes)
3388 with self._lock:
3389 with c_api_util.tf_buffer() as buf:
-> 3390 c_api.TF_GraphToGraphDef(self._c_graph, buf)
3391 data = c_api.TF_GetBuffer(buf)
3392 graph = graph_pb2.GraphDef()
InvalidArgumentError: Cannot serialize protocol buffer of type tensorflow.GraphDef as the serialized size (2897149641bytes) would be larger than the limit (2147483647 bytes)
答案 0 :(得分:0)
每次循环运行时,您都将matches_train/test
和acc_train/test
创建为 Tensors ,并将它们添加到图形中。将它们移出循环,或替换为numpy函数。