我正在尝试保存和恢复张量流模型,我得到了臭名昭着的ValueError: GraphDef cannot be larger than 2GB
。
我认为我的问题与不必要的数据有关(我只需要变量),所以我尝试通过将变量的dict交给saver来解决问题。
不幸的是我仍然遇到2G错误。 overlay.meta文件大约是325 MB。
有什么想法吗?
模型定义,会话运行和保存的代码:
batch_size = 16
patch_size = 5
depth = 16
num_hidden = 64
num_channels = 1
num_labels = 2
image_size = 64
lim_valid_test = 1000
# Input data.
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset[:lim_valid_test])
tf_test_dataset = tf.constant(test_dataset)
# Variables.
layer1_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, num_channels, depth], stddev=0.1), name='layer1_weights')
layer1_biases = tf.Variable(tf.zeros([depth]), name='layer1_biases')
layer2_weights = tf.Variable(tf.truncated_normal([patch_size, patch_size, depth, depth], stddev=0.1), name='layer2_weights')
layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]), name='layer2_biases')
layer3_weights = tf.Variable(tf.truncated_normal([image_size // 4 * image_size // 4 * depth, num_hidden], stddev=0.1), name='layer3_weights')
layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]), name='layer3_biases')
layer4_weights = tf.Variable(tf.truncated_normal([num_hidden, num_labels], stddev=0.1), name='layer4_weights')
layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]), name='layer4_biases')
# Model.
def model(data):
conv = tf.nn.conv2d(data, layer1_weights, [1, 2, 2, 1], padding='SAME')
hidden = tf.nn.relu(conv + layer1_biases)
conv = tf.nn.conv2d(hidden, layer2_weights, [1, 2, 2, 1], padding='SAME')
hidden = tf.nn.relu(conv + layer2_biases)
shape = hidden.get_shape().as_list()
reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)
return tf.matmul(hidden, layer4_weights) + layer4_biases
# Training computation.
logits = model(tf_train_dataset)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf_train_labels))
# Optimizer.
optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)
# Predictions for the training, validation, and test data.
train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(model(tf_valid_dataset))
test_prediction = tf.nn.softmax(model(tf_test_dataset))
# init saver
g_vars = {'layer1_weights':layer1_weights, 'layer1_biases':layer1_biases,
'layer2_weights':layer2_weights, 'layer2_biases':layer2_biases,
'layer3_weights':layer3_weights, 'layer3_biases':layer3_biases,
'layer4_weights':layer4_weights, 'layer4_biases':layer4_biases
}
saver = tf.train.Saver(g_vars)
num_steps = 10000
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('variables initialized')
for step in range(num_steps):
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
batch_labels = train_labels[offset:(offset + batch_size), :]
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
_, l, predictions = sess.run([optimizer, loss, train_prediction], feed_dict=feed_dict)
if (step % 50 == 0):
print('Minibatch loss at step %d: %f' % (step, l))
print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels[:lim_valid_test]))
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))
print('saving trained model')
savePath = saver.save(sess, 'overlay', global_step=1000)
print ('saved')
num_steps = 10000
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('variables initialized')
for step in range(num_steps):
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
batch_labels = train_labels[offset:(offset + batch_size), :]
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
_, l, predictions = sess.run([optimizer, loss, train_prediction], feed_dict=feed_dict)
if (step % 50 == 0):
print('Minibatch loss at step %d: %f' % (step, l))
print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), valid_labels[:lim_valid_test]))
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))
print('saving trained model')
savePath = saver.save(sess, 'overlay', global_step=1000)
print ('saved')
答案 0 :(得分:0)
我建议您尝试以下操作:
避免使用大尺寸的tf.Constant(),因为tf.Constant将存储为Graph中的Node,并且所有常量值都将打包到GrapDef原型中。我想在您的代码中
tf_valid_dataset = tf.constant(valid_dataset[:lim_valid_test])
tf_test_dataset = tf.constant(test_dataset)
应避免使用占位符
如果(1)仍然不起作用,您可以尝试使用以下代码查看哪个操作导致该错误:
graph = tf.get_default_graph()
graph.as_graph_def().ByteSize() # see whole bytes of GraphDef
graph.as_graph_def() # see nodeDef, especially notices large `attr.value.tensor_content`