def dimension_reduction(*x, algo='pca', **kwargs): algo = str(algo).lower() assert algo in ('pca', 'tsne', 'umap'), \ "No support for algorithm: '%s'" % algo if x[0].shape[1] == 1: raise ValueError("No dimension reduction for input with shape: %s" % str(x[0].shape)) elif x[0].shape[1] == 2: pass elif algo == 'tsne': x = fast_tsne(*x, n_components=2, perplexity=30.0, learning_rate=200, n_iter=1000, random_state=1234, n_jobs=8, **kwargs) elif algo == 'pca': x = fast_pca(*x, n_components=2, random_state=1234, **kwargs) else: x = fast_umap(*x, random_state=1234, **kwargs) if len(x) == 1: return x[0] return x
from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from odin import ml from odin import visual as vs os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' tf.random.set_seed(8) np.random.seed(8) X, y = load_digits(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) X_umap = ml.fast_umap(X_train, X_test) X_tsne = ml.fast_tsne(X_train, X_test) X_pca = ml.fast_pca(X_train, X_test, n_components=2) styles = dict(size=12, alpha=0.6, centroids=True) vs.plot_figure(6, 12) vs.plot_scatter(x=X_pca[0], color=y_train, ax=(1, 2, 1), **styles) vs.plot_scatter(x=X_pca[1], color=y_test, ax=(1, 2, 2), **styles) vs.plot_figure(6, 12) vs.plot_scatter(x=X_tsne[0], color=y_train, ax=(1, 2, 1), **styles) vs.plot_scatter(x=X_tsne[1], color=y_test, ax=(1, 2, 2), **styles) vs.plot_figure(6, 12) vs.plot_scatter(x=X_umap[0], color=y_train, ax=(1, 2, 1), **styles)
def callback(): trainer = get_current_trainer() x, y = x_test[:1000], y_test[:1000] px, qz = vae(x, training=False) # latents qz_mean = tf.reduce_mean(qz.mean(), axis=0) qz_std = tf.reduce_mean(qz.stddev(), axis=0) w = tf.reduce_sum(decoder.trainable_variables[0], axis=(0, 1, 2)) # plot the latents and its weights fig = plt.figure(figsize=(6, 4), dpi=200) ax = plt.gca() l1 = ax.plot(qz_mean, label='mean', linewidth=1.0, linestyle='--', marker='o', markersize=4, color='r', alpha=0.5) l2 = ax.plot(qz_std, label='std', linewidth=1.0, linestyle='--', marker='o', markersize=4, color='g', alpha=0.5) ax1 = ax.twinx() l3 = ax1.plot(w, label='weight', linewidth=1.0, linestyle='--', marker='o', markersize=4, color='b', alpha=0.5) lines = l1 + l2 + l3 labs = [l.get_label() for l in lines] ax.grid(True) ax.legend(lines, labs) img_qz = vs.plot_to_image(fig) # reconstruction fig = plt.figure(figsize=(5, 5), dpi=120) vs.plot_images(np.squeeze(px.mean().numpy()[:25], axis=-1), grids=(5, 5)) img_res = vs.plot_to_image(fig) # latents fig = plt.figure(figsize=(5, 5), dpi=200) z = fast_umap(qz.mean().numpy()) vs.plot_scatter(z, color=y, size=12.0, alpha=0.4) img_umap = vs.plot_to_image(fig) # gradients grads = [(k, v) for k, v in trainer.last_train_metrics.items() if '_grad/' in k] encoder_grad = sum(v for k, v in grads if 'Encoder' in k) decoder_grad = sum(v for k, v in grads if 'Decoder' in k) return dict(reconstruct=img_res, umap=img_umap, latents=img_qz, qz_mean=qz_mean, qz_std=qz_std, w_decoder=w, llk_test=tf.reduce_mean(px.log_prob(x)), encoder_grad=encoder_grad, decoder_grad=decoder_grad)
X_test = [] y_test = [] for x, y in sc.create_dataset(batch_size=batch_size, partition='valid', inc_labels=True).concatenate( sc.create_dataset(batch_size=batch_size, partition='test', inc_labels=True)): X_test.append(x) y_test.append(y) X_test = tf.concat(X_test, axis=0) y_test = tf.concat(y_test, axis=0) try: from odin.ml import fast_umap x_ = fast_umap(X_test.numpy()) algo = "umap" except: from odin.ml import fast_tsne x_ = fast_tsne(X_test.numpy()) algo = "tsne" # =========================================================================== # MOdel # =========================================================================== def fig2image(fig: plt.Figure, dpi=180) -> tf.Tensor: r""" Return an image shape `[1, h, w, 4]` """ buf = io.BytesIO() plt.savefig(buf, format='png', dpi=dpi) buf.seek(0)