-1

Currently studying the 'Deep Learning with Python' book by Francios Chollet. I am very new to this and I am getting this error code despite following his code verbatim. Can anyone interpret the error message or what needs to be done to solve it? Any help would be greatly appreciated!

from keras.datasets import imdb
import numpy as np
from keras import models
from keras import layers

(train_data, train_labels), (test_data, test_labels) =
imdb.load_data(num_words=10000)


def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
       results[i, sequence] = 1. 
    return results
x_train = vectorize_sequences(train_data)
y_train = vectorize_sequences(test_data)
x_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')


model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', 
         loss='binary_crossentropy', 
         metrics=['accuracy'])

model.fit(x_train, y_train, epochs=4, batch_size=512)
results = model.evaluate(x_test, y_test)

Edit: Here is an image of the error code that I am getting: enter image description here

rmahesh
  • 739
  • 2
  • 14
  • 30
  • Can you show us the error you get ? – vvvvv Jan 06 '18 at 19:41
  • What is the error message and where exactly in the code it pops up?? – desertnaut Jan 07 '18 at 00:46
  • @vinzee I have added the error code. – rmahesh Jan 07 '18 at 01:46
  • @desertnaut The problem that I am having is that it is showing up in parts of code that I did not write (if that makes sense). For example, one is showing that there is something wrong with my 'batch_size=512' statement but the rest are not code that I have written myself. – rmahesh Jan 07 '18 at 01:48
  • A related question: https://stackoverflow.com/questions/68422410/standard-implementation-of-vectorize-sequences – zabop Jul 17 '21 at 16:56

1 Answers1

1

I tested your Code and found, that x_test was not defined. I think you meant to vectorize it as follows. With this Code it worked:

x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')
Yannick
  • 337
  • 3
  • 13