Skip to content

Instantly share code, notes, and snippets.

plt.hist(baby_predictions, color='g', alpha=0.4, histtype='step', label='Babies', fill=True, linewidth=4, density=True)
plt.hist(spider_predictions, color='r', alpha=0.4, histtype='step', label='Spiders', fill=True, linewidth=4, density=True)
plt.xlabel('Predicted Height (pixels)')
plt.ylabel("Density")
plt.legend()
baby_predictions = [model.predict(np.expand_dims(new_baby, [0]))[0,0] for i in range(1000)]
spider_predictions = [model.predict(np.expand_dims(spider, [0,-1]))[0,0] for i in range(1000)]
batch_size = 5
history = model.fit(x=images_,
y=np.array(labels),
epochs=250,
verbose=1,
batch_size=batch_size,
validation_split=0.1,
validation_freq=5)
@DoctorLoop
DoctorLoop / bayesian_convolutional.py
Last active July 6, 2023 12:13
Definition of a Bayesian Convolutional Architecture for regression problems
tf.keras.backend.clear_session()
kl_divergence_function = lambda q, p, _: dist.kl_divergence(q, p) / tf.cast(836, dtype=tf.float32)
model = tf.keras.Sequential([
tf.keras.Input(shape=(126,126,1),name="basket"),
tfp.layers.Convolution2DFlipout(16, kernel_size=5, strides=(1,1), data_format="channels_last",
padding="same", activation=tf.nn.relu, name="conv_tfp_1a",
kernel_divergence_fn=kl_divergence_function),
plt.hist(underdone['time'], color='r', alpha=0.6, histtype='step', label='underdone', fill=True, linewidth=2)
plt.hist(softboiled['time'], color='y', alpha=0.6, histtype='step', label='soft-boiled', fill=True, linewidth=2)
plt.hist(hardboiled['time'], color='g', alpha=0.6, histtype='step', label='hard-boiled', fill=True, linewidth=2)
plt.xlabel('time (g)')
plt.ylabel("number examples")
plt.legend()
import matplotlib.pyplot as plt
plt.scatter(hardboiled['weight'],hardboiled['time'],color='g',marker="^",label="hard-boiled")
plt.scatter(softboiled['weight'],softboiled['time'],color='y',marker="o",label="soft-boiled")
plt.scatter(underdone['weight'],underdone['time'],color='r', marker="+",label="underdone")
plt.xlabel('weight (g)')
plt.ylabel("time (mins)")
plt.legend()
import pandas as pd
egg_timing_df = pd.read_csv("egg_times.csv",index_col=0)
egg_timing_df.head()
num_samples = 5000
predicted = []
for i in range(num_samples):
features = test_dataset.df.sample(n=3).drop('outcome',axis=1)
batch_predictions = predict(torch.tensor(features.values))
predicted.append(batch_predictions)
ax1 = plt.subplot(1,3,1)
ax1.hist(predicted[:,1].reshape(-1,1),bins=[0,1,2,3],density=False, hatch="/", color='r', edgecolor='black')
ax = plt.subplot(1,3,2, sharey=ax1)
ax.hist(predicted[:,49].reshape(-1,1),bins=[0,1,2,3],density=False, color='g', edgecolor='black')
ax = plt.subplot(1,3,3,sharey=ax1)
ax.hist(predicted[:,13].reshape(-1,1),bins=[0,1,2,3],density=False, hatch=".", edgecolor='black')
optim = pyro.optim.Adam({"lr": 0.001})
svi = SVI(model, guide, optim, loss=Trace_ELBO())
num_iterations = 50
loss = 0
for j in range(num_iterations):
loss = 0
for batch_id, data in enumerate(trainloader):
# calculate the loss and take a gradient step