AttributeError: 'list' object has no attribute 'argmax' and numpy.AxisError: axis 2 is out of bounds for array of dimension 1

让人想犯罪 __ 提交于 2020-03-21 07:00:40

问题


i have a function for predicting keras NMT model and it works fine but its reading the file and then save the prediction to another file

but i want user input and make a prediction

full code

from keras.preprocessing.sequence import pad_sequences
from keras.models import load_model
import sys
import warnings
import argparse
from seq2seq_utils import *

ap = argparse.ArgumentParser()
ap.add_argument('-max_len', type=int, default=95)
ap.add_argument('-vocab_size', type=int, default=31500)

args = vars(ap.parse_args())
MAX_LEN = args['max_len']
VOCAB_SIZE = args['vocab_size']


def load_test_data(user_input, X_word_to_ix, max_len):
    user_input = input()
    X = [text_to_word_sequence(x)[::-1] for x in user_input.split('\n') if 0 < len(x) <= max_len]
    for i, sentence in enumerate(X):
        for j, word in enumerate(sentence):

            if word in X_word_to_ix:
                X[i][j] = X_word_to_ix[word]
            elif word in X_word_to_ix is None:
                X[i][j] = None
            else:
                X[i][j] = X_word_to_ix['UNK']

    return X


model = load_model('model.h5')
model.get_weights()
X, X_vocab_len, X_word_to_ix, X_ix_to_word, y, y_vocab_len, y_word_to_ix, y_ix_to_word = load_data('english.txt',
                                                                                                   'french.txt',
                                                                                                   MAX_LEN,
                                                                                                   VOCAB_SIZE)

saved_weights = find_checkpoint_file('.')
print('please enter the value')
user_input1 = input()
if len(saved_weights) == 0:
    print("The network hasn't been trained! Program will exit...")
    sys.exit()
else:
    X_test = load_test_data(user_input1, X_word_to_ix, MAX_LEN)
    print(type(X_test))
    X_test = pad_sequences(X_test, maxlen=4, dtype='int32')
    print(type(X_test))
    arr=np.array(X_test)

    model.load_weights(saved_weights)
    predictions = np.argmax(model.predict(arr))
    # predictions = np.argmax(list(model.predict(X_test)))
    print(type(predictions))

    # predictions = np.argmax(model.predict(X_test),axis=0)
    sequences = []
    print('1')
    for prediction in predictions:
        print('2')
        sequence = ' '.join([y_ix_to_word[index] for index in prediction if index > 0])
        print(sequence)
        l = sequences.append(sequence)
        print(l)
    np.savetxt('test_result.txt', sequences, fmt='%s')

AttributeError: 'list' object has no attribute 'argmax'

During handling of the above exception, another exception occurred:

numpy.AxisError: axis 2 is out of bounds for array of dimension 1

why? and thanks for helping

来源:https://stackoverflow.com/questions/60585905/attributeerror-list-object-has-no-attribute-argmax-and-numpy-axiserror-axi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!