0
votes

I am using below code to predict next word using GRU.

import numpy as np
shakespeare_url = "https://homl.info/shakespeare"
filepath = keras.utils.get_file("shakespeare.txt",shakespeare_urlspeare_url)

with open(filepath) as f:
    shakespeare_txt = f.read()
    
tokenizer = keras.preprocessing.text.Tokenizer(char_level=True)
tokenizer.fit_on_texts(shakespeare_txt)
max_id = len(tokenizer.word_index) ## Number of distinct words
dataset_size = tokenizer.document_count ## total number of character
[encoded] = np.array(tokenizer.texts_to_sequences([shakespeare_txt])) - 1
train_size = (dataset_size * 90) // 100
dataset = tf.data.Dataset.from_tensor_slices(encoded[:train_size])
n_steps = 100
window_length = n_steps +1
dataset = dataset.window(window_length,shift=1,drop_remainder=True)
dataset = dataset.flat_map(lambda window : window.batch(window_length))
batch_size =32
dataset = dataset.shuffle(10000).batch(batch_size)
dataset = dataset.map(lambda windows : (windows[:,:-1],windows[:,1:]))
dataset = dataset.map(lambda X_batch,Y_batch : (tf.one_hot(X_batch,depth = max_id),Y_batch))
dataset = dataset.prefetch(1)
model = keras.models.Sequential([
    keras.layers.GRU(128, return_sequences=True, input_shape =[None,max_id], dropout=0.2,recurrent_dropout=0.2),
    keras.layers.GRU(128,return_sequences=True,dropout=0.2,recurrent_dropout=0.2),
    keras.layers.TimeDistributed(keras.layers.Dense(max_id,activation='softmax'))
])
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam')
history = model.fit(dataset,epochs=20)

Getting below Exception. Please help me to resolve this issue??

AttributeError Traceback (most recent call last) in ----> 1 history = model.fit(dataset,epochs=20)

c:\users\dixit\appdata\local\programs\python\python38\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 1148 1149 # Case 2: Symbolic tensors or Numpy array-like. -> 1150 x, y, sample_weights = self._standardize_user_data( 1151 x, y, 1152 sample_weight=sample_weight,

c:\users\dixit\appdata\local\programs\python\python38\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size) 572 573 # Standardize the inputs. --> 574 x = training_utils.standardize_input_data( 575 x, 576 feed_input_names,

c:\users\dixit\appdata\local\programs\python\python38\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 97 data = data.values if data.class.name == 'DataFrame' else data 98 data = [data] ---> 99 data = [standardize_single_array(x) for x in data] 100 101 if len(data) != len(names):

c:\users\dixit\appdata\local\programs\python\python38\lib\site-packages\keras\engine\training_utils.py in (.0) 97 data = data.values if data.class.name == 'DataFrame' else data 98 data = [data] ---> 99 data = [standardize_single_array(x) for x in data] 100 101 if len(data) != len(names):

c:\users\dixit\appdata\local\programs\python\python38\lib\site-packages\keras\engine\training_utils.py in standardize_single_array(x) 32 'Got tensor with shape: %s' % str(shape)) 33 return x ---> 34 elif x.ndim == 1: 35 x = np.expand_dims(x, 1) 36 return x

AttributeError: 'PrefetchDataset' object has no attribute 'ndim'

1

1 Answers

1
votes

Make sure to import keras from tensorflow and use version 2.2.4-tf. Got the same error and this worked for me.

from tensorflow import keras
keras.__version__

2.2.4-tf