Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

I use tensorflow version 1.10.0 but error when evaluate model from tensorflow text classification example

Below is the code I write from text classification example

    import tensorflow as tf
    from tensorflow import keras
    import numpy as np
    imdb = keras.datasets.imdb
    (train_data, train_labels),(test_data, test_labels) = 
    imdb.load_data(num_words=10000)
    word_index = imdb.get_word_index()
    word_index = {k:(v+3) for k,v in word_index.items()}
    word_index["<PAD>"] = 0
    word_index["<START>"] = 1
    word_index["<UNK>"] = 2  # unknown
    word_index["<UNUSED>"] = 3
    reverse_word_index = dict([(value, key) for (key, value) in 
    word_index.items()])
    def decode_review(text):
    return ' '.join([reverse_word_index.get(i, '?') for i in text])
    decode_review(train_data[0])
    train_data = 
    keras.preprocessing.sequence.pad_sequences(train_data,value=word_index[" 
   <PAD>"], padding='post',maxlen=256)
    vocab_size = 10000

I make sequential model below

 model = keras.Sequential()
   model.add(keras.layers.Embedding(vocab_size, 16))
   model.add(keras.layers.GlobalAveragePooling1D())
   model.add(keras.layers.Dense(16, activation=tf.nn.relu))
   model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
   model.summary()
   model.compile(optimizer=tf.train.AdamOptimizer(),
              loss='binary_crossentropy',
              metrics=['accuracy'])

   x_val = train_data[:10000]
   partial_x_train = train_data[10000:]
   y_val = train_labels[:10000]
   partial_y_train = train_labels[10000:]

I train the model using model fit function and no error so far

   history = model.fit(partial_x_train,
                    partial_y_train,
                    epochs=40,
                    batch_size=512,
                    validation_data=(x_val, y_val),
                    verbose=1)

I evaluate the model using this function :

 result = model.evaluate(test_data, test_labels)
   print(result)

After successfully training the model then I evaluate but got this error message The error I had ValueError: setting an array element with a sequence.

 ValueError                                Traceback (most recent call last)
<ipython-input-42-e2f1366592d5> in <module>
----> 1 result = model.evaluate(test_data, test_labels)
      2 print(result)

~\Miniconda3\envs\py37\lib\site-packages\tensorflow\python\keras\engine\training.py in evaluate(self, x, y, batch_size, verbose, sample_weight, steps)
   1444       return training_arrays.test_loop(
   1445           self, inputs=x, targets=y, sample_weights=sample_weights,
-> 1446           batch_size=batch_size, verbose=verbose, steps=steps)
   1447 
   1448   def predict(self, x, batch_size=None, verbose=0, steps=None):

~\Miniconda3\envs\py37\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py in test_loop(model, inputs, targets, sample_weights, batch_size, verbose, steps)
    482         ins_batch[i] = ins_batch[i].toarray()
    483 
--> 484       batch_outs = f(ins_batch)
    485 
    486       if isinstance(batch_outs, list):

~\Miniconda3\envs\py37\lib\site-packages\tensorflow\python\keras\backend.py in __call__(self, inputs)
   2898         tensor_type = dtypes_module.as_dtype(tensor.dtype)
   2899         array_vals.append(np.asarray(value,
-> 2900                                      dtype=tensor_type.as_numpy_dtype))
   2901 
   2902     if self.feed_dict:

~\Miniconda3\envs\py37\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    499 
    500     """
--> 501     return array(a, dtype, copy=False, order=order)
    502 
    503 

ValueError: setting an array element with a sequence.

Comments