8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

MIT 深度学习入门。实验 1,第 2 部分:使用 RNN 生成音乐

Ayaan 2月前

55 0

Alexander 和 Ava Amini 以及 Sadhana Lola 等人在 MIT 上发表了一门很棒的 ML 课程:深度学习简介。作为该课程的一部分,在 github 上发布了实验室 Lab1-Part 2....

Alexander 和 Ava Amini 以及 Sadhana Lola 等人 在 MIT 上发布了一门很棒的 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]}
帖子版权声明 1、本帖标题:MIT 深度学习入门。实验 1,第 2 部分:使用 RNN 生成音乐
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Ayaan在本站《tensorflow》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 当我尝试训练我的模型时,出现错误 — \'ValueError: 无法将'8'转换为形状\',而我的 X_train 的形状是 (5347,28)。import tensorflow as tffrom tensorflow.keras im...

    当我尝试训练我的模型时,出现错误-'ValueError:无法将'8'转换为形状',而我的X_train的形状是(5347,28)。

    import tensorflow as tf
    from tensorflow.keras import Sequential
    from tensorflow.keras.layers import Dense
    
    model = Sequential()
    
    model.add(tf.keras.Input(shape=(X_train.shape[1])))
    model.add(Dense(units=25,activation='relu'))
    model.add(Dense(units=15 , activation='relu'))
    model.add(Dense(units=1 , activation='linear'))
    
    model.summary()
    
    loss_fun = tf.keras.losses.MSE
    model.compile(loss = loss_fun,
                  optimizer = 'adam',
                  metrics=['accuracy']
                 )
    history = model.fit(X_train,y_train,epochs=25)
    

    我尝试了不同的方法,但仍然出现一些不同的错误。

  • 我尝试在我的应用程序中创建 AddMembersForm 到 Group。'GroupDetailView' 对象没有属性 'object'class GroupDetailView(DetailView): model = Group context_object_name = 'group'

    我尝试在我的应用程序中创建 AddMembersForm 到 Group。'GroupDetailView ' 对象没有属性 'object'

    class GroupDetailView(DetailView):
    
        model = Group
        context_object_name = 'group'
        template_name = 'rooms/group_detail.html'
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            context['form'] = AddGroupMemberForm(group_id=self.object.id)
            context['members'] = GroupMembers.objects.filter(group=self.object) #Download all members
            return context
    
        def post(self, request, *args, **kwargs):
            form = AddGroupMemberForm(request.POST, group_id=self.object.id)
            if form.is_valid():
                new_member = form.save(commit=False)
                new_member.group = self.get_object()
                new_member.save()
                return redirect(self.get_object().get_absolute_url())
            else:
                return self.render_to_response(self.get_context_data(form=form))
    -----------------------------------------------------------------------------------
    class Group(models.Model):
    
        author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='creator_group', on_delete=models.CASCADE)
        name = models.TextField(max_length=50)
        description = models.TextField(max_length=500)
    
        def __str__(self):
            return self.name
    
        def get_absolute_url(self):
            return reverse('group_detail', kwargs={'pk': self.pk})
    
    class GroupMembers(models.Model):
    
        group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name='membership')
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='group_membership')
        role = models.TextField(max_length=30, default='Członek grupy')
        time_release = models.DateTimeField(default=timezone.now)
    
        def __str__(self):
            return f'{self.user.username} należy do {self.group.name}'
    

    我想将用户添加到我的群组(群组创始人将有此选项)。

  • 您能告诉我您的训练数据集是否已正确转换为张量或 numpy 数组吗?如果没有看到 X_train 和 y_train 数据及其 dtypes,就很难判断。

  • 在以下像你试图得到的那样 self.object.id ,你的 self GroupDetailView .

    form = AddGroupMemberForm(request.POST, group_id=self.object.id)

    然后您可以添加以下内容。如果你发送的是 group_id /add/member/ 这样的 URL

    def post(self, request, group_id,**kwargs):
       form = AddGroupMemberForm(request.POST, group_id=group_id)
    
  • Nar 2月前 0 只看Ta
    引用 6

    我使用了僧伽罗语英语数据集并训练它来翻译僧伽罗语英语 java 相关问题。它提供了良好的训练准确性,但问题是保存后无法加载并使用它进行开发...

    我使用了僧伽罗英语数据集并训练它来翻译僧伽罗英语 java 相关问题。它提供了良好的训练准确性,但问题是保存后无法加载并使用它开发翻译 api。我将此模型保存为 h5 格式并尝试加载它给出

    import os
    import shutil
    import subprocess
    import warnings
    from pathlib import Path
    
    import numpy as np
    import pandas as pd
    import plotly.express as px
    import tensorflow as tf
    import tensorflow_datasets as tfds
    from tensorflow import keras
    from keras import layers
    !pip install colorama
    from colorama import Fore, Style
    from IPython.core.display import HTML
    
    warnings.filterwarnings("ignore")
    
    easy_dataset_user = "test"
    easy_dataset = "language-translation-englishfrench"
    data_dir = Path("data")
    from google.colab import drive
    drive.mount('/content/drive')
    
    if not ON_KAGGLE:
        download_dataset_from_kaggle(easy_dataset_user, easy_dataset, data_dir)
        easy_dataset_path = "/content/drive/MyDrive/java_sinhala_vocabulary.csv"
    else:
        easy_dataset_path = Path(
            "/content/drive/MyDrive/java_sinhala_vocabulary.csv"
        )
    
    easy_dataset = pd.read_csv(easy_dataset_path, encoding="utf-8", engine="pyarrow")
    easy_dataset = easy_dataset.sample(len(easy_dataset), random_state=42)
    easy_dataset.head()
    
    easy_dataset.info()
    
    easy_dataset["English Words in Sentence"] = (
        easy_dataset["english"].str.split().apply(len)
    )
    easy_dataset["French Words in Sentence"] = (
        easy_dataset["sinhala"].str.split().apply(len)
    )
    
    fig = px.histogram(
        easy_dataset,
        x=["English Words in Sentence", "French Words in Sentence"],
        color_discrete_sequence=["#3f384a", "#e04c5f"],
        labels={"variable": "Variable", "value": "Words in Sentence"},
        marginal="box",
        barmode="group",
        height=540,
        width=840,
        title="Easy Dataset - Words in Sentence",
    )
    fig.update_layout(
        font_color=FONT_COLOR,
        title_font_size=18,
        plot_bgcolor=BACKGROUND_COLOR,
        paper_bgcolor=BACKGROUND_COLOR,
        bargap=0.2,
        bargroupgap=0.1,
        legend=dict(orientation="h", yanchor="bottom", xanchor="right", y=1.02, x=1),
        yaxis_title="Count",
    )
    fig.show()
    
    sentences_en = easy_dataset["english"].to_numpy()
    sentences_fr = easy_dataset["sinhala"].to_numpy()
    
    valid_fraction = 0.1
    valid_len = int(valid_fraction * len(easy_dataset))
    
    sentences_en_train = sentences_en[:-valid_len]
    sentences_fr_train = sentences_fr[:-valid_len]
    
    sentences_en_valid = sentences_en[-valid_len:]
    sentences_fr_valid = sentences_fr[-valid_len:]
    
    def prepare_input_and_target(sentences_en, sentences_fr):
        """Return data in the format: `((encoder_input, decoder_input), target)`"""
        return (sentences_en, b"startofseq " + sentences_fr), sentences_fr + b" endofseq"
    
    
    def from_sentences_dataset(
        sentences_en,
        sentences_fr,
        batch_size=32,
        cache=True,
        shuffle=False,
        shuffle_buffer_size=10_000,
        seed=None,
    ):
        dataset = tf.data.Dataset.from_tensor_slices((sentences_en, sentences_fr))
        dataset = dataset.map(prepare_input_and_target, num_parallel_calls=tf.data.AUTOTUNE)
        if cache:
            dataset = dataset.cache()
        if shuffle:
            dataset = dataset.shuffle(shuffle_buffer_size, seed=seed)
        return dataset.batch(batch_size)
    
    benchmark_ds = from_sentences_dataset(sentences_en_train, sentences_fr_train)
    benchmark_ds = benchmark_ds.prefetch(tf.data.AUTOTUNE)
    bench_results = tfds.benchmark(benchmark_ds, batch_size=32)
    
    example_ds = from_sentences_dataset(
        sentences_en_train, sentences_fr_train, batch_size=4
    )
    list(example_ds.take(1))[0]
    
    example_ds.cardinality()  # Number of batches per epoch.
    
    class ColoramaVerbose(keras.callbacks.Callback):
        def on_epoch_end(self, epoch, logs=None):
            print(
                f"{CLR}Epoch: {RED}{epoch + 1:02d}{CLR} -",
                f"{CLR}loss: {RED}{logs['loss']:.5f}{CLR} -",
                f"{CLR}accuracy: {RED}{logs['accuracy']:.5f}{CLR} -",
                f"{CLR}val_loss: {RED}{logs['val_loss']:.5f}{CLR} -",
                f"{CLR}val_accuracy: {RED}{logs['val_accuracy']:.5f}",
            )
    
    
    def adapt_compile_and_fit(
        model,
        train_dataset,
        valid_dataset,
        n_epochs=50,
        n_patience=5,
        init_lr=0.001,
        lr_decay_rate=0.1,
        colorama_verbose=False,
    ):
    
    
        model.vectorization_en.adapt(
            train_dataset.map(
                lambda sentences, target: sentences[0],  # English sentences.
                num_parallel_calls=tf.data.AUTOTUNE,
            )
        )
        model.vectorization_fr.adapt(
            train_dataset.map(
                lambda sentences, target: sentences[1] + b" endofseq",  # French sentences.
                num_parallel_calls=tf.data.AUTOTUNE,
            )
        )
    
        train_dataset_prepared = train_dataset.map(
            lambda sentences, target: (sentences, model.vectorization_fr(target)),
            num_parallel_calls=tf.data.AUTOTUNE,
        ).prefetch(tf.data.AUTOTUNE)
    
        valid_dataset_prepared = valid_dataset.map(
            lambda sentences, target: (sentences, model.vectorization_fr(target)),
            num_parallel_calls=tf.data.AUTOTUNE,
        ).prefetch(tf.data.AUTOTUNE)
    
        early_stopping_cb = keras.callbacks.EarlyStopping(
            monitor="val_accuracy", patience=n_patience, restore_best_weights=True
        )
    
        # The line below doesn't work with multi-file interleaving.
        # n_decay_steps = n_epochs * train_dataset_prepared.cardinality().numpy()
        # Less elegant solution.
        n_decay_steps = n_epochs * len(list(train_dataset_prepared))
        scheduled_lr = keras.optimizers.schedules.ExponentialDecay(
            initial_learning_rate=init_lr,
            decay_steps=n_decay_steps,
            decay_rate=lr_decay_rate,
        )
    
        model_callbacks = [early_stopping_cb]
        verbose_level = 1
        if colorama_verbose:
            model_callbacks.append(ColoramaVerbose())
            verbose_level = 0
    
        model.compile(
            loss="sparse_categorical_crossentropy",
            optimizer=keras.optimizers.RMSprop(learning_rate=scheduled_lr),
            metrics=["accuracy"],
        )
    
        return model.fit(
            train_dataset_prepared,
            epochs=n_epochs,
            validation_data=valid_dataset_prepared,
            callbacks=model_callbacks,
            verbose=verbose_level,
        )
    
    
    def translate(model, sentence_en):
        translation = ""
        for word_idx in range(model.max_sentence_len):
            X_encoder = np.array([sentence_en])
            X_decoder = np.array(["startofseq " + translation])
            # Last token's probas.
            y_proba = model.predict((X_encoder, X_decoder), verbose=0)[0, word_idx]
            predicted_word_id = np.argmax(y_proba)
            predicted_word = model.vectorization_fr.get_vocabulary()[predicted_word_id]
            if predicted_word == "endofseq":
                break
            translation += " " + predicted_word
        return translation.strip()
    
    class BidirectionalEncoderDecoderWithAttention(keras.Model):
        def __init__(
            self,
            vocabulary_size=5000,
            max_sentence_len=50,
            embedding_size=256,
            n_units_lstm=512,
            **kwargs,
        ):
            super().__init__(**kwargs)
            self.max_sentence_len = max_sentence_len
    
            self.vectorization_en = layers.TextVectorization(
                vocabulary_size, output_sequence_length=max_sentence_len
            )
            self.vectorization_fr = layers.TextVectorization(
                vocabulary_size, output_sequence_length=max_sentence_len
            )
    
            self.encoder_embedding = layers.Embedding(
                vocabulary_size, embedding_size, mask_zero=True
            )
            self.decoder_embedding = layers.Embedding(
                vocabulary_size, embedding_size, mask_zero=True
            )
    
            self.encoder = layers.Bidirectional(
                layers.LSTM(n_units_lstm // 2, return_sequences=True, return_state=True)
            )
            self.decoder = layers.LSTM(n_units_lstm, return_sequences=True)
            self.attention = layers.Attention()
            self.output_layer = layers.Dense(vocabulary_size, activation="softmax")
    
        def call(self, inputs):
            encoder_inputs, decoder_inputs = inputs
    
            encoder_input_ids = self.vectorization_en(encoder_inputs)
            decoder_input_ids = self.vectorization_fr(decoder_inputs)
    
            encoder_embeddings = self.encoder_embedding(encoder_input_ids)
            decoder_embeddings = self.decoder_embedding(decoder_input_ids)
    
            # The final hidden state of the encoder, representing the entire
            # input sequence, is used to initialize the decoder.
            encoder_output, *encoder_state = self.encoder(encoder_embeddings)
            encoder_state = [
                tf.concat(encoder_state[0::2], axis=-1),  # Short-term state (0 & 2).
                tf.concat(encoder_state[1::2], axis=-1),  # Long-term state (1 & 3).
            ]
            decoder_output = self.decoder(decoder_embeddings, initial_state=encoder_state)
            attention_output = self.attention([decoder_output, encoder_output])
    
            return self.output_layer(attention_output)
    
    K.clear_session()  # Resets all state generated by Keras.
    tf.random.set_seed(42)  # Ensure reproducibility on CPU.
    
    easy_train_ds = from_sentences_dataset(
        sentences_en_train, sentences_fr_train, shuffle=True, seed=42
    )
    easy_valid_ds = from_sentences_dataset(sentences_en_valid, sentences_fr_valid)
    
    bidirect_encoder_decoder = BidirectionalEncoderDecoderWithAttention(max_sentence_len=15)
    bidirect_history = adapt_compile_and_fit(
        bidirect_encoder_decoder,
        easy_train_ds,
        easy_valid_ds,
        init_lr=0.01,
        lr_decay_rate=0.01,
        colorama_verbose=True,
    )
    fig = px.line(
        bidirect_history.history,
        markers=True,
        height=540,
        width=840,
        symbol="variable",
        labels={"variable": "Variable", "value": "Value", "index": "Epoch"},
        title="Easy Dataset - Encoder-Decoder RNN Training Process",
        color_discrete_sequence=px.colors.diverging.balance_r,
    )
    fig.update_layout(
        font_color=FONT_COLOR,
        title_font_size=18,
        plot_bgcolor=BACKGROUND_COLOR,
        paper_bgcolor=BACKGROUND_COLOR,
    )
    fig.show()
    translation1 = translate(bidirect_encoder_decoder, "Hello, how are you?")
    translation2 = translate(bidirect_encoder_decoder, "This is a test sentence")
    translation3 = translate(bidirect_encoder_decoder, "you will receive a confirmation code after completing the registration.")
    
    print(CLR + "Actual Possible Translations:")
    print(BLUE + "Hello, how are you?".ljust(25), RED + "-> ", BLUE + "හෙලෝ, ඔයාට කෙසේද?")
    print(
        BLUE + "This is a test sentence".ljust(25),
        RED + "-> ",
        BLUE + "මෙය පරීක්ෂණ වාක්යයකි.",
    )
    print(
        BLUE + "you will receive a confirmation code after completing the registration".ljust(25),
        RED + "-> ",
        BLUE + "ලියාපදිංචිය සම්පූර්ණ කලාට පස්සෙ ඔයාට තහවුරු කිරීමේ කේතයක්  හම්බවේවි",
    )
    print()
    print(CLR + "Model Translations:")
    print(BLUE + "Hello, how are you?".ljust(25), RED + "-> ", BLUE + translation1)
    print(BLUE + "This is a test sentence".ljust(25), RED + "-> ", BLUE + translation2)
    print(BLUE + "you will receive a confirmation code after completing the registration".ljust(25), RED + "-> ", BLUE + translation3)
    
    PS D:app> python model.py
    2024-07-21 00:08:39.758670: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
    2024-07-21 00:08:40.515736: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
    Traceback (most recent call last):
      File "D:app\model.py", line 15, in <module>       
        model = tf.keras.models.load_model(model_path)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    保存此模型后,请帮助我构建 api。我想使用这个保存的模型构建 api 来获取给定英文句子的含义。我们需要以哪种格式保存此模型?

    这就是我尝试开发 API 的方式。

    import numpy as np
    import keras
    from keras import ops
    import tensorflow as tf
    from tensorflow.keras.models import load_model
    from tensorflow.keras.layers import Layer
    from fastapi import FastAPI, HTTPException
    import tensorflow as tf
    import numpy as np
    
    app = FastAPI()
    
    # Load the model
    model_path = "english_sinhala_transformer_model.h5"
    model = tf.keras.models.load_model(model_path)
    
    def translate(sentence_en):
        translation = ""
        for word_idx in range(model.max_sentence_len):
            X_encoder = np.array([sentence_en])
            X_decoder = np.array(["startofseq " + translation])
            y_proba = model.predict((X_encoder, X_decoder), verbose=0)[0, word_idx]
            predicted_word_id = np.argmax(y_proba)
            predicted_word = model.vectorization_fr.get_vocabulary()[predicted_word_id]
            if predicted_word == "endofseq":
                break
            translation += " " + predicted_word
        return translation.strip()
    
    @app.get("/translate")
    def get_translation(sentence: str):
        try:
            translation = translate(sentence)
            return {"translation": translation}
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))
    
  • () 32 # 使用自定义层加载模型 33 with custom_object_scope({'PositionalEmbedding': PositionalEmbedding}): ---> 34 model = load_model('/content/drive/My Drive/trained_model.h5') 35 36 # 使用模型进行预测TypeError Traceback(最近一次调用最后一次)在

  • /usr/local/lib/python3.10/dist-packages/keras/src/engine/base_layer.py in from_config(cls, config) 868 return cls(**config) 869 except Exception as e: --> 870 raise TypeError( 871 f \'使用 \' 反序列化类“{cls。__name__}”时出错 872 f \'config={config}.\n\n遇到异常:{e} \' TypeError:使用 config={'name': 'positional_embedding', 'trainable': True, 'dtype': 'float32', 'output_dim': 256, 'sequence_length': 20, 'input_dim': 15000} 反序列化类“PositionalEmbedding”时出错。

  • 您可能正在使用不同版本的 keras 或 tensorflow 进行保存和加载。

返回
作者最近主题: