0

I am new to data science and I am still learning machine learning. I know we can use Regression, Classification, Clustering, ANN, CNN, RNN models and so on according to the application.

These models we code, training and predict some data in PC. Some models take to much time training also. After that, we shut down the PC.

If I want the same model with the same data set after some days, again open the PC and training same model.

I want to know how to use trained modal in future without training, again and again, every time PC open. I asking mostly for ANN, CNN, RNN models.

Also, I want to know where the weights values are stored for modal because weights are not stored in the variable. How can I find it and can not I use those trained weight data to give ANN in future.

Morse
  • 8,258
  • 7
  • 39
  • 64
Ind
  • 377
  • 2
  • 6
  • 16

3 Answers3

2

Usually, simple models (e.g. Logistic Regression, Decision Tree) don't take a huge amount of time training, obviously depending on the size of the data you train them on.

On the other hand, deep learning models, tend to have high training time. A common technique is to save the trained model(s) using the HDF5 file format. In case you are interested, you can check this link for further info on the format.

Probably the most simple way to achieve this is by using Keras's built-in function model.save:

from keras.models import load_model

model = train_neural_network() # Train your model

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
model = load_model('my_model.h5') # Load your saved model and use it on whatever data you want

Since you are a data science begginer, if you have some basic knowledge on the area and want to jump directly onto deep learning, I recommend using Google's Colaboratory (link).

Each user is assigned a virtual machine with hardware built specifically for tasks involving deep learning. It contains most dependencies you need to run neural networks.

GRoutar
  • 1,311
  • 1
  • 15
  • 38
1

Saving a fully-functional model is very useful—you can load them. In TensorFlow, you can save the entire model to a file that contains the weight values, the model's configuration, and even the optimizer's configuration. You may do:

model.save('ModelName.model')

Also, Keras provides a basic save format using the HDF5 standard.

#Save entire model to a HDF5 file model.save('my_model.h5')

For more details check the documentation

For weights, for example, if you have a binary classifier with twice as many in the 0 label as the 1 label, you may set them when fitting the model like this:

 Class_Weights = {0: 1., 1: 2} #twice as many 0 as 1

 #fit model and pass weights
 model.fit(X, y, class_weight=Class_Weights
           batch_size=20, epochs=5, validation_split=0.3,)
Suleiman
  • 316
  • 1
  • 4
  • 15
0

If you just use TensorFlow, you can use the SavedModel API. This is Tom's answer. Also, you can find an example in github by Wen

WEN WEN
  • 126
  • 2
  • 10