简体   繁体   中英

Keras Predict Single Example with keras.predict?

I am sure/hopeful that this is a very simple problem, but I haven't been able to find any answers.

I've made a sequential model in Keras with 366 inputs neurons and one output neuron. It seems to train and evaluate fine, but whenever I try to predict a single example, I get a numpy array of shape (366, 1) despite model.output_shape being (None, 1) .

I'm aware that a very similar question exists here , but unfortunately none of the proposed solutions solved my problem.

From what I've read, this is because Keras is treating every input as a separate example for which to make a prediction, however, this hasn't helped me so far. I've tried passing the input as a numpy array of size (366, 1) , (1, 366) , (366,) , and a list containing each of those variants, but nothing has worked ( (1, 366) threw an error, and all of the others had an output of size of (366, 1) ).

If anyone could help with this, it would be greatly appreciated. Thanks.

Here is the code: (Sorry if it's not very neat)

For training:

import example_generator as eg
import numpy as np
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

training_data = np.load("../data/training_data.npy", allow_pickle=True)
testing_data = np.load("../data/testing_data.npy", allow_pickle=True)

model = Sequential([
    Dense(100, activation="relu"),
    Dense(30, activation="relu"),
    Dense(1, activation="linear")
])

model.compile(
    optimizer = "Adam",
    loss="mean_squared_error",
    metrics="mean_absolute_error"
)

model.fit(
    eg.yield_training_example(training_data, 366),
    epochs=1,
    steps_per_epoch = 14590,
    batch_size=50
)

model.evaluate(
    eg.yield_training_example(testing_data, 366),
    steps = 50
)

model.save("../models/model")

For testing:

import example_generator as eg
import numpy as np
from tensorflow.keras.models import load_model

testing_data = np.load("../../data/testing_data.npy", allow_pickle=True)
model = load_model("../../data/models/feedforward")

testing_example = next(eg.yield_training_example(testing_data, 366))
X = testing_example[0]

prediction = model.predict(
    X
)

print(f"Prediction: {prediction}\nAnswer: {testing_example[1]}\n\n")

For the generator that I'm using to return examples:

import numpy as np

# Acts as an iterable of training examples
def yield_training_example(data, num_nn_inputs = 366):
    for eg in data[:, 1]:
        inc = 0
        while (inc <= (len(eg) - num_nn_inputs - 1)):
            yield (
                np.array(eg[inc:(inc + num_nn_inputs), :]).astype(np.float32),
                np.array(eg[(inc + num_nn_inputs), :]).astype(np.float32)
            )
            inc += 1

Ok, I ended up fixing this. I needed to specify the input dimensions when making my model, like this:

model = Sequential([
    Dense(100, activation="relu", input_dim=366),
    Dense(30, activation="relu"),
    Dense(1, activation="linear")
])

From there I just had to make sure all of the inputs to the network were passed as arrays of shape (1, 366) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM