如何在 TensorFlow 中对非本地方法中的方法本地张量进行就地更改?

我们知道在python中,数据是按名称跨方法传递的。假设我有一个列表a,它是方法 m1() 的本地列表,我想将它传递给另一个方法并在其他方法中对其进行一些更改并保留这些更改,然后它非常简单并且可以如下进行:


def m1(a):

   a.append(5)

def m2():

   a = [1, 2, 3, 4]

   print('Before: ', a) # Output= Before: [1, 2, 3, 4]

   m1(a)

   print('After: ', a) # Output= After: [1, 2, 3, 4, 5]

m2()

如果a是张量,如何做同样的事情?我想做类似的事情


def m1(t1):

  t2 = tf.constant([[[7, 4], [8, 4]], [[2, 10], [15, 11]]])

  tf.concat([t1, t2], axis = -1)



def m2():

  t1 = tf.constant([[[1, 2], [2, 3]], [[4, 4], [5, 3]]])

  se = tf.Session()

  print('Before: ', se.run(t1)) # Output = Before: [[[1, 2], [2, 3]], [[4, 4], [5, 3]]]

  m1(t1)

  print('After: ', se.run(t1))  #Actual Output = After : [[[1, 2], [2, 3]], [[4, 4], [5, 3]]] | Desired Output = After : [[[1, 2, 7, 4], [2, 3, 8, 4]], [[4, 4, 2, 10], [5, 3, 15, 11]]]


m2()


慕雪6442864
浏览 167回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python