bennyo27
bennyo27

Reputation: 43

Upon prediction, my loaded model is giving me an AttributeError

I'm fairly new to Tensorflow and Machine Learning in general but I know enough that I've built a small model. Although, when loaded and I use model.predict, I get an attribute error:

import tensorflow as tf
import numpy as np

checkpoint_path = "training_1/cp.ckpt"
# Hyperparamters
vocab_size = 2000
embedding_dim = 16
max_length = 1
trunc_type = "post"
padding_type = "post"
oov_tok = "<OOV>"
training_size = 100

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(
        vocab_size, embedding_dim, input_length=max_length),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dense(3, activation="softmax")
])

# Compile the model
model.compile(loss="sparse_categorical_crossentropy",
              optimizer="adam", metrics=["accuracy"])

model.load_weights(checkpoint_path)


test = ["Example of text here"]


prediction = model.predict(test)
print(prediction)
Traceback (most recent call last):
  File "./ModelTest.py", line 36, in <module>
    prediction = model.predict(test)
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1060, in predict
    x, check_steps=True, steps_name='steps', steps=steps)
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2651, in _standardize_user_data
    exception_prefix='input')
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 334, in standardize_input_data
    standardize_single_array(x, shape) for (x, shape) in zip(data, shapes)
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 334, in <listcomp>
    standardize_single_array(x, shape) for (x, shape) in zip(data, shapes)
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 265, in standardize_single_array
    if (x.shape is not None and len(x.shape) == 1 and
AttributeError: 'str' object has no attribute 'shape'

Upvotes: 2

Views: 268

Answers (1)

The Guy with The Hat
The Guy with The Hat

Reputation: 11132

Make sure the input you're giving is in the correct format for the model you build. In your case, an Embedding layer expects a 2D tensor. The data should be a numpy array that looks something like this: [[0, 2, 64], [24, 6, 8]]. Each number there represents a word, and each sequence of numbers represents a phrase. The whole tensor represents a batch of sequences. In my example, that's a batch of 2 sequences, each with 3 words.

What you need to do is tokenize "Example of text here" using the correct vocabulary for the model you're loading. Once you do, you'll get an array like [[3, 8, 4, 6]], where each of those numbers corresponds to one of the words in "Example of text here". How to tokenize it correctly depends on how the data it was trained on was tokenized, and we don't know that without knowing where you got training_1/cp.ckpt from.

Upvotes: 2

Related Questions