Thank you, @vdw! I believe you are right. The data item should be represented like you said:
input_sentence = [1, 4, 5, 7]
target_word = 9

But I also think the prediction should be repeated for each item in the sequence. The reason why I think this is because if we only consider the last element in the sequence, it is the element with the longest sequence of previous elements. So in order to make a fair prediction, it should be repeated for each of the next items in the sequences. Splitting the sequences like this:
input_sentence = [1]
target_word = 4

input_sentence = [1, 4]
target_word = 5

input_sentence = [1, 4, 5]
target_word = 7

input_sentence = [1, 4, 5, 7]
target_word = 9

But I think splitting sequences like this can be hard to combine with minibatching during training. Especially since the sequences have varying lengths.
My idea is therefore to carry out this sequence split before I train and test the model. That is before loading the dataset. And it would lead to that the size of the dataset would increase. I believe this would make it easier to combine session splitting with minibatching.  Do you think that would make sense?
This is how I would create the dataset:
'''
Splitting the sequences and adds more training data like this:

[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
'''
def next_item_sequence_split(data):
    new_data = []
    for d in data:
        splits = [torch.LongTensor(d[:i]) for i in range(2,len(d)+1)]
        new_data.extend(splits)
    random.shuffle(new_data)
    return new_data

'''
Splitting the data into 80% training and 20% testing
'''
def split_dataset(dataset):
    random.shuffle(dataset)
    train_size = int(0.8 * len(dataset))
    train = dataset[0:train_size]
    test = dataset[train_size:-1]
    return train, test 

# split data into training and testing, x is a list of sequences
train, test = split_dataset(x)

x_train = next_item_sequence_split(train)
x_test = next_item_sequence_split(test)

Thanks!