【问题标题】:TensorFlow 2.0 Tutorial IssueTensorFlow 2.0 教程问题
【发布时间】:2019-09-06 03:29:54
【问题描述】:

我正在关注https://www.tensorflow.org/alpha/tutorials/sequences/text_classification_rnn的官方教程,但遇到了问题。以下行导致错误:

train_dataset = train_dataset.padded_batch(BATCH_SIZE, train_dataset.output_shapes)

Traceback(最近一次调用最后一次): 文件“main.py”,第 30 行,在 train_dataset = train_dataset.padded_batch(BATCH_SIZE, train_dataset.output_shapes) AttributeError:“ShuffleDataset”对象没有属性“output_shapes”

我错过了什么? 这是我完成了一半的代码:

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow_datasets as tfds
import tensorflow as tf
import matplotlib.pyplot as plt
import tensorflow.keras

def plot_graphs(history, string):
    plt.plot(history.history[string])
    plt.plot(history.history['val_'+string])
    plt.xlabel("Epochs")
    plt.ylabel(string)
    plt.legend([string, 'val_'+string])
    plt.show()
dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True,
                          as_supervised=True)
train_dataset, test_dataset = dataset['train'], dataset['test']
tokenizer = info.features['text'].encoder
print ('Vocabulary size: {}'.format(tokenizer.vocab_size))
# sample_string = 'TensorFlow is cool.'
# tokenized_string = tokenizer.encode(sample_string)
# print ('Tokenized string is {}'.format(tokenized_string))
# original_string = tokenizer.decode(tokenized_string)
# print ('The original string: {}'.format(original_string))
# assert original_string == sample_string
# for ts in tokenized_string:
#     print ('{} ----> {}'.format(ts, tokenizer.decode([ts])))
BUFFER_SIZE = 10000
BATCH_SIZE = 64
train_dataset = train_dataset.shuffle(BUFFER_SIZE)
train_dataset = train_dataset.padded_batch(BATCH_SIZE, train_dataset.output_shapes)
test_dataset = test_dataset.padded_batch(BATCH_SIZE, test_dataset.output_shapes)

【问题讨论】:

标签: python tensorflow tensorflow2.0


【解决方案1】:

请更换

train_dataset = train_dataset.padded_batch(BATCH_SIZE, train_dataset.output_shapes)

train_dataset = train_dataset.padded_batch(BATCH_SIZE, tf.compat.v1.data.get_output_shapes(train_dataset))

也替换

test_dataset = test_dataset.padded_batch(BATCH_SIZE, test_dataset.output_shapes)

test_dataset = test_dataset.padded_batch(BATCH_SIZE, tf.compat.v1.data.get_output_shapes(test_dataset))

【讨论】:

  • 请添加一些补充说明来解释您的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
相关资源
最近更新 更多