-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_1.py
122 lines (90 loc) · 3.6 KB
/
example_1.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import numpy as np
from numpy import genfromtxt
import scipy.io as sio
import rbmsemisuplearn
from dbncreateopts import dbncreateopts
import dbncheckopts
from dbncheckopts import dbncheckopts
from dbnsetup import dbnsetup
from dbntrain import dbntrain
from rbmsemisuplearn import rbmsemisuplearn
from rbmdiscriminative import rbmdiscriminative
from rbmgenerative import rbmgenerative
from dbnpredict import dbnpredict
import h5py
biomag_labeled_1 = sio.loadmat("./data/data_labeled_6.mat")
# biomag_unlabeled_1 = []
# with h5py.File('./data/Biomag_unlabeled_3.mat', 'r') as f:
# biomag_unlabeled_1 = np.array(f["unlabeled_min_max_norm"])
# print(biomag_unlabeled_1.shape)
#biomag_unlabeled_1 = f["unlabeled_min_max_norm"]
#biomag_unlabeled_2 = sio.loadmat("./data/Biomag_unlabeled_3.mat")
#biomag_unlabeled_2 = genfromtxt(r"I:\OneDrive - Szegedi Tudományegyetem\egyetem\tdk\program\unlabeled_mmn.mat", delimiter=',')
sizes = [500]
opts, valid_fields = dbncreateopts()
# print("opts: ", opts)
opts.numepochs = 50
opts.patience = 15
opts.batchsize = 10
# Select training mode:
train_func_selector_var = 3
if train_func_selector_var == 0:
opts.train_function = rbmgenerative # todo : 'train_func' correction in opts
elif train_func_selector_var == 1:
opts.train_function = rbmdiscriminative
else:
opts.train_function = rbmsemisuplearn
opts.semisup_type = rbmdiscriminative
# Choosing unlabeled data amount:
if opts.train_function == rbmsemisuplearn:
data_size_select_var = 0 # only if semi-supervised train mode
if data_size_select_var == 0:
# 5000 rows of unlabeled set
biomag_unlabeled_1 = sio.loadmat("./data/unlabeled_data_5000_set_001.mat")
opts.x_semisup = biomag_unlabeled_1["unlabeled_data_5000_set_001"]
elif data_size_select_var == 1:
# big data file
f2 = h5py.File(r"D:\python_project\rbm-tf\data\data_unlabeled_2.mat", 'r')
print(list(f2.keys()))
f2_list = list(f2.keys())
x_train_un = f2.get('unlabeled')
x_train_un_re = x_train_un[:, :]
# x_train_un_re_all = x_train_un[:,:]
# x_train_un_re = x_train_un_re.T
opts.x_semisup = x_train_un_re
opts.learningrate = 0.05
opts.momentum = 0.001
opts.semisup_beta = 0.1
opts.traintype = "CD"
opts.init_type = "crbm"
# print(sio.whosmat(r"D:\python_project\wip\data_labeled_for_py_1_2.mat"))
# biomag_labeled_1 = sio.loadmat(r"C:\Users\Pap Gergő\PycharmProjects\rbm-tf\data_labeled_for_py_1_2.mat")
print(sio.whosmat("./data/data_labeled_6.mat"))
opts.y_train = biomag_labeled_1["y_train"]
opts.x_train = biomag_labeled_1["x_train"]
opts.x_val = biomag_labeled_1["x_val"]
opts.y_val = biomag_labeled_1["y_val"]
#print(biomag_unlabeled_1.shape) #.o["unlabeled_data_5000_set_001"]
x_train = biomag_labeled_1["x_train"]
x_test = biomag_labeled_1["test_x"]
y_test = biomag_labeled_1["test_y"]
# ----------dbncheckopts
# print(opts.__dict__.keys())
print("\n")
# print("fieldnames", fieldnames)
print("\n")
# ---------!dbncheckopts
dbncheckopts(opts,valid_fields)
# print("Sizes sizes: ", sizes)
#dbnsetup(sizes,x_train,opts)
#rbmlist = []
rbmlist, dbn, dbn_sizes = dbnsetup(sizes, x_train, opts)
#print("rbmlist[u] at example_1: ", rbmlist[0])
dbn = dbntrain(rbmlist[:], dbn, x_train, opts)
# Dbn = dbnsetup(sizes, x_train, opts)
pred_y = dbnpredict(dbn, x_test)
pred_y = pred_y + 1 # data labels encoded differently from python 0 starting index
result = pred_y == np.reshape(y_test,(y_test.shape[0]))
accuracy_final = np.sum(result) / x_test.shape[0] * 100
print("Accuracy on test: ",accuracy_final,"%")
np.savetxt('RBM_accuracy.txt', pred_y, fmt='%d')