繁体   English   中英

如何在Tensorflow中存储临时变量

[英]How can I store temporary variables in 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变量作为临时存储可能不正确...

有什么帮助吗?

谢谢!

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

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

op1 = tf.assign(values_array[0, 0], 9999.0)未完成,因为在以下control_dependencies上下文中没有op。 将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的结果之前不会进行优化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM