张量”?,python,tensorflow,keras"/>

首页 > 解决方案 > 这个错误是什么意思“无法转换类型的对象张量”?

问题描述

我在 Keras 有一个网络,我尝试对层的输出进行一些更改,然后将其馈送到剩余的网络。在apply function它接受一个形状为 (batch size,1,32,32) 的张量,所有的张量都是 format channel_first。然后我将它与形状为 (8,8,1,64) 的张量进行卷积,输出为 (?,64,4,4),但是当我想将形状更改为 (8,8,4,4)它会产生以下错误。为什么会发生这个错误?因为当我尝试在没有网络的情况下使用张量检查代码时,它可以工作!我也在这里添加代码。你能告诉我有什么问题吗?

    def apply_conv(self, image, filter_type: str):

        if filter_type == 'dct':
            filters = self.dct_conv_weights
        elif filter_type == 'idct':
            filters = self.idct_conv_weights
        else:
            raise('Unknown filter_type value.')
        print(image.shape)

        image_conv_channels = []
        for channel in range(image.shape[1]):
            print(image.shape)
            print(channel)
            image_yuv_ch = K.expand_dims(image[:, channel, :, :],1)
            print( image_yuv_ch.shape)
            print(filters.shape)
            image_conv = Kr.backend.conv2d(image_yuv_ch,filters,strides=(8,8),data_format='channels_first')
            print(image_conv.shape)
            print('salam')
#            image_conv = Kr.backend.permute_dimensions(image_conv,(0, 2, 3, 1))
            image_conv = Kr.backend.reshape(image_conv,(image_conv.shape[0], 8,8,image_conv.shape[2], image_conv.shape[3]))
            print(image_conv.shape)
            image_conv =  Kr.backend.permute_dimensions(image_conv,(0, 1, 3, 2, 4))
            image_conv = Kr.backend.reshape(image_conv,(image_conv.shape[0],
                                                  image_conv.shape[1]*image_conv.shape[2],
                                                  image_conv.shape[3]*image_conv.shape[4]))

#            Kr.backend.expand_dims(image_conv,1)

            # image_conv = F.conv2d()
            image_conv_channels.append(image_conv)

        image_conv_stacked = Kr.backend.concatenate(image_conv_channels, axis=1)

        return image_conv_stacked

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

文件“”,第 376 行,在 decoded_noise=JpegCompression()(act11)#16

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\base_layer.py”,第 457 行,调用 输出 = self.call(inputs, **kwargs)

文件“”,第 162 行,调用 image_dct = self.apply_conv(noised_image, 'dct')

文件“”,第 126 行,在 apply_conv image_conv = Kr.backend.reshape(image_conv,(image_conv.shape[0], 8,8,image_conv.shape[2], image_conv.shape[3]))

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py”,第 1969 行,reshape return tf.reshape(x, shape)

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py”,第 6482 行,在 reshape “Reshape”中,tensor=tensor,shape=shape,name=name )

_apply_op_helper 中的文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\op_def_library.py”,第 513 行,引发错误

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\op_def_library.py”,第 510 行,在 _apply_op_helper preferred_dtype=default_dtype 中)

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py”,第 1146 行,internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref =as_ref)

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\constant_op.py”,第 229 行,在 _constant_tensor_conversion_function 返回常量(v,dtype=dtype,name=name)

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\constant_op.py”,第 208 行,常量值,dtype=dtype,shape=shape,verify_shape=verify_shape))

文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\tensor_util.py”,第 531 行,make_tensor_proto“支持的类型”。%(类型(值),值))

TypeError:无法将类型对象转换为张量。内容:(维度(无)、8、8、维度(4)、维度(4))。考虑将元素转换为支持的类型。

标签: pythontensorflowkeras

解决方案


推荐阅读