为什么会发生此错误:“仅对变量支持切片分配" [英] why does this error happens:"Sliced assignment is only supported for variables"

查看:90
本文介绍了为什么会发生此错误:“仅对变量支持切片分配"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Keras的深层网络,我需要在一层的输出上应用裁剪,然后发送到下一层.为此,我将以下代码编写为lambda层:

I have a deep network using Keras and I need to apply cropping on the output of one layer and then send to the next layer. for this aim, I write the following code as a lambda layer:

def cropping_fillzero(img, rate=0.6): # Q = percentage
    residual_shape = img.get_shape().as_list()
    h, w = residual_shape[1:3]
    blacked_pixels = int((rate) * (h*w))
    ratio = int(np.floor(np.sqrt(blacked_pixels)))
#    width = int(np.ceil(np.sqrt(blacked_pixels)))

    x = np.random.randint(0, w - ratio)
    y = np.random.randint(0, h - ratio)
    cropingImg = img[y:y+ratio, x:x+ratio].assign(tf.zeros([ratio, ratio]))

    return cropingImg

decoded_noise = layers.Lambda(cropping_fillzero, arguments={'rate':residual_cropRate}, name='residual_cropout_attack')(bncv11)

但是会产生以下错误,我不知道为什么?!

but it produces the following error and I do not know why?!

回溯(最近通话最近一次):

Traceback (most recent call last):

文件",第1行,在运行文件('E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py',wdir ='E:/code_v28-7-19')

File "", line 1, in runfile('E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py', wdir='E:/code_v28-7-19')

文件"D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py",运行文件中的第704行execfile(文件名,命名空间)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile execfile(filename, namespace)

文件"D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py",execfile中的第108行exec(compile(f.read(),文件名,'exec'),命名空间)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

文件"E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py",第143行,在encoded_noise =层数.Lambda(cropping_fillzero,arguments = {'rate':residual_cropRate},name ='residual_cropout_attack')(bncv11)

File "E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py", line 143, in decoded_noise = layers.Lambda(cropping_fillzero, arguments={'rate':residual_cropRate}, name='residual_cropout_attack')(bncv11)

文件"D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ base_layer.py",第457行,致电输出= self.call(输入,** kwargs)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\base_layer.py", line 457, in call output = self.call(inputs, **kwargs)

文件"D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ layers \ core.py",687行,正在通话中返回self.function(inputs,** arguments)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\layers\core.py", line 687, in call return self.function(inputs, **arguments)

文件"E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py",第48行,在cropping_fillzero中cropingImg = img [y:y + ratio,x:x + ratio] .assign(tf.zeros([ratio,ratio]))

File "E:/code_v28-7-19/CIFAR_los4x4convolvedw_5_cropAttack_RandomSize_Pad.py", line 48, in cropping_fillzero cropingImg = img[y:y+ratio, x:x+ratio].assign(tf.zeros([ratio, ratio]))

文件"D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ tensorflow \ python \ ops \ array_ops.py",分配中的第700行引发ValueError(仅变量支持切片分配")

File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\array_ops.py", line 700, in assign raise ValueError("Sliced assignment is only supported for variables")

ValueError:仅变量支持切片分配

ValueError: Sliced assignment is only supported for variables

能否请您告诉我如何解决此错误?谢谢

could you please tell me how can I solve this error? Thank you

推荐答案

cropingImg = img[y:y+ratio, x:x+ratio].assign(tf.zeros([ratio, ratio]))

您正在尝试在张量上使用切片符号.切片符号只能用于变量,如错误消息所述.为了获得实际的变量,请尝试使用tf.identity()加载上一层的输出:

You are trying to use slice notation on a tensor. Slice notation may only be used on variables, as the error message says. In order to get the actual variable, try loading the previous layer output using tf.identity():

self.output = your_layer
self.output = tf.identity(self.output, name='output')
output = graph.get_tensor_by_name('output:0')

这篇关于为什么会发生此错误:“仅对变量支持切片分配"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆