Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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
Python 3.x 断言失败:[条件x==y未保留元素:]_Python 3.x_Tensorflow_Keras_Nlp_Attention Model - Fatal编程技术网

Python 3.x 断言失败:[条件x==y未保留元素:]

Python 3.x 断言失败:[条件x==y未保留元素:],python-3.x,tensorflow,keras,nlp,attention-model,Python 3.x,Tensorflow,Keras,Nlp,Attention Model,我已经为句子分类任务构建了一个带有注意层的BiLSTM模型,但是我得到了一个错误,由于参数数量不匹配,我的断言失败了。注意层代码在这里,错误在代码下面 class attention(Layer): def __init__(self, return_sequences=True): self.return_sequences = return_sequences super(attention,self).__init__()

我已经为句子分类任务构建了一个带有注意层的BiLSTM模型,但是我得到了一个错误,由于参数数量不匹配,我的断言失败了。注意层代码在这里,错误在代码下面

class attention(Layer):
    
    def __init__(self, return_sequences=True):
        self.return_sequences = return_sequences
        super(attention,self).__init__()
        
    def build(self, input_shape):
        
        self.W=self.add_weight(name="att_weight", shape=(input_shape[-1],1),
                               initializer="normal")
        self.b=self.add_weight(name="att_bias", shape=(input_shape[1],1),
                               initializer="zeros")
        
        super(attention,self).build(input_shape)
        
    def call(self, x):
        
        e = K.tanh(K.dot(x,self.W)+self.b)
        a = K.softmax(e, axis=1)
        output = x*a
        
        if self.return_sequences:
            return output
        
        return K.sum(output, axis=1)
当我训练包含注意层的模型时,它给出了一个断言失败的错误

Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-45-ac310033130c> in <module>()
      1 #Early stopping, Adam, dropout = 0.3, 0.5, 0.5
      2 #history = model.fit(sequences_matrix, Y_train, batch_size=256, epochs=5, validation_split=0.1,  callbacks=[EarlyStopping(monitor='val_loss', min_delta=0.0001)])
----> 3 history = model.fit(sequences_matrix, Y_train, batch_size=32, epochs=10, validation_split=0.1)

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  assertion failed: [Condition x == y did not hold element-wise:] [x (sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [32 1] [y (sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [32 758]
     [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/assert_equal_1/Assert/Assert (defined at <ipython-input-45-ac310033130c>:3) ]] [Op:__inference_train_function_19854]

Function call stack:
train_function
火车的形状是

max_words = 48369
max_len = 768
tok = Tokenizer(num_words = max_words)
tok.fit_on_texts(X_train)
sequences = tok.texts_to_sequences(X_train)
sequences_matrix = sequence.pad_sequences(sequences, maxlen = max_len)
Y_train = np.array(Y_train)
Y_test = np.array(Y_test)

print(Y_train.shape)

(43532, 1)

您的目标是2D,因此您需要在最后一个注意层中设置
return\u sequences=False
,以便以2D格式返回输出

报告模型(而不是摘要)。你的头发是什么形状的target@MarcoCerliani用模型编辑问题。我不明白你说的目标形状是什么意思?我的嵌入的维数为768*48369,总目标值为48369(二进制分类)。目标的形状为y_列。形状。。。它返回什么?它是
(43532,1)
max_words = 48369
max_len = 768
tok = Tokenizer(num_words = max_words)
tok.fit_on_texts(X_train)
sequences = tok.texts_to_sequences(X_train)
sequences_matrix = sequence.pad_sequences(sequences, maxlen = max_len)
Y_train = np.array(Y_train)
Y_test = np.array(Y_test)

print(Y_train.shape)

(43532, 1)