Python 张量流:tf.随机均匀vs.tf.常数

Python 张量流:tf.随机均匀vs.tf.常数,python,tensorflow,Python,Tensorflow,所以,我一直在用tensorflow耗尽内存。为了了解发生了什么,我尝试做一个简单的矩阵加法:C=a+B,其中a和B是大小随机的矩阵(1500015000) 我在这里使用了这个非常简单的示例代码,只是对它做了一些修改: 这是我的修改: import sys import numpy as np import tensorflow as tf from datetime import datetime device_name = sys.argv[1] # Choose device fr

所以,我一直在用tensorflow耗尽内存。为了了解发生了什么,我尝试做一个简单的矩阵加法:
C=a+B
,其中
a
B
是大小随机的矩阵(1500015000)

我在这里使用了这个非常简单的示例代码,只是对它做了一些修改:

这是我的修改:

import sys 
import numpy as np
import tensorflow as tf
from datetime import datetime

device_name = sys.argv[1]  # Choose device from cmd line. Options: gpu or cpu
shape = int(sys.argv[2])
if device_name == "gpu":
    device_name = "/gpu:0"
else:
    device_name = "/cpu:0"


with tf.device(device_name):
    a = np.random.rand(shape,shape)
    b = np.random.rand(shape,shape)
    sum_operation = tf.add(a,b)


startTime = datetime.now()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
    result = session.run(sum_operation)
    print(result)

# It can be hard to see the results on the terminal with lots of output -- add some newlines to improve readability.
print("\n" * 5)
print("Shape:", shape, "Device:", device_name)
print("Time taken:", datetime.now() - startTime)

print("\n" * 5)
我运行了:
python测试文件.pygpu 15000

你认为我为什么会收到这个错误

Traceback (most recent call last):
  File "test_file.py", line 28, in <module>
    result = session.run(sum_operation)
  File "/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1139, in _do_call
    return fn(*args)
  File "/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1117, in _run_fn
    self._extend_graph()
  File "/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1161, in _extend_graph
    add_shapes=self._add_shapes)
  File "/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2324, in _as_graph_def
    raise ValueError("GraphDef cannot be larger than 2GB.")
ValueError: GraphDef cannot be larger than 2GB.
不走运。同样的错误。我可能做错了什么

但具体来说——我试图将无速度与张量流速度与我的一些更复杂的计算(数据集大得多)进行比较。所以,我最终尝试将相同的随机矩阵输入到theano和tensorflow,我猜这种方法不起作用


为什么
tf.random\u uniform
有效,而
tf.constant
方法无效?

tf.constant将值嵌入到图形(2GB)限制中,而tf.random\u uniform使用您拥有的任何内存(无限制)动态分配它。tf.constant将值嵌入到图形(2GB)限制中,而tf.random_uniform使用您拥有的任何内存动态分配它(没有限制)
with tf.device(device_name):
    a = np.random.rand(shape,shape)
    b = np.random.rand(shape,shape)
    A = tf.constant(a)
    B = tf.constant(b)
    sum_operation = tf.add(A,B)