Let's say I've trained my model and made my predictions.
My question is... How can I append some new data to my pre-trained model without retrain the model from the beginning.
Let's say I've trained my model and made my predictions.
My question is... How can I append some new data to my pre-trained model without retrain the model from the beginning.
It all depends on the specific algorithm you're using. Some of them support incremental learning, while others don't.
For example, in the case of scikit-learn, using fit() more than once on the same model will simply overwrite the model's weights each time (see here for more details).
What you can do however, is look for algorithms that implement the partial_fit api, and use this to retrain your existing models - see the documentation for a list of algorithms that support incremental learning and thus implement this api.
An alternative solution is to look for algorithms that support the warm_start parameter, e.g. LogisticRegression. Note that warm_start might also be influenced by other parameters, so you need to pay attention to their values, too - e.g. in the case of LogisticRegression, warm_start won't work if you use the liblinear solver (which is the default).
I would say it depends upon the ML framework you are using. I have worked on Scikit and Tensorflow.
Both works in a different way.
Scikit:
Tensorflow: Consider a basic TF code as mentioned below:
import numpy as np
import tensorflow as tf
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler
housing = fetch_california_housing()
m, n = housing.data.shape
target = housing.target.reshape(-1,1)
scaler = StandardScaler()
scaled_housing_data = scaler.fit_transform(housing.data)
housing_data_plus_bias = np.c_[np.ones((m, 1)), scaled_housing_data]
n_epochs = 1000
learning_rate = 0.01
X = tf.placeholder(shape = (None, n+1), dtype=tf.float32, name="X")
y = tf.placeholder(shape=(None,1), dtype=tf.float32, name="y")
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed=42), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
gradients = 2/m * tf.matmul(tf.transpose(X), error)
training_op = tf.assign(theta, theta - learning_rate * gradients)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
fn,wights, loss, pred, op = sess.run((error, theta, mse, y_pred,training_op), feed_dict={X:housing_data_plus_bias, y:target})
print(loss)
best_theta = theta.eval()
print(best_theta)
Consider I have trained and deployed this model(as a tf.saver() object). Now if I want to predict new values I have to feed my model with new X values and identify "y_pred".. This process will not update my theta(weights) values(tensor graph, evaluate its dependent tensors only) and for prediction model will use existing theta. In case I want to update theta, using the new samples, I have to evaluate training_op.
In this way we can improve TF model later with the new data.
Note: training_op, has a dependency on theta and gradient.
I cannot comment yet. If you just load the model and use a fit method it will update the weights, not reinstance all the weights. It will just perform a number of weights update that you can chose, using the new data.
fit() more than once will overwrite what was learned by any previous fit().
– Vlad
Sep 24 '18 at 07:15
fit() method do ? update or overwrite from scratch ?
– Bhanuchander Udhayakumar
Jan 03 '19 at 05:40
fiton a trained model in tf.keras updates the weights. This can be seen by a very low loss in the first epoch of the second call to fit. – Alireza Amani Nov 10 '21 at 16:36