-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels1.py
81 lines (66 loc) · 1.93 KB
/
models1.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
# _*_ coding" utf-8 _*_
from keras.layers import Input, Dense
from keras.models import Model
import numpy as np
from mod_gendata import *
# usr-settings
epochs=50
#epochs=10
batch_size=256
inputdir='/home/kurihana/ml_model/work_mymodel/ex4/data/train_data'
testdir='/home/kurihana/ml_model/work_mymodel/ex4/data/test_data'
# plot-settings
lon=360
lat=181
n = 5 # number of pics on screen
#get data
gd = gen_grads_data()
x_train = gd.load_data(inputdir)
x_test = gd.load_data(testdir)
xdim = x_train.shape[1]
print(x_train.shape[1])
#stop
# adjust minst data
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# compile - model
encoding_dim = 32 # num. of dim in a mid-layer
input_img = Input(shape=(xdim,))
encoded = Dense(encoding_dim, activation='relu')(input_img)
decoded = Dense(xdim, activation='sigmoid')(encoded)
autoencoder = Model(input=input_img, output=decoded)
autoencoder.compile(optimizer='adadelta',
loss='categoricical_corssentropy',
)
# learning
autoencoder.fit(x_train, x_train,
epochs = epochs,
batch_size = batch_size,
shuffle= 'True',
verbose = 1,
validation_data=(x_test, x_test)
)
# check score
#score = autoencoder.evaluate(x_test, x_test, verbose=1)
#print()
#print('Test loss:', score[0])
#print('Test accuracy:', score[1])
autoencoder.save('./sflp_autoencoder50.h5')
#### plot
import matplotlib.pyplot as plt
# comvert test-images
decoded_imgs = autoencoder.predict(x_test)
# show
plt.figure(figsize=(16,8))
for i in range(n):
ax = plt.subplot(2,n,i+1)
plt.imshow(x_test[i].reshape(lat,lon))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(2,n,i+1+n)
plt.imshow(decoded_imgs[i].reshape(lat,lon))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()