Как импортировать набор данных imdb с помощью Colab?

Я пытаюсь импортировать набор данных imdb из keras на платформе CoLab.

и я получил эту ошибку:

AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _get_default_graph()
     65     try:
---> 66         return tf.get_default_graph()
     67     except AttributeError:

AttributeError: модуль 'tensorflow' не имеет атрибута 'get_default_graph'

Во время обработки вышеуказанного исключения произошло другое исключение:

RuntimeError                              Traceback (most recent call last)
7 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _get_default_graph()
     67     except AttributeError:
     68         raise RuntimeError(
---> 69             'It looks like you are trying to use '
     70             'a version of multi-backend Keras that '
     71             'does not support TensorFlow 2.0. We recommend '

RuntimeError: похоже, вы пытаетесь использовать версию Keras с несколькими серверными приложениями, которая не поддерживает TensorFlow 2.0. Мы рекомендуем использовать tf.keras или, в качестве альтернативы, перейти на TensorFlow 1.14.

Код, который я использую:

from tensorflow.compat.v1 import keras
from keras.datasets import imdb
(x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz",
                                                  num_words=None,
                                                  skip_top=0,
                                                  maxlen=None,
                                                  seed=113,
                                                  start_char=1,
                                                  oov_char=2,
                                                  index_from=3)

person Nibras Abo Alzahab    schedule 24.11.2019    source источник
comment
Проблема и решение очевидны: RuntimeError: Похоже, вы пытаетесь использовать версию Keras с несколькими серверными приложениями, которая не поддерживает TensorFlow 2.0. Мы рекомендуем использовать tf.keras или, в качестве альтернативы, перейти на TensorFlow 1.14.   -  person Dr. Snoopy    schedule 24.11.2019
comment
Спасибо, но подскажите, пожалуйста, как сделать предложенное решение?   -  person Nibras Abo Alzahab    schedule 25.11.2019
comment
Используйте tf.keras или обновите Keras до версии, которая поддерживает TensorFlow 2.0 (например, Keras 2.3.1), или понизьте TensorFlow до 1.14 / 1.15.   -  person Dr. Snoopy    schedule 25.11.2019


Ответы (1)


Вы можете загрузить набор данных IMDB в TensorFlow, используя следующие методы.

1. Использование tensorflow.keras.datasets:

import tensorflow
from tensorflow.keras.datasets import imdb 

(x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz",
                                                  num_words=None,
                                                  skip_top=0,
                                                  maxlen=None,
                                                  seed=113,
                                                  start_char=1,
                                                  oov_char=2,
                                                  index_from=3) 

Каждый из которых будет иметь 25000 записей как в x_train, так и в x_test

2. Использование tensorflow_datasets:

import tensorflow_datasets as tfds
ds = tfds.load('imdb_reviews', split='train')

for ex in ds.take(4):
  print(ex)

Образец данных:

{'label': <tf.Tensor: shape=(), dtype=int64, numpy=0>, 'text': <tf.Tensor: shape=(), dtype=string, numpy=b"This was an absolutely terrible movie. Don't be lured in by Christopher Walken or Michael Ironside. Both are great actors, but this must simply be their worst role in history. Even their great acting could not redeem this movie's ridiculous storyline. This movie is an early nineties US propaganda piece. The most pathetic scenes were those when the Columbian rebels were making their cases for revolutions. Maria Conchita Alonso appeared phony, and her pseudo-love affair with Walken was nothing but a pathetic emotional plug in a movie that was devoid of any real meaning. I am disappointed that there are movies like this, ruining actor's like Christopher Walken's good name. I could barely sit through it.">}
{'label': <tf.Tensor: shape=(), dtype=int64, numpy=0>, 'text': <tf.Tensor: shape=(), dtype=string, numpy=b'I have been known to fall asleep during films, but this is usually due to a combination of things including, really tired, being warm and comfortable on the sette and having just eaten a lot. However on this occasion I fell asleep because the film was rubbish. The plot development was constant. Constantly slow and boring. Things seemed to happen, but with no explanation of what was causing them or why. I admit, I may have missed part of the film, but i watched the majority of it and everything just seemed to happen of its own accord without any real concern for anything else. I cant recommend this film at all.'>}
{'label': <tf.Tensor: shape=(), dtype=int64, numpy=0>, 'text': <tf.Tensor: shape=(), dtype=string, numpy=b'Mann photographs the Alberta Rocky Mountains in a superb fashion, and Jimmy Stewart and Walter Brennan give enjoyable performances as they always seem to do. <br /><br />But come on Hollywood - a Mountie telling the people of Dawson City, Yukon to elect themselves a marshal (yes a marshal!) and to enforce the law themselves, then gunfighters battling it out on the streets for control of the town? <br /><br />Nothing even remotely resembling that happened on the Canadian side of the border during the Klondike gold rush. Mr. Mann and company appear to have mistaken Dawson City for Deadwood, the Canadian North for the American Wild West.<br /><br />Canadian viewers be prepared for a Reefer Madness type of enjoyable howl with this ludicrous plot, or, to shake your head in disgust.'>}
{'label': <tf.Tensor: shape=(), dtype=int64, numpy=1>, 'text': <tf.Tensor: shape=(), dtype=string, numpy=b'This is the kind of film for a snowy Sunday afternoon when the rest of the world can go ahead with its own business as you descend into a big arm-chair and mellow for a couple of hours. Wonderful performances from Cher and Nicolas Cage (as always) gently row the plot along. There are no rapids to cross, no dangerous waters, just a warm and witty paddle through New York life at its best. A family film in every sense and one that deserves the praise it received.'>}  

Это также будет иметь такое же разделение, как указано выше.

person Tensorflow Warrior    schedule 04.06.2020
comment
@Nibras Abo Alzahab Если ваша проблема решена с использованием вышеуказанного подхода, примите и проголосуйте за ответ. - person Tensorflow Warrior; 04.06.2020