Skip to content

Instantly share code, notes, and snippets.

@breadplop
Created November 4, 2018 09:09
Show Gist options
  • Save breadplop/0014877e42f3bc7774ba51ab32db21bb to your computer and use it in GitHub Desktop.
Save breadplop/0014877e42f3bc7774ba51ab32db21bb to your computer and use it in GitHub Desktop.
BT4221 Assignment 4 Question 2
# https://paper.dropbox.com/doc/Assignment-4--AQAaS_sEpPtBSwlJT9IK8u3qAg-AbLDwYkKF1jWLBcH7aM0J
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Model
from keras.layers import Input, Dense, Flatten
from keras.layers import Dense, Activation
from keras.layers import SimpleRNN
from keras import initializers
from keras.optimizers import RMSprop
batch_size = 32
num_classes = 10
epochs = 10
#hidden_units = 100
#learning_rate = 1e-6
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], -1, 1)
x_test = x_test.reshape(x_test.shape[0], -1, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
print('Evaluate RNN...')
#'''
# Create Model
input_layer = Input(shape=(784,1)) #input layer
#layer = Dense(10, activation='tanh')(input_layer) #hidden layer
#output_layer = Dense(1, activation = 'sigmoid')(layer) #output layer
#output_layer = Dense(784,)(input_layer)
layers= Dense(10, activation='tanh')(input_layer)
layers = Flatten()(layers)
output_layer=Dense(10, activation='sigmoid')(layers)
model = Model(inputs=input_layer, outputs= output_layer) #i get an error
# Compile Model
model.compile(loss='categorical_crossentropy',
optimizer='Adagrad',
metrics=['accuracy'])
# Fit Model
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
# Evaluate Model
scores = model.evaluate(x_test, y_test, verbose=0)
print('RNN test score:', scores[0])
print('RNN test accuracy:', scores[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment