I'm testing the RNN model of mxnet. The tutorial here does not work and the error message said many functions had been deprecated. I did not find the up-to-date tutorial for RNN.
There are still some examples in the mxnet project. But for RNN, the examples only show how to train a model using training set. They don't show how to use the trained model to make further prediction. The training code is as follows:
model.fit(
train_data = data_train,
eval_data = data_val,
eval_metric = mx.metric.Perplexity(invalid_label),
kvstore = args.kv_store,
optimizer = args.optimizer,
optimizer_params = { 'learning_rate': args.lr,
'momentum': args.mom,
'wd': args.wd },
initializer = mx.init.Xavier(factor_type="in", magnitude=2.34),
num_epoch = args.num_epochs,
batch_end_callback = mx.callback.Speedometer(args.batch_size, args.disp_batches))
Does someone know how to use the trained RNN model to make inference or prediction?
I must clearify that I'm looking for how to use RNN model to make prediction, not CNN or other models.
Thank you very much for helping me!!!
Usually model is extends BaseModel class. And BaseModel has the method predict. The method can work with same type that is used by fit method: DataIter with only one difference, it does not require train_data, only eval_data. So the actual prediction process can be implemented in a simple way like this:
result = mod.predict(dataiter.next)
Related
I trained a Pytorch model, saved the testing error, and saved the complete model using torch.save(model, 'model.pt')
I loaded the model to test it on another dataset and found the error to be higher, so I tested it on the exact same dataset as before, and found the results to be different:
Here, there is not a lot of difference in the predicted values which tells me that the model is correct, but somehow different.
1 difference is that originally the model was trained on GPUs with nn.DataParallel, and while testing after loading, I am evaluating it on CPU.
model = torch.load('model.pt')
model = model.module # To remove the DataParallel module
model.eval()
with torch.no_grad():
x = test_loader.dataset.tensors[0].cuda()
pred = model(x)
mae_loss = torch.nn.L1Loss(reduction='mean')
mae = mae_loss(pred, y)
What could be causing this difference in model evaluation? Thank you in advance
I am working on a project involving neural machine translation (translating English to French).
I have worked through some examples online, and have now finished the model. Once a model is trained using Keras, how do I then get a prediction of a translation without training the entire model again, because with the large dataset I am using, each epoch takes some time and of course, I can't train the model every time I want a translation.
So what is the correct way of then generating predictions on new inputs without training the whole model again?
Thanks
You need to save your model the model and its weights when the fit ends using :
keras.model.save(model_name)
At any time, you can load your trained model using
model = keras.load(model_name)
then perform predictions as
y_pred = model.predict(x_test)
Hope this will be helpful
You can use the .predict() function which you can pass new inputs into it and it give you a prediction. The docs for this function are here: keras
I am training a CNN model for Image Classification using Keras. I am using VGG19 model and a custom dataset with 200 classes and uniformly distributed 90000 training images, 10000 Validation Images and 10000 test images. Even though the training is at 200 epochs, the accuracy is staying at a constant 0.0050. Same with the loss, 5.2988. I am using Kaggle's TPU instance to run this model.
How can I make the model more accurate? Can you suggest any different pretrained models for this purpose?
Your CNN model is behaving like a random model.
I know this because since there are 200 classes, the probability of getting a correct class at random is 1/200=0.0050 which is the accuracy that you have.
This happens when you use tensorflow/keras API instead of sequential()
Since you are using VGG19, if you are trying to use transfer learning, then maybe you have freezed the wrong layer.
If you are using API then you have to do
model = Model(inputs = input_layer, outputs = output_layer) #which is not required in sequential()
print(model.layers) # if you are using API or sequential() this is used to check your layers
Then you have to freeze the layer required as
model.layers[index_of_freeze_layer].trainable = False
If you are not freezing your model layers, then try to use lower learning rate since VGG19 is very sensitive to learning rate. (0.00001 or less depends)
I have trained a cnn model using tf.estimator and tf.data.TFRecordDataset, which define a model in model_fn funcition and input in input_fn function. Also using an one-shot iterator to get one batch examples at a time.
Now I have trained model files(ckpt, meta, index) in a directory. What I want to do is predicting a image's label based on the trained model without training and evaluation again. The image can be numpy array but not possible a TFRecords file(which used when traing).
I can't find an effictive solution after trying all day. I only can get the value of weights and biases and don't know how to make my predicting image and model compatible.
FYI, my training code is here.
The similar question is Prediction from model saved with tf.estimator.Estimator in Tensorflow
, but no accepted answer and my model input is using the dataset api.
So reaaally need help. Thanks.
I have answered a similar question here.
To make predictions using a custom input, you need to use the built-in predict method of Estimators:
estimator = tf.estimator.Estimator(model_fn, ...)
predict_input_fn = ... # define this using tf.data
predict_results = estimator.predict(predict_input_fn)
for idx, prediction in enumerate(predict_results):
print(idx)
for key in prediction:
print("...{}: {}".format(key, prediction[key]))
I am building a CNN model with Keras and tensorflow backend. I've trained the model for 6 hours. Now, I want to predict my custom external image using my model. How to do that in Keras? Thank you
To simplify, you could say there is usually 4 main steps when working with models (not just in Keras) :
building the model and compiling it : model.compile()
training the model with your training data : model.fit()
evaluating the model with your test data : model.evaluate()
making predictions with your target data : model.predict()
Depending on which output you need, you will have to use model.predict_proba() or model.predict_classes().
See https://keras.io/models/sequential/#sequential-model-methods for complete reference and arguments.
Keras repository has also plenty of examples : https://github.com/fchollet/keras/tree/master/examples