Tensorflow:tf.random_uniform vs tf.constant

时间:2017-08-04 18:12:10

标签: python tensorflow

所以,我一直在用张量流来耗尽内存。要查看发生了什么,我尝试添加一个简单的矩阵:C = A + B,其中AB是大小的随机矩阵(15000,15000)

我在这里使用这个非常简单的示例代码,除了我修改了一下:

https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/5_MultiGPU/multigpu_basics.py

以下是我的修改:

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 test_file.py gpu 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.

我也尝试过使用:

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)

没有运气。同样的错误。我可能做错了。

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

为什么tf.random_uniform有效但tf.constant方法不起作用?

0 个答案:

没有答案