Python 分配张量元素困难(张量不易损坏,不能分配要调用的函数)

Python 分配张量元素困难(张量不易损坏,不能分配要调用的函数),python,numpy,tensorflow,Python,Numpy,Tensorflow,我的数据输入到5x16矩阵中,前四列是秩4 2x2x2张量的坐标,最后一列是该元素的值。我试图分配这些值的方式给了我两个错误 import numpy as np import tensorflow as tf N = 2 Z = np.array([[1, 1, 1, 1, 0.77460594], [1, 1, 1, 2, 0.2444343 ], [1, 1, 2, 1, 0.2444343 ], [1, 1, 2, 2, 0.49572458], [1, 2, 1, 1, 0.24

我的数据输入到5x16矩阵中,前四列是秩4 2x2x2张量的坐标,最后一列是该元素的值。我试图分配这些值的方式给了我两个错误

import numpy as np
import tensorflow as tf
N = 2

Z = np.array([[1, 1, 1, 1, 0.77460594], [1, 1, 1, 2, 0.2444343 ],
 [1, 1, 2, 1, 0.2444343 ], [1, 1, 2, 2, 0.49572458],
 [1, 2, 1, 1, 0.2444343 ], [1, 2, 1, 2, 0.11234285],
 [1, 2, 2, 1, 0.11234285], [1, 2, 2, 2, 0.30349089],
 [2, 1, 1, 1, 0.2444343 ], [2, 1, 1, 2, 0.11234285],
 [2, 1, 2, 1, 0.11234285], [2, 1, 2, 2, 0.30349089],
 [2, 2, 1, 1, 0.49572458], [2, 2, 1, 2, 0.30349089],
 [2, 2, 2, 1, 0.30349089], [2, 2, 2, 2, 1.05571294]],)

T = {tf.zeros(
   [N, N, N, N], dtype=tf.dtypes.float32, name=None
)}


for b in range(N ** 4):
        i = (Z[b][0] - 1)
        j = (Z[b][1] - 1)
        k = (Z[b][2] - 1)
        l = (Z[b][3] - 1)
        def T(i, j, k, l, b):
            T.ref([i][j][k][l]) = Z[b][4]
如果T后面没有.ref(),则会出现“如果启用了张量相等,则张量是不可损坏的。相反,请使用Tensor.experimental_ref()作为键”错误。它给出了一个简单的例子

T.ref([i][j][k][l]) = Z[b][4]
    ^
SyntaxError: can't assign to function call

肯定感觉我没有正确赋值,只是不确定我能做什么。

在创建变量
T
时,以及在
for
循环中赋值时,几乎没有小错误。已修复这些错误,下面是工作代码。在代码中,已将更改解释为注释

代码-

import numpy as np
import tensorflow as tf
N = 2

Z = np.array([[1, 1, 1, 1, 0.77460594], [1, 1, 1, 2, 0.2444343 ],
 [1, 1, 2, 1, 0.2444343 ], [1, 1, 2, 2, 0.49572458],
 [1, 2, 1, 1, 0.2444343 ], [1, 2, 1, 2, 0.11234285],
 [1, 2, 2, 1, 0.11234285], [1, 2, 2, 2, 0.30349089],
 [2, 1, 1, 1, 0.2444343 ], [2, 1, 1, 2, 0.11234285],
 [2, 1, 2, 1, 0.11234285], [2, 1, 2, 2, 0.30349089],
 [2, 2, 1, 1, 0.49572458], [2, 2, 1, 2, 0.30349089],
 [2, 2, 2, 1, 0.30349089], [2, 2, 2, 2, 1.05571294]],)

# Use (N, N, N, N) instead of [N, N, N, N] for shape
# Make it of tf.Variable so that we can use assign in for loop
T = tf.Variable(tf.zeros(
   (N, N, N, N), dtype=tf.dtypes.float32, name=None 
))

# Use assign for assigning the value
for b in range(N ** 4):
        i = tf.dtypes.cast((Z[b][0] - 1), tf.int32)
        j = tf.dtypes.cast((Z[b][1] - 1), tf.int32)
        k = tf.dtypes.cast((Z[b][2] - 1), tf.int32)
        l = tf.dtypes.cast((Z[b][3] - 1), tf.int32)
        T[i,j,k,l].assign(Z[b][4])

print(T)
<tf.Variable 'Variable:0' shape=(2, 2, 2, 2) dtype=float32, numpy=
array([[[[0.77460593, 0.2444343 ],
         [0.2444343 , 0.4957246 ]],

        [[0.2444343 , 0.11234285],
         [0.11234285, 0.30349088]]],


       [[[0.2444343 , 0.11234285],
         [0.11234285, 0.30349088]],

        [[0.4957246 , 0.30349088],
         [0.30349088, 1.0557129 ]]]], dtype=float32)>
输出-

import numpy as np
import tensorflow as tf
N = 2

Z = np.array([[1, 1, 1, 1, 0.77460594], [1, 1, 1, 2, 0.2444343 ],
 [1, 1, 2, 1, 0.2444343 ], [1, 1, 2, 2, 0.49572458],
 [1, 2, 1, 1, 0.2444343 ], [1, 2, 1, 2, 0.11234285],
 [1, 2, 2, 1, 0.11234285], [1, 2, 2, 2, 0.30349089],
 [2, 1, 1, 1, 0.2444343 ], [2, 1, 1, 2, 0.11234285],
 [2, 1, 2, 1, 0.11234285], [2, 1, 2, 2, 0.30349089],
 [2, 2, 1, 1, 0.49572458], [2, 2, 1, 2, 0.30349089],
 [2, 2, 2, 1, 0.30349089], [2, 2, 2, 2, 1.05571294]],)

# Use (N, N, N, N) instead of [N, N, N, N] for shape
# Make it of tf.Variable so that we can use assign in for loop
T = tf.Variable(tf.zeros(
   (N, N, N, N), dtype=tf.dtypes.float32, name=None 
))

# Use assign for assigning the value
for b in range(N ** 4):
        i = tf.dtypes.cast((Z[b][0] - 1), tf.int32)
        j = tf.dtypes.cast((Z[b][1] - 1), tf.int32)
        k = tf.dtypes.cast((Z[b][2] - 1), tf.int32)
        l = tf.dtypes.cast((Z[b][3] - 1), tf.int32)
        T[i,j,k,l].assign(Z[b][4])

print(T)
<tf.Variable 'Variable:0' shape=(2, 2, 2, 2) dtype=float32, numpy=
array([[[[0.77460593, 0.2444343 ],
         [0.2444343 , 0.4957246 ]],

        [[0.2444343 , 0.11234285],
         [0.11234285, 0.30349088]]],


       [[[0.2444343 , 0.11234285],
         [0.11234285, 0.30349088]],

        [[0.4957246 , 0.30349088],
         [0.30349088, 1.0557129 ]]]], dtype=float32)>


希望这能回答你的问题。愉快学习。

在创建变量
T
时,以及在
for
循环中赋值时,几乎没有小错误。已修复这些错误,下面是工作代码。在代码中,已将更改解释为注释

代码-

import numpy as np
import tensorflow as tf
N = 2

Z = np.array([[1, 1, 1, 1, 0.77460594], [1, 1, 1, 2, 0.2444343 ],
 [1, 1, 2, 1, 0.2444343 ], [1, 1, 2, 2, 0.49572458],
 [1, 2, 1, 1, 0.2444343 ], [1, 2, 1, 2, 0.11234285],
 [1, 2, 2, 1, 0.11234285], [1, 2, 2, 2, 0.30349089],
 [2, 1, 1, 1, 0.2444343 ], [2, 1, 1, 2, 0.11234285],
 [2, 1, 2, 1, 0.11234285], [2, 1, 2, 2, 0.30349089],
 [2, 2, 1, 1, 0.49572458], [2, 2, 1, 2, 0.30349089],
 [2, 2, 2, 1, 0.30349089], [2, 2, 2, 2, 1.05571294]],)

# Use (N, N, N, N) instead of [N, N, N, N] for shape
# Make it of tf.Variable so that we can use assign in for loop
T = tf.Variable(tf.zeros(
   (N, N, N, N), dtype=tf.dtypes.float32, name=None 
))

# Use assign for assigning the value
for b in range(N ** 4):
        i = tf.dtypes.cast((Z[b][0] - 1), tf.int32)
        j = tf.dtypes.cast((Z[b][1] - 1), tf.int32)
        k = tf.dtypes.cast((Z[b][2] - 1), tf.int32)
        l = tf.dtypes.cast((Z[b][3] - 1), tf.int32)
        T[i,j,k,l].assign(Z[b][4])

print(T)
<tf.Variable 'Variable:0' shape=(2, 2, 2, 2) dtype=float32, numpy=
array([[[[0.77460593, 0.2444343 ],
         [0.2444343 , 0.4957246 ]],

        [[0.2444343 , 0.11234285],
         [0.11234285, 0.30349088]]],


       [[[0.2444343 , 0.11234285],
         [0.11234285, 0.30349088]],

        [[0.4957246 , 0.30349088],
         [0.30349088, 1.0557129 ]]]], dtype=float32)>
输出-

import numpy as np
import tensorflow as tf
N = 2

Z = np.array([[1, 1, 1, 1, 0.77460594], [1, 1, 1, 2, 0.2444343 ],
 [1, 1, 2, 1, 0.2444343 ], [1, 1, 2, 2, 0.49572458],
 [1, 2, 1, 1, 0.2444343 ], [1, 2, 1, 2, 0.11234285],
 [1, 2, 2, 1, 0.11234285], [1, 2, 2, 2, 0.30349089],
 [2, 1, 1, 1, 0.2444343 ], [2, 1, 1, 2, 0.11234285],
 [2, 1, 2, 1, 0.11234285], [2, 1, 2, 2, 0.30349089],
 [2, 2, 1, 1, 0.49572458], [2, 2, 1, 2, 0.30349089],
 [2, 2, 2, 1, 0.30349089], [2, 2, 2, 2, 1.05571294]],)

# Use (N, N, N, N) instead of [N, N, N, N] for shape
# Make it of tf.Variable so that we can use assign in for loop
T = tf.Variable(tf.zeros(
   (N, N, N, N), dtype=tf.dtypes.float32, name=None 
))

# Use assign for assigning the value
for b in range(N ** 4):
        i = tf.dtypes.cast((Z[b][0] - 1), tf.int32)
        j = tf.dtypes.cast((Z[b][1] - 1), tf.int32)
        k = tf.dtypes.cast((Z[b][2] - 1), tf.int32)
        l = tf.dtypes.cast((Z[b][3] - 1), tf.int32)
        T[i,j,k,l].assign(Z[b][4])

print(T)
<tf.Variable 'Variable:0' shape=(2, 2, 2, 2) dtype=float32, numpy=
array([[[[0.77460593, 0.2444343 ],
         [0.2444343 , 0.4957246 ]],

        [[0.2444343 , 0.11234285],
         [0.11234285, 0.30349088]]],


       [[[0.2444343 , 0.11234285],
         [0.11234285, 0.30349088]],

        [[0.4957246 , 0.30349088],
         [0.30349088, 1.0557129 ]]]], dtype=float32)>


希望这能回答你的问题。快乐学习。

我以前从未与TensorFlow合作过。循环中的函数定义是常见的习惯用法吗?我觉得这很奇怪。对不起,你想通过这样定义
T
得到什么:
T={tf.zero([N,N,N,N],dtype=tf.dtypes.float32,name=None)}
?@anroesti我怀疑这是否常见,因为它可能是错的。我的想法是用for循环给出的值填充这5个变量,并为T.The.ref()中的矩阵元素赋值是在打字错误后出现的,说它不易损坏,无法使用that@Anwarvic在这里,我只是想创建一个空的张量,然后通过循环填充,因为在倒数第二行中,您实际上正在定义一个名为T的新函数。也许可以删除倒数第二行,并将最后一行替换为
T[I][j][k][l]=Z[b][4] 
?我以前没有使用过TensorFlow。循环中的函数定义是一种常见的习惯用法吗?对我来说它看起来很奇怪。很抱歉,通过这样定义
t
你想得到什么:
t={tf.zeros([N,N,N,N],dtype=tf.dtypes.float32,name=None)}
?@anroesti我怀疑它是否常见,因为它可能是错误的。我的想法是用for循环给出的值填充这5个变量,并在T.the.ref()中为该矩阵元素赋值是在打字错误后出现的,说它不易损坏,无法使用that@Anwarvic在这里,我只是想创建一个空的张量,然后通过循环填充,因为在倒数第二行中,您实际上正在定义一个名为T的新函数。也许可以删除倒数第二行,并将最后一行替换为
T[I][j][k][l]=Z[b][4] 
?@CriminyMorel-希望我们已经回答了您的问题。如果您对答案感到满意,请接受并投票。@CriminyMorel-希望我们已经回答了您的问题。如果您对答案感到满意,请接受并投票。