如何在签名中修复 Tensorflow 1.14.0rc 中的 'strided slice assignment are not compatible with expected types'?
How to fix 'strided slice assignment are not compatible with expected types' in Tensorflow 1.14.0rc in autograph?
我正在尝试在 tensorflow 1.14.0 中实现滑动 DFT 算法并使用 tf.function 这样我就不必太担心控制流,但是我 运行一个问题。当我尝试将一个变量的一个元素与该变量中的另一个元素赋值时,我收到关于跨步切片赋值中不兼容类型的错误。
我试过使用 tf.scatter 更新、tf 分配和仅使用典型的切片分配,但是其中 none 有效。
@tf.function
def sdft_func(self,input_tensor):
for i in range(self.N_t):
#retrieving variables so that I have direct access to it
#instead of getting access to the read tensor
_, _, self.in_s = self.get_variables()
last = self.in_s[self.N_t-1]
for j in range(self.N_t,0,-1):
_, _, self.in_s = self.get_variables()
val = self.in_s[j-1]
#The line below gives the error
self.in_s = self.in_s[j].assign(val)
print(self.in_s)
我得到的错误如下:
TypeError: In op 'strided_slice_1/_assign', input types ([tf.complex64, tf.int32, tf.int32, tf.int32, tf.complex64]) are not compatible with expected types ([tf.complex64_ref, tf.int32, tf.int32, tf.int32, tf.complex64])
提前致谢!
我似乎将问题缩小为尝试在 tf.function 函数内设置 tf.complex64 变量。所以为了克服这个问题,我简单地抽象了操作,以便在 tf.function 函数之外完成变量设置。解决方法见下:
def sdft_func(self,input_tensor):
@tf.function
def func(input_tensor,N_t,in_s,coeffs,freqs):
in_s = tf.identity(in_s)
coeffs = tf.identity(coeffs)
freqs = tf.identity(freqs)
for i in range(N_t):
last = in_s[self.N_t-1]
in_s = in_s[:-1]
new_val = tf.expand_dims(tf.complex(input_tensor[i],
tf.cast(0.0,dtype=tf.float32)),0)
in_s = tf.concat([new_val,in_s],axis=0)
delta = in_s[0] - last
freqs_2 = tf.TensorArray(tf.complex64,size=self.N)
for j in range(self.N_t):
freqs_2 = freqs_2.write(j,(freqs[j]+delta)*coeffs[j])
freqs = freqs_2.stack()
freqs.set_shape([self.N])
return freqs,in_s
new_freqs, new_in_s = func(input_tensor,self.N_t,
self.in_s,self.coeffs,self.freqs)
self.in_s = self.in_s.assign(new_in_s)
self.freqs = self.freqs.assign(new_freqs)
我正在尝试在 tensorflow 1.14.0 中实现滑动 DFT 算法并使用 tf.function 这样我就不必太担心控制流,但是我 运行一个问题。当我尝试将一个变量的一个元素与该变量中的另一个元素赋值时,我收到关于跨步切片赋值中不兼容类型的错误。
我试过使用 tf.scatter 更新、tf 分配和仅使用典型的切片分配,但是其中 none 有效。
@tf.function
def sdft_func(self,input_tensor):
for i in range(self.N_t):
#retrieving variables so that I have direct access to it
#instead of getting access to the read tensor
_, _, self.in_s = self.get_variables()
last = self.in_s[self.N_t-1]
for j in range(self.N_t,0,-1):
_, _, self.in_s = self.get_variables()
val = self.in_s[j-1]
#The line below gives the error
self.in_s = self.in_s[j].assign(val)
print(self.in_s)
我得到的错误如下:
TypeError: In op 'strided_slice_1/_assign', input types ([tf.complex64, tf.int32, tf.int32, tf.int32, tf.complex64]) are not compatible with expected types ([tf.complex64_ref, tf.int32, tf.int32, tf.int32, tf.complex64])
提前致谢!
我似乎将问题缩小为尝试在 tf.function 函数内设置 tf.complex64 变量。所以为了克服这个问题,我简单地抽象了操作,以便在 tf.function 函数之外完成变量设置。解决方法见下:
def sdft_func(self,input_tensor):
@tf.function
def func(input_tensor,N_t,in_s,coeffs,freqs):
in_s = tf.identity(in_s)
coeffs = tf.identity(coeffs)
freqs = tf.identity(freqs)
for i in range(N_t):
last = in_s[self.N_t-1]
in_s = in_s[:-1]
new_val = tf.expand_dims(tf.complex(input_tensor[i],
tf.cast(0.0,dtype=tf.float32)),0)
in_s = tf.concat([new_val,in_s],axis=0)
delta = in_s[0] - last
freqs_2 = tf.TensorArray(tf.complex64,size=self.N)
for j in range(self.N_t):
freqs_2 = freqs_2.write(j,(freqs[j]+delta)*coeffs[j])
freqs = freqs_2.stack()
freqs.set_shape([self.N])
return freqs,in_s
new_freqs, new_in_s = func(input_tensor,self.N_t,
self.in_s,self.coeffs,self.freqs)
self.in_s = self.in_s.assign(new_in_s)
self.freqs = self.freqs.assign(new_freqs)