blei-lab / edward

A probabilistic programming language in TensorFlow. Deep generative models, variational inference.
http://edwardlib.org
Other
4.83k stars 760 forks source link

Save, Restore model, weights, inference #914

Closed omarperezr closed 6 years ago

omarperezr commented 6 years ago

Hello, I'm new with Edward and I have a trained Bayesian Neural Network that works perfectly but I want to save it so I can train it with new information in the future, I tried pickle but it gives me an error, I tried tensorflow.train.Saver() and I managed to save the session, but after that when I do saver.restore(sess, "path/of/model") how do I access the weights and the inference?

I need the posterior weight because I have a function like this to predict

def sample(qW_0, qW_1, qW_2, qb_0, qb_1, qb_2, n_samples=1000):

    qW_0_samples = qW_0.sample(sample_shape=n_samples)
    qW_1_samples = qW_1.sample(sample_shape=n_samples)
    qW_2_samples = qW_2.sample(sample_shape=n_samples)
    qb_0_samples = qb_0.sample(sample_shape=n_samples)
    qb_1_samples = qb_1.sample(sample_shape=n_samples)
    qb_2_samples = qb_2.sample(sample_shape=n_samples)

    print("Preparing to sample...")
    samplenodes = tf.stack([neural_network(
                            X, qW_0_samples[i], qW_1_samples[i], qW_2_samples[i],
                            qb_0_samples[i], qb_1_samples[i], qb_2_samples[i]) for i in range(n_samples)],
                            axis=0)

    print("Sampling...")
    samplepredictions = samplenodes.eval(feed_dict={X: data_test_scaled})

    probs = []
    centers = []

    for i in range(len(data_test_scaled)):
        histogram = np.histogram(samplepredictions[:, i], bins=20)
        probs.append(histogram[0] / n_samples)
        delta = histogram[1][1] - histogram[1][0]
        centers.append([np.float32(a + delta / 2) for a in histogram[1][:-1]])

    y_post = []

    for i in range(len(data_test_scaled)):
        print("Forming the posterior predictive distribution for test data point", i+1, "/", len(data_test_scaled), "...")
        y_post.append(Mixture(Categorical(probs = probs[i]),
                    [Normal(loc=centers[i][j], scale=1.0) for j in range(len(centers[i]))]))

    print("Sampling the posterior predictive distribution for", len(data_test_scaled), "test data points...")
    posteriorsamplenodes = tf.stack([y_post[i].sample(n_samples) for i in range(len(data_test_scaled))], axis=1)
    posteriorsamples = pd.DataFrame(posteriorsamplenodes.eval())

    results['ed_prediction'] = posteriorsamples.mean()                   # predictions
    results['ed_prediction_low'] = posteriorsamples.quantile(0.01)  # predictions_low
    results['ed_prediction_high'] = posteriorsamples.quantile(0.99) # predictions_high

    return results

If you could help or at least point me in the right direction would mean a lot to me, Thanks in advance!

@dustinvtran @akucukelbir @mariru