3
$\begingroup$

I have defined, trained and saved my tensor keras NN. Now that that is complete how do I use it output classifications to non training data?

import tensorflow as tf
import numpy as np
from tensorflow.keras import layers
from syslog import syslog_pred

model = tf.keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(128, activation='relu'))
# Add another:
model.add(layers.Dense(128, activation='relu'))
# Add a softmax layer with 8 output units:
model.add(layers.Dense(8, activation='softmax'))

model.compile(optimizer=tf.train.AdamOptimizer(0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

model.load_weights('.my_model')

x = np.array([arr[:-1] for arr in syslog_pred], dtype=np.float32)
dataset = tf.data.Dataset.from_tensor_slices(x)

answer = model.predict(dataset, steps=30)
print(answer)

The code at the end isn't what it should be but I'm a little lost. Any help would be appreciated!

$\endgroup$
3
$\begingroup$

Once you have a trained model, you can pass new samples to it by using the predict method of the model, which will get you the probabilities of all classes. You than pick the class with the highest probability for each sample as the predicted class:

y_prob = model.predict(new_data)   # Get class probability vector for each sample
y_class = y_prob.argmax(axis=-1)   # Set prediction to class with highest probability

If you are using a Sequential() model, you can also use the predict_classes method, which will get you the same answer:

y_class = model.predict_classes(new_data)
$\endgroup$
2
$\begingroup$

To predict classes you simply need:

answer = model.predict_classes(dataset)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.