在tensorflow中,如果我们创建一个以初始值作为常量的变量,则该常量值将存储在tensorflow图中,因此您不能有较大的常量值,因为tensorflow图原型的限制为2GB。 参见例如:
import tensorflow as tf
import numpy as np
shape=(3*(10**8))
my_large_array = np.random.random_integers(low=0,high=10,size=shape)
print("Size of array in Giga bytes ",my_large_array.nbytes*(10**-9))
b= tf.Variable(name="constant_var",initial_value=my_large_array)
with tf.Session() as sess:
print(sess.run(tf.global_variables_initializer()))
print("Init done")
输出:
Size of array in Giga bytes 2.4000000000000004
....
ValueError: Cannot create a tensor proto whose content is larger than 2GB.
但是,当将具有相同值的常量初始化程序用作初始值时,不会引发任何错误。例如:
import tensorflow as tf
import numpy as np
shape=(3*(10**8))
my_large_array = np.random.random_integers(low=0,high=10,size=shape)
print("Size of array in Giga bytes ",my_large_array.nbytes*(10**-9))
A = tf.get_variable(name="initializer_var",initializer=tf.constant_initializer(value=my_large_array),shape=shape)
# b= tf.Variable(name="constant_var",initial_value=my_large_array)
with tf.Session() as sess:
print(sess.run(tf.global_variables_initializer()))
print("Init done")
print(np.array_equal(sess.run(A),my_large_array))
输出
Size of array in Giga bytes 2.4000000000000004
None
Init done
True
如果不在图中,则在第二种情况下此常量numpy数组保存在哪里?在这种情况下,变量不具有初始状态吗?