RNN 模型错误:“ValueError:此模型尚未构建。”

Posted

技术标签:

【中文标题】RNN 模型错误:“ValueError:此模型尚未构建。”【英文标题】:RNN Model Error: "ValueError: This model has not yet been built." 【发布时间】:2021-12-22 07:14:14 【问题描述】:

我正在使用 Google Colab 上的本教程构建一个基于字符的 LSTM-RNN 文本生成器:https://colab.research.google.com/github/tensorflow/text/blob/master/docs/tutorials/text_generation.ipynb#scrollTo=d4tSNwymzf-q。

虽然他们的代码使用他们的莎士比亚数据集在我的 Google Colab 帐户上运行和编译,但当我输入我自己的数据集时它不起作用。这个错误不断出现:

"ValueError: This model has not yet been built.

他们使用的数据集是来自 Tensorflow (https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt) 的莎士比亚文本。另一方面,我的数据集是短字符的形式。这是我的数据集的前五行(我正在尝试生成肽序列):

acssspskhcg

agcknffwktftsc

agilkrw

agyllgkinlkalaalakkil

aplepeypgdnatpeqmaqyaaelrryinmltrpry

卡加西

我认为这可能是问题的一部分。

这是我目前的代码:

import tensorflow as tf
from tensorflow.keras.layers.experimental import preprocessing

import numpy as np
import os
import time

# Read, then decode for py2 compat.
text = open("/content/generatorinput.txt", 'rb').read().decode(encoding='utf-8')
# length of text is the number of characters in it
print(f'Length of text: len(text) characters')

# The unique characters in the file
vocab = sorted(set(text))
print(f'len(vocab) unique characters')

example_texts = ['acdefgh', 'tvy']
chars = tf.strings.unicode_split(example_texts, input_enco
chars

ids_from_chars = preprocessing.StringLookup(
    vocabulary=list(vocab), mask_token=None)

ids = ids_from_chars(chars)
ids

chars_from_ids = tf.keras.layers.experimental.preprocessing.StringLookup(
    vocabulary=ids_from_chars.get_vocabulary(), invert=True, mask_token=None)

chars = chars_from_ids(ids)
chars

tf.strings.reduce_join(chars, axis=-1).numpy()

def text_from_ids(ids):
  return tf.strings.reduce_join(chars_from_ids(ids), axis=-1)

all_ids = ids_from_chars(tf.strings.unicode_split(text, 'UTF-8'))
all_ids

ids_dataset = tf.data.Dataset.from_tensor_slices(all_ids)

for ids in ids_dataset.take(10):
    print(chars_from_ids(ids).numpy().decode('utf-8'))

seq_length = 100
examples_per_epoch = len(text)//(seq_length+1)

sequences = ids_dataset.batch(seq_length+1, drop_remainder=True)

for seq in sequences.take(1):
  print(chars_from_ids(seq))

def split_input_target(sequence):
    input_text = sequence[:-1]
    target_text = sequence[1:]
    return input_text, target_text

dataset = sequences.map(split_input_target)

for input_example, target_example in dataset.take(1):
    print("Input :", text_from_ids(input_example).numpy())
    print("Target:", text_from_ids(target_example).numpy())

# Batch size
BATCH_SIZE = 64

# Buffer size to shuffle the dataset
# (TF data is designed to work with possibly infinite sequences,
# so it doesn't attempt to shuffle the entire sequence in memory. Instead,
# it maintains a buffer in which it shuffles elements).
BUFFER_SIZE = 100

dataset = (
    dataset
    .shuffle(BUFFER_SIZE)
    .batch(BATCH_SIZE, drop_remainder=True)
    .prefetch(tf.data.experimental.AUTOTUNE))

dataset

# Length of the vocabulary in chars
vocab_size = len(vocab)

# The embedding dimension
embedding_dim = 256

# Number of RNN units
rnn_units = 1024

class MyModel(tf.keras.Model):
  def __init__(self, vocab_size, embedding_dim, rnn_units):
    super().__init__(self)
    self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
    self.gru = tf.keras.layers.GRU(rnn_units,
                                   return_sequences=True,
                                   return_state=True)
    self.dense = tf.keras.layers.Dense(vocab_size)

  def call(self, inputs, states=None, return_state=False, training=False):
    x = inputs
    x = self.embedding(x, training=training)
    if states is None:
      states = self.gru.get_initial_state(x)
    x, states = self.gru(x, initial_state=states, training=training)
    x = self.dense(x, training=training)

    if return_state:
      return x, states
    else:
      return x

model = MyModel(
    # Be sure the vocabulary size matches the `StringLookup` layers.
    vocab_size=len(ids_from_chars.get_vocabulary()),
    embedding_dim=embedding_dim,
    rnn_units=rnn_units)

for input_example_batch, target_example_batch in dataset.take(1):
    example_batch_predictions = model(input_example_batch)
    print(example_batch_predictions.shape, "# (batch_size, sequence_length, vocab_size)")

model.summary() # <-- This is where the code stops working 

我的尝试:重新启动运行时,更改缓冲区大小并定义输入形状。

当我定义输入形状并继续编写代码时,我得到了:

sampled_indices = tf.random.categorical(example_batch_predictions[0], num_samples=1)
sampled_indices = tf.squeeze(sampled_indices, axis=-1).numpy()

ERROR: example_batch_predictions is not defined

无论哪种方式,我都会收到错误消息。我该如何解决这个问题?任何建议都非常感谢。

【问题讨论】:

【参考方案1】:

如果您尝试将一些数据传递给您的模型,就像您尝试使用以下行一样:example_batch_predictions = model(input_example_batch)(在您的 for 循环中),您的模型的摘要将起作用,但请注意在您的循环中没有打印任何内容。问题是您正在使用 example_texts,它包含两个字符串,而您仍在使用 64 的 batch_size 和 100 的 sequence_length。如果您将 batch_size 更改为 2,将 sequence_length 更改为 5 ,你应该会看到这样的输出:

Length of text: 100 characters
20 unique characters
a
c
s
s
s
p
s
k
h
c
tf.Tensor([b'a' b'c' b's'], shape=(3,), dtype=string)
Input : b'ac'
Target: b'cs'
(1, 2, 21) # (batch_size, sequence_length, vocab_size)
Model: "my_model_13"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_13 (Embedding)     multiple                  5376      
_________________________________________________________________
gru_13 (GRU)                 multiple                  3938304   
_________________________________________________________________
dense_13 (Dense)             multiple                  21525     
=================================================================
Total params: 3,965,205
Trainable params: 3,965,205
Non-trainable params: 0
_________________________________________________________________

【讨论】:

以上是关于RNN 模型错误:“ValueError:此模型尚未构建。”的主要内容,如果未能解决你的问题,请参考以下文章

RNN+CTC 模型似乎没有正确获取数据维度

tf.nn.dynamic_rnn 中的排名错误

在 Tensorflow RNN 中,logits 和标签必须是可广播的错误

多输入双向RNN错误值错误?

python RNN LSTM错误

Recurrentshop 和 Keras:多维 RNN 导致维度不匹配错误