def formatit(X0, X1, Y0, Y1): w = list(range(int(np.ceil(X0)), int(X1) + 1)) plt.xticks(w, [str(t) for t in w]) w = list(range(int(np.ceil(Y0)), int(Y1) + 1)) plt.yticks(w, [str(t) for t in w]) for ss in [0.5, 0.2, 0.5]: np.random.seed(int(sys.argv[-1]) + 10) sigma_x = np.eye(Ds[-1]) * ss for i in range(5): fig = plt.figure(figsize=(5, 5)) model = networks.create_fns(BS, R, Ds, 0, var_x=np.ones(2) * ss**2) output, A, b, inequalities, signs = model['input2all'](np.random.randn( Ds[0])) regions = utils.search_region(model['signs2ineq'], model['signs2Ab'], signs) As = np.array([regions[s]['Ab'][0] for s in regions]) Bs = np.array([regions[s]['Ab'][1] for s in regions]) predictions = np.array([ model['input2all'](np.random.randn(Ds[0]))[0] for z in range(200) ]) noise = np.random.randn(*predictions.shape) * ss
plt.imshow(predictions[i].reshape((8, 8))) for i in range(50): plt.subplot(10, 10, 51 + i) plt.imshow(DATA[i].reshape((8, 8))) np.random.seed(int(sys.argv[-1]) + 10) Ds = [1, 12, 64] R, BS = 110, 150 sigma_x = .1 model = networks.create_fns(BS, R, Ds, 1, lr=0.001, leakiness=0.1, var_x=sigma_x**2) DATA = datasets.load_digits(n_class=1).images DATA = DATA[:BS].reshape((BS, -1)) + np.random.randn(BS, Ds[-1]) * 0.1 #DATA -= DATA.mean(1, keepdims=True) DATA /= (DATA.max() * 10) print(DATA) L = [] for iter in tqdm(range(36)): plt_state() plt.savefig('samples_{}.png'.format(iter)) plt.close() L.append(networks.EM(model, DATA, 1, 10, pretrain=iter==0, update_var=iter>5))
import sys sys.path.insert(0, "../../SymJAX") sys.path.insert(0, "../") import numpy as np import symjax as sj import symjax.tensor as T import utils import networks from tqdm import tqdm error1, error2 = [], [] for Ds in [[1, 8, 8]]: print(Ds) model = networks.create_fns(1, 1, Ds, 0, var_x=np.ones(Ds[-1])) x = np.random.randn(Ds[0])/10 output, A, b, inequalities, signs = model['input2all'](x) regions = utils.search_region(model['signs2ineq'], model['signs2Ab'], signs) print(len(regions)) K = 100 xx = [np.linspace(-10, 10, K)] * Ds[0] xx = np.meshgrid(*xx) xx = np.vstack([x.flatten() for x in xx]).T yy = np.zeros((int(K ** Ds[0]), Ds[-1])) yy2 = np.zeros((int(K ** Ds[0]), Ds[-1])) As = np.array([regions[s]['Ab'][0] for s in regions]) Bs = np.array([regions[s]['Ab'][1] for s in regions])
def plt_state(): predictions = model['sample'](100) plt.figure(figsize=(30, 30)) for i in range(50): plt.subplot(10, 10, 1 + i) plt.imshow(predictions[i].reshape((8, 8))) for i in range(50): plt.subplot(10, 10, 51 + i) plt.imshow(DATA[i].reshape((8, 8))) np.random.seed(int(sys.argv[-1]) + 10) Ds = [1, 20, 64] R, BS = 110, 150 model = networks.create_fns(BS, R, Ds, 0, var_x=np.ones(Ds[-1]), lr=0.001) DATA = datasets.load_digits(n_class=1).images DATA = DATA[:BS].reshape((BS, -1)) DATA /= (0.1 + DATA.max(1, keepdims=True)) DATA -= DATA.mean(1, keepdims=True) DATA += np.random.randn(BS, Ds[-1]) * 0.3 L = [] for iter in tqdm(range(16)): L.append(networks.EM(model, DATA, 100)) plt_state() plt.savefig('samples_{}.png'.format(iter)) plt.close() plt.figure()
if Ds[0] == 1: xx = np.linspace(-5, 5, 1500).reshape((-1, 1)) else: xx = np.meshgrid(np.linspace(-5, 5, 100), np.linspace(-3, 3, 100)) xx = np.vstack([xx[0].flatten(), xx[1].flatten()]).T ss = 0.1 for seed in [37, 146, 53, 187, 79]: np.random.seed(seed) for i in range(1): fig = plt.figure(figsize=(5, 5)) model = networks.create_fns(BS, R, Ds, seed, var_x=np.ones(Ds[-1]) * ss**2, leakiness=0.01) z = np.random.randn(Ds[0]) output, A, b, inequalities, signs = model['input2all'](z) outpute = output + np.random.randn(Ds[-1]) * ss regions = utils.search_region(model['signs2ineq'], model['signs2Ab'], signs) print(len(regions)) As = np.array([regions[s]['Ab'][0] for s in regions]) Bs = np.array([regions[s]['Ab'][1] for s in regions]) predictions = model['sample'](20000)
elif args.network == 'large': Ds = [1, 8, 8, DATA.shape[1]] R = 64 elif args.network == 'xlarge': Ds = [1, 32, 32, DATA.shape[1]] R = 128 graph = sj.Graph('test') with graph: if args.model != 'EM': lr = sj.tensor.Variable(1., name='lr', trainable=False) emt = networks.create_fns(BS, R, Ds, 1, var_x=np.ones(Ds[-1]) * args.std**2, lr=0.005, leakiness=args.leakiness) model = networks.create_vae(50, Ds, args.seed, lr=lr, leakiness=args.leakiness, scaler=1) else: model = networks.create_fns(BS, R, Ds, 1, lr=0.001,
parser.add_argument('--pretrain', type=int) parser.add_argument('--seed', type=int) args = parser.parse_args() np.random.seed(args.seed) Ds = [1, 12, 2] R, BS = 64, 500 #if sys.argv[-1] == 'VAE': # model = networks.create_vae(BS, Ds, 1, lr=0.005, leakiness=0.1) #else: model = networks.create_fns(BS, R, Ds, 1, lr=0.001, leakiness=0.1, var_x=args.std**2) X = model['sample'](BS) noise = np.random.randn(*X.shape) * np.sqrt(model['varx']()) plt.scatter(X[:, 0], X[:, 1]) plt.scatter(X[:, 0] + noise[:, 0], X[:, 1] + noise[:, 1]) plt.savefig('ori.png') plt.close() DATA = np.random.randn(BS, Ds[-1]) DATA /= np.linalg.norm(DATA, 2, 1, keepdims=True) DATA = np.random.randn(BS) * 2