Skip to content

Instantly share code, notes, and snippets.

def generator():
""" Declare generator """
model = Sequential()
model.add(Dense(256, input_shape=(10,)))
model.add(LeakyReLU(alpha=0.2)) # 使用 LeakyReLU 激活函數
model.add(BatchNormalization(momentum=0.8)) # 使用 BatchNormalization 優化
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
import numpy as np
import os
from keras.datasets import mnist
from keras.layers import Dense, Reshape, Flatten
from keras.layers import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential
from keras.optimizers import Adam
# Eval.
q = model.predict(x, verbose=0)
p = target_distribution(q) # update the auxiliary target distribution p
# evaluate the clustering performance
y_pred = q.argmax(1)
if y is not None:
acc = np.round(accu(y, y_pred), 5)
nmi = np.round(nmis(y, y_pred), 5)
ari = np.round(aris(y, y_pred), 5)
model.compile(optimizer=SGD(0.01, 0.9), loss='kld')
#%%
kmeans = KMeans(n_clusters=n_clusters, n_init=20)
y_pred = kmeans.fit_predict(encoder.predict(x))
#%%
y_pred_last = np.copy(y_pred)
model.get_layer(name='clustering').set_weights([kmeans.cluster_centers_])
#%%
# computing an auxiliary target distribution
def target_distribution(q):
class ClusteringLayer(Layer):
"""
Clustering layer converts input sample (feature) to soft label, i.e. a vector that represents the probability of the
sample belonging to each cluster. The probability is calculated with student's t-distribution.
# Example
```
model.add(ClusteringLayer(n_clusters=10))
```
# Arguments
n_clusters: number of clusters.
import os
import keras.backend as K
from keras.engine.topology import Layer, InputSpec
from keras.layers import Dense, Input
from keras.models import Model
from keras.optimizers import SGD
from keras import callbacks
from keras.initializers import VarianceScaling
def autoencoder(dims, act='relu', init='glorot_uniform'):
@MortisHuang
MortisHuang / DeepClustering_1.py
Last active June 11, 2019 09:01
Let's train a K-Means model to cluster the MNIST handwritten digits to 10 clusters.
from sklearn.cluster import KMeans
from keras.datasets import mnist
import numpy as np
def accu(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 22 10:47:52 2019
@author: Mortis
"""
#%%
import os
import numpy as np
import keras.backend as K
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 21 12:53:15 2019
@author: Mortis
"""
import umap
import pandas as pd
import numpy as np
from sklearn.datasets import load_digits
from bokeh.plotting import figure, output_file, show
# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
# output to static HTML file
output_file("lines.html", title="line plot example")
# create a new plot with a title and axis labels