层 simple_rnn_16 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4

时间:2021-04-03 23:07:42

标签: python tensorflow keras recurrent-neural-network

我正在尝试多类分类,这里是我的训练输入和输出的详细信息。我看过其他帖子/答案,但无法弄清楚。感谢您的帮助。

**vectorized_data['train_sents']   #This is how the train data looks like
array([[[   1,    1,    1, ...,    1,    1,    1],
        [   0,    0,    0, ...,    0,    0,    0],
        [5818, 7142, 2467, ..., 7927, 7927, 7927],
        [  10,   19,    6, ...,   22,   22,   22],
        [   6,    6,    6, ...,    8,    8,    8],
        [   0,    0,    0, ..., 7927, 7927, 7927]],

       [[1108, 1108, 1108, ..., 1108, 1108, 1108],
        [   0,    0,    0, ...,    0,    0,    0],
        [2244, 1362,  904, ..., 7927, 7927, 7927],
        [   0,    0,   13, ...,   22,   22,   22],
        [   3,    9,    6, ...,    8,    8,    8],
        [   0,    0,    0, ..., 7927, 7927, 7927]]])

vectorized_data['train_sents'].shape
(2522, 6, 67)

vectorized_data['train_lab'] # Labels data
array([[46, 46,  7, ..., 25, 25, 25],
       [46, 46, 46, ..., 25, 25, 25],
       [46, 11,  3, ..., 25, 25, 25],
       ...,
       [46, 46, 46, ..., 25, 25, 25],
       [ 7, 31, 31, ..., 25, 25, 25],
       [ 7, 31, 46, ..., 25, 25, 25]])

vectorized_data['train_lab'].shape
(2522, 67)

#Define Batch size
train_batch_size = 32
valid_batch_size = 64
test_batch_size = 64

max_seq_len = 124 #Define max_length of 124 for padding sentences and labels 
EMBEDDING_DIM = 300
epochs = 5
hidden = 256 #no of neurons
lr = .0001 # learning rate

batched_train_dataset
<BatchDataset shapes: ((32, 6, 67), (32, 67)), types: (tf.int64, tf.int64)>

#Define RNN Simple model
class RNN_Simple(tf.keras.Model):
  def __init__(self, max_seq_len, embed_input_dim, embed_output_dim, num_labels, weights):
    super(RNN_Simple, self).__init__()
    self.embedding = layers.Embedding(input_dim=embed_input_dim, output_dim=embed_output_dim, 
                     weights=weights, input_length=max_seq_len, trainable=False, mask_zero=True)

    self.rnnsimple = layers.SimpleRNN(hidden, return_sequences=True) #Define hidden layer with 256 units

    self.dense = layers.Dense(num_labels, activation = 'softmax')

  def call(self, inputs):
    x = self.embedding(inputs) 
    x = self.rnnsimple(x) 
    logits = self.dense(x)
    return logits

# Run RNN Simple model
model = RNN_Simple(max_seq_len=max_seq_len,embed_input_dim=len(vocab), embed_output_dim=EMBEDDING_DIM, weights=[embed_weights], num_labels=len(indicies))
optimizer = tf.keras.optimizers.Adam(learning_rate=lr) #use Adam Optimizer with learning rate = .0001
sccel = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) #sparse categorical cross-entropy loss

# Train the RNN Simpl model. Calculate Loss.
train_loss_m = tf.keras.metrics.Mean('training_loss', dtype=tf.float32)
valid_loss_m = tf.keras.metrics.Mean('valid_loss', dtype=tf.float32)

def train_func(sent_batch, labels_batch):
  '''
   This function applies gradients and calculates loss for training dataset
   '''
  with tf.GradientTape() as tape:
    logits = model(sent_batch)
    loss = sccel(labels_batch, logits)
  grads = tape.gradient(loss, model.trainable_variables)
  optimizer.apply_gradients(list(zip(grads,   model.trainable_variables)))
  return loss, logits**

此功能在 >>>>>> logits = model(sent_batch)

上失败
 def train_func(sent_batch, labels_batch):
      with tf.GradientTape() as tape:
        logits = model(sent_batch)

错误是: ValueError: 层 simple_rnn_16 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。收到完整形状:(32, 6, 67, 300)

0 个答案:

没有答案