如何在Tensorflow中存储临时变量

时间:2018-09-19 19:30:52

标签: python tensorflow

我想知道TF是否有能力在训练阶段临时存储数据?下面是一个示例:

import tensorflow as tf
import numpy as np


def loss_function(values, a, b):
    N = values.shape[0]
    i = tf.constant(0)
    values_array = tf.get_variable(
        "values", values.shape, initializer=tf.constant_initializer(values), dtype=tf.float32) # The  temporary data solution in this example
    result = tf.constant(0, dtype=tf.float32)

    def body1(i):

        op2 = tf.assign(values_array[i, 0],
                        234.0) # Here is where it should be updated. The value being assigned is actually calculated from variable a and b.

        with tf.control_dependencies([op2]):
            return i + 1

    def condition1(i): return tf.less(i, N)
    i = tf.while_loop(condition1, body1, [i])

    op1 = tf.assign(values_array[0, 0],
                    9999.0) # Here is where it should be updated

    result = result + tf.reduce_mean(values_array) # The final cost is calculated based on the entire values_array
    with tf.control_dependencies([op1]):
        return result

# The parameters we want to calculate in the end
a = tf.Variable(tf.random_uniform([1], 0, 700), name='a')
b = tf.Variable(tf.random_uniform([1], -700, 700), name='b')

values = np.ones([2, 4], dtype=np.float32)

# cost function
cost_function = loss_function(values, a, b)

# training algorithm
optimizer = tf.train.MomentumOptimizer(
    0.1, momentum=0.9).minimize(cost_function)

# initializing the variables
init = tf.global_variables_initializer()

# starting the session session
sess = tf.Session()
sess.run(init)

_, training_cost = sess.run([optimizer, cost_function])

print tf.get_collection(
    tf.GraphKeys.GLOBAL_VARIABLES, scope="values")[0].eval(session=sess)

目前,我从控制台获得的信息是:

[[ 0.98750001  0.98750001  0.98750001  0.98750001]
     [ 0.98750001  0.98750001  0.98750001  0.98750001]]

从该示例中可以得到的是(如果可以打印出临时数据):

[[ 9999.0  1.0  1.0  1.0]
     [ 234.0  1.0  1.0  1.0]]

总的来说,我想要的是cost函数根据输入的numpy 2D数组以及参数a和b计算一个临时2D数组。然后,根据临时2D阵列计算最终成本。但是我认为使用TF变量作为临时存储可能不正确...

有帮助吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的while循环永远不会运行,因为永远不会再使用i。使用tf.control_dependencies使其运行。

此外,当您似乎只想按原样添加数组时,还要添加values_array的平均值。摆脱reduce_mean以获得所需的输出。

op1 = tf.assign(values_array[0, 0], 9999.0)未完成,因为在以下control_dependencies上下文中没有任何操作。将op移至上下文,以确保分配op包含在图中。

def loss_function(values, a, b):
    N = values.shape[0]
    i = tf.constant(0)
    values_array = tf.get_variable(
        "values", values.shape, initializer=tf.constant_initializer(values), dtype=tf.float32, trainable=False)

    temp_values_array = tf.get_variable(
        "temp_values", values.shape, dtype=tf.float32)

    # copy previous values for calculations & gradients
    temp_values_array = tf.assign(temp_values_array, values_array)

    result = tf.constant(0, dtype=tf.float32)

    def body1(i):

        op2 = tf.assign(temp_values_array[i, 0],
                        234.0) # Here is where it should be updated. The value being assigned is actually calculated from variable a and b.

        with tf.control_dependencies([op2]):
            return [i+1]

    def condition1(i): return tf.less(i, N)

    i = tf.while_loop(condition1, body1, [i])

    with tf.control_dependencies([i]):
        op1 = tf.assign(temp_values_array[0, 0],
                    9999.0) # Here is where it should be updated

        with tf.control_dependencies([op1]):
            result = result + temp_values_array # The final cost is calculated based on the entire values_array

            # save the calculations for later
            op3 = tf.assign(values_array, temp_values_array)
            with tf.control_dependencies([op3]):
                return tf.identity(result)

此外,您正在获取optimizer,因此输出的未分配元素将比您期望的要小。如果这样做,您的结果将更接近

training_cost = sess.run([cost_function])
_ = sess.run([optimizer])

这将确保您在获得cost_function的结果之前不进行优化