麻省理工学院的深度学习简介。 Lab-1,第 2 部分:使用 RNN 生成音乐

问题描述 投票:0回答:1

Alexander、Ava Amini 和 Sadhana Lola 等人发布了很棒的 ML 课程麻省理工学院:深度学习简介。作为该课程的一部分,在 github 上发布了实验室Lab1-Part 2。 因此,由于我的 Google Colab 运行非常糟糕(我几个小时无法连接到 T4),我决定将笔记本电脑移至我的家用笔记本电脑上。问题是这样的: 谁能解释一下为什么相同的代码可以在 google colab 上运行,而不能在我的本地笔记本上运行。我查了一下TF版本,我的是16.2,对比G-Colab的15.0。 tf.keras.layers.Embedding 函数的签名似乎是相同的,但我不知道如何解决这个问题...:

### Defining the RNN Model ###

'''TODO: Add LSTM and Dense layers to define the RNN model using the Sequential API.'''
def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
  model = tf.keras.Sequential([
    # Layer 1: Embedding layer to transform indices into dense vectors
    #   of a fixed embedding size
    tf.keras.layers.Embedding(vocab_size, embedding_dim, batch_input_shape=[batch_size, None]),

    # Layer 2: LSTM with `rnn_units` number of units.
    # TODO: Call the LSTM function defined above to add this layer.
    LSTM(rnn_units=rnn_units),

    # Layer 3: Dense (fully-connected) layer that transforms the LSTM output
    #   into the vocabulary size.
    # TODO: Add the Dense layer.
    tf.keras.layers.Dense(units=vocab_size,
                          activation='softmax')
  ])

  return model

# Build a simple model with default hyperparameters. You will get the
#   chance to change these later.
model = build_model(len(vocab), embedding_dim=256, rnn_units=1024, batch_size=32)

我的笔记本显示的错误如下:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[16], line 25
     21   return model
     23 # Build a simple model with default hyperparameters. You will get the
     24 #   chance to change these later.
---> 25 model = build_model(len(vocab), embedding_dim=256, rnn_units=1024, batch_size=32)

Cell In[16], line 8, in build_model(vocab_size, embedding_dim, rnn_units, batch_size)
      4 def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
      5   model = tf.keras.Sequential([
      6     # Layer 1: Embedding layer to transform indices into dense vectors
      7     #   of a fixed embedding size
----> 8     tf.keras.layers.Embedding(vocab_size, embedding_dim, batch_input_shape=[batch_size, None]),
      9 
     10     # Layer 2: LSTM with `rnn_units` number of units.
     11     # TODO: Call the LSTM function defined above to add this layer.
     12     LSTM(rnn_units=rnn_units),
     13 
     14     # Layer 3: Dense (fully-connected) layer that transforms the LSTM output
     15     #   into the vocabulary size.
     16     # TODO: Add the Dense layer.
     17     tf.keras.layers.Dense(units=vocab_size,
     18                           activation='softmax')
     19   ])
     21   return model

File ~/miniconda3/envs/gpt_env/lib/python3.10/site-packages/keras/src/layers/core/embedding.py:93, in Embedding.__init__(self, input_dim, output_dim, embeddings_initializer, embeddings_regularizer, embeddings_constraint, mask_zero, weights, lora_rank, **kwargs)
     89 if input_length is not None:
     90     warnings.warn(
     91         "Argument `input_length` is deprecated. Just remove it."
     92     )
---> 93 super().__init__(**kwargs)
     94 self.input_dim = input_dim
     95 self.output_dim = output_dim

File ~/miniconda3/envs/gpt_env/lib/python3.10/site-packages/keras/src/layers/layer.py:266, in Layer.__init__(self, activity_regularizer, trainable, dtype, autocast, name, **kwargs)
    264     self._input_shape_arg = input_shape_arg
    265 if kwargs:
--> 266     raise ValueError(
    267         "Unrecognized keyword arguments "
    268         f"passed to {self.__class__.__name__}: {kwargs}"
    269     )
    271 self._path = None  # Will be determined in `build_wrapper`
    272 self.built = False

ValueError: Unrecognized keyword arguments passed to Embedding: {'batch_input_shape': [32, None]}
tensorflow jupyter-notebook google-colaboratory
1个回答
0
投票
# It works
import tensorflow as tf
from tensorflow.keras.layers import LSTM

def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
    model = tf.keras.Sequential([
        # Layer 1
        tf.keras.layers.Embedding(vocab_size, embedding_dim, input_shape=(None,)),

        # Layer 2
        LSTM(rnn_units, return_sequences=True),

        # Layer 3
        tf.keras.layers.Dense(vocab_size)
    ])

    return model

vocab = ['a', 'b', 'c']  # Just a placeholder for the vocab list
model = build_model(len(vocab), embedding_dim=256, rnn_units=1024, batch_size=32)
model.summary()
© www.soinside.com 2019 - 2024. All rights reserved.