Python 如何使用图中的旧值用新值更新张量?

Python 如何使用图中的旧值用新值更新张量?,python,python-3.x,tensorflow,tensor,Python,Python 3.x,Tensorflow,Tensor,我对tensorflow很陌生。我想用张量的旧值来计算新值tf.assign仅适用于tf.Variables。我不知道如何实现张量运算 下面的代码不是实际的代码片段,但其思想是相同的 data.csv inp1 inp2 288.15 288.15 289.87912 303.10137 291.60825 318.05275 292.90509 329.26628 294.20194 340.47981 2

我对tensorflow很陌生。我想用张量的旧值来计算新值
tf.assign
仅适用于
tf.Variables
。我不知道如何实现张量运算

下面的代码不是实际的代码片段,但其思想是相同的

data.csv
inp1            inp2
288.15          288.15
289.87912       303.10137
291.60825       318.05275
292.90509       329.26628
294.20194       340.47981
295.75815       353.93605
297.31436       367.39229
298.87057       380.84852
300.42679       394.30476
301.983         407.761

在培训期间,我希望
op=tf.add(tf.multiply(PREVop,inp2),inp1)
为每个样本使用先前的
op


任何建议都将不胜感激。

由于
op
的值总是在变化,您可以使用
tf.Variable()
在每次迭代后存储其值。这里,
tf.Variable()
在开始时用零张量初始化

import tensorflow as tf
import numpy as np

inp1 = tf.placeholder(tf.float32, [None, 1], name="inp1")
inp2 = tf.placeholder(tf.float32, [None, 1], name="inp2")

PREVop = tf.Variable(tf.zeros([2, 1]), tf.float32)

out = tf.add(tf.multiply(PREVop, inp2), inp1) 
PREVop = tf.assign(PREVop, out)  

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for i in range(3):
        res, var = sess.run([out, PREVop], feed_dict={inp1:np.random.rand(2, 1), inp2:np.random.rand(2, 1)})
        print('out operation result: \n{}'.format(res))
        print('PREVop value after assigning: \n{}'.format(var))
        print(20*'-')
输出:

out operation result: 
[[0.86163723]
 [0.7938016 ]]
PREVop value after assigning: 
[[0.86163723]
 [0.7938016 ]]
--------------------
out operation result: 
[[0.5666107]
 [0.9492748]]
PREVop value after assigning: 
[[0.5666107]
 [0.9492748]]
--------------------
out operation result: 
[[0.89638215]
 [0.93310213]]
PREVop value after assigning: 
[[0.89638215]
 [0.93310213]]
--------------------
更新:因此,您需要使用
tf.add(inp1,10)
初始化
PREVop
,然后使用
op
的值更新它,即
tf.add(tf.multiply(PREVop,inp2),inp1)
。我正在添加一种方法来实现这一点,但老实说,我不喜欢它

代码:


在上面的代码中,
batch_size=2
,我使用了一个占位符
inp3
,并将值
inp1+10
,该值稍后分配给变量
PREVop
。这种情况在开始时只发生过一次,后来
PREVop
被赋值为
out
(在下面的代码中)。

感谢您的回答。然而,当我尝试用另一个张量初始化变量时,我得到了一些错误。我已经更新了代码片段供您复制。我遗漏了什么吗?告诉我一件事
PREVop的初始值是0?
没有,PREVop的初始值来自上面提到的另一个张量op。这只是数字,我已经为您提供了示例。
out operation result: 
[[0.86163723]
 [0.7938016 ]]
PREVop value after assigning: 
[[0.86163723]
 [0.7938016 ]]
--------------------
out operation result: 
[[0.5666107]
 [0.9492748]]
PREVop value after assigning: 
[[0.5666107]
 [0.9492748]]
--------------------
out operation result: 
[[0.89638215]
 [0.93310213]]
PREVop value after assigning: 
[[0.89638215]
 [0.93310213]]
--------------------
batch_size = 2

inp1 = tf.placeholder(tf.float32, [None, 1],name="inp1")
inp2 = tf.placeholder(tf.float32, [None, 1],name="inp2")

inp3 = tf.placeholder(tf.float32, [None, 1], name="inp3")
PREVop = tf.Variable(tf.zeros([batch_size, 1]), dtype=tf.float32)
PREVop = tf.assign(PREVop, inp3)

out = tf.add(tf.multiply(PREVop, inp2), inp1)  

inp = pd.read_csv("data.csv", sep=' ')
x_train = inp.iloc[:,:-1]
training_steps = 100

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    out_res = 0
    for step in range(training_steps):
        total_batch = len(inp)//batch_size
        for i in range(total_batch):
            batch_x = x_train[i*batch_size:min((i+1)*batch_size, len(inp))]

            if step==0 and i==0:
                _, res = sess.run([PREVop, out], feed_dict={inp1: batch_x['inp1'].values.reshape(2, 1), 
                                                            inp2: batch_x['inp2'].values.reshape(2, 1), 
                                                            inp3: batch_x['inp1'].values.reshape(2, 1)+10})   
            else:
                _, res = sess.run([PREVop, out], feed_dict={inp1: batch_x['inp1'].values.reshape(2, 1), 
                                                            inp2: batch_x['inp2'].values.reshape(2, 1), 
                                                            inp3: out_res})
            out_res = res