Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 使用tf.keras.layers.LSTM时获取AttributeError_Tensorflow_Keras_Lstm - Fatal编程技术网

Tensorflow 使用tf.keras.layers.LSTM时获取AttributeError

Tensorflow 使用tf.keras.layers.LSTM时获取AttributeError,tensorflow,keras,lstm,Tensorflow,Keras,Lstm,当我使用tf.keras.layers.LSTM(尝试使用GPU实例)时,我的错误率低于此值,而正常的keras.layers.LSTM工作正常(不使用GPU) 如果我使用keras.layers.LSTM创建模型,我将使用该模型 def build_model(embedding_matrix, num_aux_targets): title = Input(shape=(MAX_LEN,)) question_body = Input(shape=(MAX_LEN,))

当我使用tf.keras.layers.LSTM(尝试使用GPU实例)时,我的错误率低于此值,而正常的keras.layers.LSTM工作正常(不使用GPU)

如果我使用keras.layers.LSTM创建模型,我将使用该模型

def build_model(embedding_matrix, num_aux_targets):
    title = Input(shape=(MAX_LEN,))
    question_body = Input(shape=(MAX_LEN,))
    answer = Input(shape=(MAX_LEN,))
    #category = Input(shape=(1,))

    title_embb = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False )(title)
    question_body_embb = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False)(question_body)
    answer_embb = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False)(answer)
    concat = Concatenate(axis=1)
    embb_final = concat([title_embb,question_body_embb,answer_embb])

    x1 = SpatialDropout1D(0.3)(embb_final)
    x1 = Bidirectional(tf.keras.layers.LSTM(LSTM_UNITS, return_sequences=True))(x1)
    hidden1 = concatenate([
        GlobalMaxPooling1D()(x1), 
        GlobalAveragePooling1D()(x1),#layer returns a fixed-length output vector for each example by averaging over the sequence dimension. This allows the model to handle input 
        #of variable length in the simplest way possible.
    ])
    hidden1 = add([hidden1, Dense(DENSE_HIDDEN_UNITS, activation='relu')(hidden1)])

    result = Dense(30, activation='sigmoid')(hidden1)
    model = Model(inputs=[title,question_body,answer], outputs= result)
    model._name = 'mymodel'
    model.compile(loss='binary_crossentropy',metrics = ['accuracy'], optimizer='adam')
    model.summary()
    return model
调用模型后,我发现以下错误:

    ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __setattr__(self, name, value)
   2238       try:
-> 2239         super(tracking.AutoTrackable, self).__setattr__(name, value)
   2240       except AttributeError:

AttributeError: can't set attribute

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-62-8baf6fbe617d> in <module>
      1 for model_idx in range(NUM_MODELS):
----> 2     model = build_model(embedding_matrix,1)
      3     for global_epoch in range(EPOCHS):
      4 
      5         model.fit(

<ipython-input-61-925b81ce0745> in build_model(embedding_matrix, num_aux_targets)
     12 
     13     x1 = SpatialDropout1D(0.3)(embb_final)
---> 14     x1 = Bidirectional(keras.layers.LSTM(LSTM_UNITS, return_sequences=True,recurrent_activation='sigmoid',recurrent_dropout=0,unroll=False,use_bias=True))(x1)
     15     hidden1 = concatenate([
     16         GlobalMaxPooling1D()(x1),

/opt/conda/lib/python3.6/site-packages/keras/layers/wrappers.py in __init__(self, layer, merge_mode, weights, **kwargs)
    366                              'Merge mode should be one of '
    367                              '{"sum", "mul", "ave", "concat", None}')
--> 368         self._set_sublayers(layer)
    369         self.merge_mode = merge_mode
    370         if weights:

/opt/conda/lib/python3.6/site-packages/keras/engine/base_layer.py in wrapped_fn(*args, **kwargs)
     28         prev_value = _DISABLE_TRACKING.value
     29         _DISABLE_TRACKING.value = True
---> 30         out = func(*args, **kwargs)
     31         _DISABLE_TRACKING.value = prev_value
     32         return out

/opt/conda/lib/python3.6/site-packages/keras/layers/wrappers.py in _set_sublayers(self, layer)
    390         config['go_backwards'] = not config['go_backwards']
    391         self.backward_layer = layer.__class__.from_config(config)
--> 392         self.forward_layer.name = 'forward_' + self.forward_layer.name
    393         self.backward_layer.name = 'backward_' + self.backward_layer.name
    394 

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __setattr__(self, name, value)
   2242             ('Can\'t set the attribute "{}", likely because it conflicts with '
   2243              'an existing read-only @property of the object. Please choose a '
-> 2244              'different name.').format(name))
   2245       return
   2246 

AttributeError: Can't set the attribute "name", likely because it conflicts with an existing read-only @property of the object. Please choose a different name.
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
/opt/conda/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/base\u layer.py in\uuuuuuuuu setattr\uuuuuuu(self、name、value)
2238请尝试:
->2239超级(tracking.AutoTrackable,self)。\uuuuu setattr\uuuuuu(名称,值)
2240除属性错误外:
AttributeError:无法设置属性
在处理上述异常期间,发生了另一个异常:
AttributeError回溯(最近一次呼叫上次)
在里面
1适用于范围内的idx型号(NUM型号):
---->2模型=构建模型(嵌入矩阵,1)
3对于范围内的全局_历元(历元):
4.
5.5模型拟合(
内置模型(嵌入矩阵、数量辅助目标)
12
13 x1=空间衰减1D(0.3)(embb_最终版)
--->14 x1=双向(keras.layers.LSTM(LSTM\u单位,返回序列=真,周期性激活='sigmoid',周期性丢失=0,展开=假,使用偏差=真))(x1)
15 hidden1=连接([
16全局展开1D()(x1),
/opt/conda/lib/python3.6/site-packages/keras/layers/wrappers.py in uuuuuu init_uuuuuuuu(自、层、合并模式、权重,**kwargs)
366“合并模式应为下列之一”
367'{“总和”、“多”、“平均”、“concat”、无}')
-->368自组子层(层)
369 self.merge\u mode=合并\u mode
370如果重量:
/opt/conda/lib/python3.6/site-packages/keras/engine/base_layer.py in wrapped_fn(*args,**kwargs)
28 prev_value=_DISABLE_TRACKING.value
29 _DISABLE_TRACKING.value=True
--->30 out=func(*args,**kwargs)
31 _DISABLE_TRACKING.value=上一个值
32返回
/opt/conda/lib/python3.6/site-packages/keras/layers/wrappers.py in_set_子层(self,layer)
390配置['go_Backward']=非配置['go_Backward']
391 self.backward\u layer=layer.\uuuuuu class\uuuuuu.from\u config(配置)
-->392 self.forward\u layer.name='forward\u'+self.forward\u layer.name
393 self.backward\u layer.name='backward\u'+self.backward\u layer.name
394
/opt/conda/lib/python3.6/site-packages/tensorflow\u core/python/keras/engine/base\u layer.py in\uuuuuuuuu setattr\uuuuuuu(self、name、value)
2242('无法设置属性“{}”,可能是因为它与'
2243'对象的现有只读@属性。请选择'
->2244“不同名称”。)。格式(名称))
2245返回
2246
AttributeError:无法设置属性“name”,可能是因为它与对象的现有只读@属性冲突。请选择其他名称。

我没有使用任何属性“name”在代码中,也不允许升级或降级Keras或TF版本。

请发布您的,以及您使用的是哪个版本的TF和Keras?Tensorflow版本:2.1.0rc0和Keras版本2.3.1在问题中更新。@Buntysah避免使用
模型。_name='mymodel'
在您的模型描述中删除了仍然是它的我的错误,当我开始接收错误时,我实际上添加了这个。