Skip to content

Commit bfafc47

Browse files
authored
Add files via upload
1 parent 3ca1160 commit bfafc47

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

AutoEncoder.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from tensorflow.contrib.keras.python.keras.callbacks import TensorBoard
4+
from tensorflow.contrib.keras.python.keras.datasets import mnist
5+
from tensorflow.contrib.keras.python.keras.engine import Input, Model
6+
from tensorflow.contrib.keras.python.keras.layers import Dense, Conv2D, MaxPooling2D, UpSampling2D
7+
from tensorflow.contrib.keras.python.keras import backend as K
8+
9+
10+
(x_train, _), (x_test, _) = mnist.load_data()
11+
12+
if K.image_data_format() == 'channels_last':
13+
shape_ord = (28, 28, 1)
14+
else:
15+
shape_ord = (1, 28, 28)
16+
17+
(x_train, _), (x_test, _) = mnist.load_data()
18+
19+
x_train = x_train.astype('float32') / 255.
20+
x_test = x_test.astype('float32') / 255.
21+
22+
x_train = np.reshape(x_train, ((x_train.shape[0],) + shape_ord))
23+
x_test = np.reshape(x_test, ((x_test.shape[0],) + shape_ord))
24+
25+
26+
27+
28+
input_img = Input(shape=(28, 28, 1))
29+
30+
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
31+
x = MaxPooling2D((2, 2), padding='same')(x)
32+
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
33+
x = MaxPooling2D((2, 2), padding='same')(x)
34+
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
35+
encoded = MaxPooling2D((2, 2), padding='same')(x)
36+
37+
# at this point the representation is (4, 4, 8) i.e. 128-dimensional
38+
39+
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
40+
x = UpSampling2D((2, 2))(x)
41+
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
42+
x = UpSampling2D((2, 2))(x)
43+
x = Conv2D(16, (3, 3), activation='relu')(x)
44+
x = UpSampling2D((2, 2))(x)
45+
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
46+
47+
conv_autoencoder = Model(input_img, decoded)
48+
conv_autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
49+
50+
51+
batch_size=128
52+
steps_per_epoch = np.int(np.floor(x_train.shape[0] / batch_size))
53+
conv_autoencoder.fit(x_train, x_train, epochs=50, batch_size=128,
54+
shuffle=True, validation_data=(x_test, x_test),
55+
callbacks=[TensorBoard(log_dir='./tf_autoencoder_logs')])
56+
57+
58+
decoded_imgs = conv_autoencoder.predict(x_test)
59+
60+
n = 10
61+
plt.figure(figsize=(20, 4))
62+
for i in range(n):
63+
# display original
64+
ax = plt.subplot(2, n, i+1)
65+
plt.imshow(x_test[i].reshape(28, 28))
66+
plt.gray()
67+
ax.get_xaxis().set_visible(False)
68+
ax.get_yaxis().set_visible(False)
69+
70+
# display reconstruction
71+
ax = plt.subplot(2, n, i + n + 1)
72+
plt.imshow(decoded_imgs[i].reshape(28, 28))
73+
plt.gray()
74+
ax.get_xaxis().set_visible(False)
75+
ax.get_yaxis().set_visible(False)
76+
plt.show()

CNN.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
from tensorflow.contrib.keras.python.keras.datasets import mnist
3+
from tensorflow.contrib.keras.python.keras.layers import Convolution2D, MaxPooling2D, Dropout, Flatten, Dense
4+
from tensorflow.contrib.keras.python.keras.models import Sequential
5+
from tensorflow.contrib.keras.python.keras.utils import np_utils
6+
7+
(X_train, y_train), (X_test, y_test) = mnist.load_data()
8+
9+
# Preprocess
10+
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
11+
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
12+
X_train = X_train.astype('float32')
13+
X_test = X_test.astype('float32')
14+
X_train /= 255
15+
X_test /= 255
16+
17+
# Preprocess labels
18+
Y_train = np_utils.to_categorical(y_train, 10)
19+
Y_test = np_utils.to_categorical(y_test, 10)
20+
21+
# Model
22+
model = Sequential()
23+
24+
#defaultformat: (samples, rows, col
25+
model.add(Convolution2D(32, kernel_size=(3, 3),strides=(1, 1),activation='relu', input_shape=(28, 28, 1)))
26+
model.add(Convolution2D(32, kernel_size=(3, 3),strides=(1, 1), activation='relu'))
27+
model.add(MaxPooling2D(pool_size=(2, 2)))
28+
model.add(Dropout(0.25))
29+
30+
model.add(Flatten())
31+
model.add(Dense(128, activation='relu'))
32+
model.add(Dropout(0.5))
33+
model.add(Dense(10, activation='softmax'))
34+
model.summary()
35+
36+
37+
model.compile(loss='categorical_crossentropy',
38+
optimizer='adam',
39+
metrics=['accuracy']
40+
)
41+
42+
43+
model.fit(X_train, Y_train,
44+
batch_size=32, epochs=10, verbose=1)
45+
46+
47+
score = model.evaluate(X_test, Y_test, verbose=0)
48+
print('Test loss:', score[0])
49+
print('Test accuracy:', score[1])

RNN.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from tensorflow.contrib.keras.python.keras.datasets import imdb
2+
from tensorflow.contrib.keras.python.keras.layers import Embedding, SimpleRNN, Dropout, Dense, Activation, LSTM, GRU
3+
from tensorflow.contrib.keras.python.keras.models import Sequential
4+
from tensorflow.contrib.keras.python.keras.preprocessing import sequence
5+
6+
max_features = 20000
7+
maxlen = 100
8+
batch_size = 32
9+
10+
11+
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=max_features)
12+
13+
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
14+
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
15+
print('X_train shape:', X_train.shape)
16+
print('X_test shape:', X_test.shape)
17+
18+
19+
model = Sequential()
20+
model.add(Embedding(max_features, 128, input_length=maxlen))
21+
22+
#model.add(SimpleRNN(128))
23+
#model.add(GRU(128))
24+
model.add(LSTM(128))
25+
26+
model.add(Dropout(0.5))
27+
model.add(Dense(1))
28+
model.add(Activation('sigmoid'))
29+
30+
31+
model.compile(loss='binary_crossentropy', optimizer='adam')
32+
33+
34+
model.fit(X_train, y_train, batch_size=batch_size, epochs=1,
35+
validation_data=(X_test, y_test))

0 commit comments

Comments
 (0)