Keras 类型错误:';非类型';对象不可编辑,且AttributeError:';非类型';对象没有属性'_入站节点';当我创建一个模型时

Keras 类型错误:';非类型';对象不可编辑,且AttributeError:';非类型';对象没有属性'_入站节点';当我创建一个模型时,keras,deep-learning,google-colaboratory,spyder,keras-layer,Keras,Deep Learning,Google Colaboratory,Spyder,Keras Layer,我正试图实现我的研究目的的论文。为了创建模型的一部分,我创建了一个类。该代码在GoogleColab上运行良好,但当我尝试在spyder上的Windows上运行相同的代码时,这些错误就出现了。我不确定代码或环境中是否存在错误 Keras版本:2.3.1 Python版本:2.3.6 早些时候,我的目标是在与初始特征映射X相乘后添加连接层,我得到了标题中提到的TypeError 备注:代码在Colab上运行良好 这是我的密码 import keras from keras.laye

我正试图实现我的研究目的的论文。为了创建模型的一部分,我创建了一个类。该代码在GoogleColab上运行良好,但当我尝试在spyder上的Windows上运行相同的代码时,这些错误就出现了。我不确定代码或环境中是否存在错误

Keras版本:2.3.1 Python版本:2.3.6

早些时候,我的目标是在与初始特征映射X相乘后添加连接层,我得到了标题中提到的TypeError

备注:代码在Colab上运行良好 这是我的密码

    import keras
    from keras.layers import Input, Conv2D, GlobalAveragePooling2D, BatchNormalization,Reshape,Dot, 
    Multiply, Concatenate,Add,Conv1D,Activation
    from keras.models import Model
    import keras.backend as K
    from keras.layers import Layer

    X = Input(shape = (28,28,512))


#Adding attention maps

class MyLayer(Layer):

    def __init__(self, **kwargs):
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self._x = self.add_weight(name='Lambda', 
                                    shape=(1,28),
                                    initializer='uniform',
                                    trainable=True)
        super(MyLayer, self).build(input_shape)  # Be sure to call this at the end

    def call(self, x):
        A = x
        result = Dot(axes = (-1,-1))([self._x, A])
        return result

    def compute_output_shape(self, input_shape):
        return input_shape[0]

class Low_Rank_Tensor_Module(Layer):

    def __init__(self, X, **kwargs):
        super(Low_Rank_Tensor_Module, self).__init__(**kwargs)
        self.X = X
        self.batch_size, self.height, self.width, self.channels = X.shape
        self.rank = self.height
        self.X_shortcut = X

    def pool(self, pool_axis ):
        
        
        if pool_axis == 1:
            X = Reshape((self.channels,self.width,self.height))(self.X)
            X = GlobalAveragePooling2D()(X)
            X = Reshape((1, 1,self.height))(X)
        elif pool_axis == 2:
            X = Reshape((self.channels,self.width,self.height))(self.X)
            X = GlobalAveragePooling2D()(X)
            X = Reshape((1,1,self.width))(X)
        elif pool_axis ==3:
            X = GlobalAveragePooling2D()(self.X)
            X = Reshape((1,1,self.channels))(X)
        else:
            print('Pool_Axis Value should be 1,2 or 3')

        return X

    def axis_tgm(self,axis ):
        # batch_size, height, width, channels = X.shape
        # rank = 28    
        X = self.pool(axis)
        if axis == 1:
            X = Conv2D(self.height, kernel_size=(1,1), padding='valid')(X)
        if axis == 2:
            X = Conv2D(self.width, kernel_size=(1,1), padding='valid')(X)    
        if axis == 3:
            X = Conv2D(self.channels, kernel_size=(1,1), padding='valid')(X)
        
        X = Activation('sigmoid')(X)

        return X

    def tgm(self):
        rank = int(self.rank)

        height_feature, width_feature, channel_feature = [], [], []
        H_mod = K.expand_dims(self.axis_tgm(1))
        W_mod = K.expand_dims(self.axis_tgm(2))
        C_mod = K.expand_dims(self.axis_tgm(3))

        height_feature = H_mod
        width_feature = W_mod
        channel_feature = C_mod

        for i in range(rank-1):
            H = K.expand_dims(self.axis_tgm(1))
            height_feature = Concatenate()([height_feature, H])
            
            W = K.expand_dims(self.axis_tgm(2))
            width_feature = Concatenate()([width_feature, W])
            
            C = K.expand_dims(self.axis_tgm(3))
            channel_feature = Concatenate()([channel_feature, C])

        return height_feature, width_feature, channel_feature 

#TENSOR Reconstruction Module Starting from here
    
    def low_rank_cons(self):
        height_feature, width_feature, channel_feature = self.tgm()
        Attn_Maps = []

        for i in range(self.rank):
            h_f = height_feature[:,:,:,:,i]
            w_f = width_feature[:,:,:,:,i]
            two_d = Dot(axes=1)([h_f,w_f])
            attention_maps = Dot(axes = 1)([two_d,channel_feature[:,:,:,:,i]])
            attention_maps = Reshape((self.height, self.width, self.channels))(attention_maps)
            Attn_Maps.append(K.expand_dims(attention_maps))
            
        Attn_Maps = Concatenate()(Attn_Maps)
        add_lambda = self.add_weight(name = 'lambda', shape = (1,self.rank),initializer= 'uniform')
        attention_map = Dot(axes = -1)([add_lambda,Attn_Maps])
        print(attention_map.shape)
        print(self.X_shortcut.shape)
        # attention_map = MyLayer()(Attn_Maps)
        modified_features = Multiply()([attention_map, self.X_shortcut])
        modified_features = Add()([self.X_shortcut,modified_features])
        modified_features = Conv2D(512, kernel_size=(1,1), padding= 'same')(modified_features)

        
        print(modified_features.shape)
        
        return modified_features


modified_features = Low_Rank_Tensor_Module(X).low_rank_cons()
# modified_features = Reshape((28,28,512))(modified_features)
print(modified_features.shape)
# modified_features = Concatenate()([ modified_features,X])
# modified_features = Concatenate(axis = -1)([modified_features, self.X])
# attention_map = MyLayer()(modified_features)    
    
    
model = Model(X,modified_features)
错误是

runfile('C:/Users/akash/Thesis_Project/low_rank_tensor_mod.py', wdir='C:/Users/akash/Thesis_Project')
(None, 28, 28, 512)
(None, 28, 28, 512)
(None, 28, 28, 512)
(None, 28, 28, 512)
Traceback (most recent call last):

  File "C:\Users\akash\Thesis_Project\low_rank_tensor_mod.py", line 145, in <module>
    model = Model(X,modified_features)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py", line 94, in __init__
    self._init_graph_network(*args, **kwargs)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py", line 241, in _init_graph_network
    self.inputs, self.outputs)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py", line 1434, in _map_graph_network
    tensor_index=tensor_index)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py", line 1421, in build_map
    node_index, tensor_index)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py", line 1421, in build_map
    node_index, tensor_index)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py", line 1421, in build_map
    node_index, tensor_index)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py", line 1421, in build_map
    node_index, tensor_index)

  File "C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py", line 1393, in build_map
    node = layer._inbound_nodes[node_index]

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
runfile('C:/Users/akash/Thesis\u Project/low\u rank\u tensor\u mod.py',wdir='C:/Users/akash/Thesis\u Project')
(无、28、28、512)
(无、28、28、512)
(无、28、28、512)
(无、28、28、512)
回溯(最近一次呼叫最后一次):
文件“C:\Users\akash\Thesis\u Project\low\u rank\u tensor\u mod.py”,第145行,在
模型=模型(X,修改的_特征)
文件“C:\Users\akash\anaconda3\envs\keras gpu\lib\site packages\keras\legacy\interfaces.py”,第91行,在包装器中
返回函数(*args,**kwargs)
文件“C:\Users\akash\anaconda3\envs\keras gpu\lib\site packages\keras\engine\network.py”,第94行,在uu init中__
自初始化图网络(*args,**kwargs)
文件“C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py”,第241行,在网络中
自输入、自输出)
文件“C:\Users\akash\anaconda3\envs\keras-gpu\lib\site-packages\keras\engine\network.py”,第1434行,在网络图中
张量指数=张量指数)
文件“C:\Users\akash\anaconda3\envs\keras gpu\lib\site packages\keras\engine\network.py”,第1421行,在构建图中
节点索引(张量索引)
文件“C:\Users\akash\anaconda3\envs\keras gpu\lib\site packages\keras\engine\network.py”,第1421行,在构建图中
节点索引(张量索引)
文件“C:\Users\akash\anaconda3\envs\keras gpu\lib\site packages\keras\engine\network.py”,第1421行,在构建图中
节点索引(张量索引)
文件“C:\Users\akash\anaconda3\envs\keras gpu\lib\site packages\keras\engine\network.py”,第1421行,在构建图中
节点索引(张量索引)
文件“C:\Users\akash\anaconda3\envs\keras gpu\lib\site packages\keras\engine\network.py”,第1393行,在构建图中
节点=层。\入站\节点[节点\索引]
AttributeError:“非类型”对象没有属性“\u入站节点”

大家好,欢迎来到stackoverflow。请提供更多信息,您实际想要实现的目标,例如,您参考的论文