Hiding
Hiding

Reputation: 278

Using Tensorflow's numpy_input_fn is throwing "list object has no attribute shape"

I am trying to create a text classifier with an RNN. The classifier.train line is throwing the error:

    model_fn = rnn_model
    classifier = tf.estimator.Estimator(model_fn=model_fn)

    # Train.
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={WORDS_FEATURE: x_train},
        y=y_train,
        batch_size=len(x_train),
        num_epochs=None,
        shuffle=True)
    classifier.train(input_fn=train_input_fn, steps=100)

This is what x_train looks like:

MAX_DOCUMENT_LENGTH = 50000
...
x_train = depTrain_data[:]
...
vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(MAX_DOCUMENT_LENGTH)
...
x_transform_train = vocab_processor.fit_transform(x_train)
...
x_train = np.array(list(x_transform_train))

This is the error: enter image description here

I am using Python 3.4 and Tensorflow 1.4

I know I need to change a list to np.array but I do not know where.

Upvotes: 0

Views: 773

Answers (2)

Blue482
Blue482

Reputation: 3136

I don't know why this question is being down voted, it is a legitimate question.

The answer is your y_train is most likely a list, convert it to numpy array should fix the issue.

Upvotes: 1

mrry
mrry

Reputation: 126154

The tf.estimator.inputs.numpy_input_fn() function requires all values in the x dictionary to be NumPy arrays. You can perform the necessary conversion as follows:

train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={WORDS_FEATURE: np.array(x_train)},  # Convert `x_train` to a NumPy array.
    y=y_train,
    batch_size=len(x_train),
    num_epochs=None,
    shuffle=True)

Note that this will only work if x_train is a list of lists in which each nested list has the same length. If not, you will need to pad each of the nested lists to the same length.

Upvotes: 1

Related Questions