-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathising_main.py
58 lines (45 loc) · 1.97 KB
/
ising_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import Binarizer
from sklearn.model_selection import train_test_split
#import matplotlib.pyplot as plt
from my_RBM_tf2 import RBM
import deepdish as dd
import itertools
import os
import os
from optimizer import Optimizer
import yaml
here = os.path.dirname(os.path.abspath(__file__))
datah5 = dd.io.load(here+'/data/ising/ising_data_L32.hdf5')
#Transform -1 in 0 and take spin up as standard configuration
binarizer = Binarizer(threshold=0)
keys = list(datah5.keys())
#put here the temperature from keys that you want to use for the training
class_names = [keys[i] for i in [4,6,7,8,9,10,11,12,16]]
n_samples = datah5[keys[0]].shape[0]
datah5_norm={}
data_bin={}
for key in keys:
datah5_norm[key] = np.array([np.where(np.sum(slice)<0,-slice,slice) for slice in datah5[key]])
data_bin[key] = np.array([binarizer.fit_transform(slice) for slice in datah5_norm[key]])
#class labels even if they are not really useful here
class_labels = np.asarray(list(itertools.chain.from_iterable(itertools.repeat(x, n_samples) for x in range(0,len(class_names)))))
data = data_bin[class_names[0]]
for temperature in class_names[1:]:
data = np.concatenate([data,data_bin[temperature]])
#create dictionary for training
x_train, x_test, y_train, y_test = train_test_split(data, class_labels, test_size=0.1, random_state=42)
#reshape pictures to be vectors and fix datatype
x_train = x_train.reshape(x_train.shape[0],-1).astype(np.float32)
x_test = x_test.reshape(x_test.shape[0],-1).astype(np.float32)
data = {"x_train": x_train ,"y_train": y_train,"x_test": x_test,"y_test": y_test}
#Create a restricted boltzmann machines
machine = RBM(x_train[0].shape[0], 800, 100, (32, 32), 128,'cd')
optimus = Optimizer(machine, 0.9, opt = 'adam')
machine.save_param(optimus,data = class_names)
#Train the machine
machine.train(data,optimus)