Created
November 28, 2016 04:47
-
-
Save tomthetrainer/ecfd8486c97d5c25c3b1a199c7de1175 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import pandas | |
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.wrappers.scikit_learn import KerasClassifier | |
from keras.utils import np_utils | |
from sklearn.cross_validation import cross_val_score, KFold | |
from sklearn.preprocessing import LabelEncoder | |
from sklearn.pipeline import Pipeline | |
# fix random seed for reproducibility | |
seed = 7 | |
numpy.random.seed(seed) | |
# load dataset | |
dataframe = pandas.read_csv("iris.csv", header=None) | |
dataset = dataframe.values | |
X = dataset[:,0:4].astype(float) | |
Y = dataset[:,4] | |
print(X) | |
print(Y) | |
#encode class values as integers | |
encoder = LabelEncoder() | |
encoder.fit(Y) | |
encoded_Y = encoder.transform(Y) | |
# convert integers to dummy variables (hot encoded) | |
dummy_y = np_utils.to_categorical(encoded_Y) | |
print(dummy_y) | |
# define baseline model | |
#def baseline_model(): | |
# create model | |
model = Sequential() | |
model.add(Dense(4, input_dim=4, activation='relu')) | |
model.add(Dense(3,activation='sigmoid')) | |
##model.add(Dense(3,activation='sigmoid',output_dim=3)) | |
##model.add(Dense(4, input_dim=4, init='normal', activation='relu')) | |
##model.add(Dense(3, init='normal', activation='sigmoid')) | |
# Compile model | |
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) | |
# return model | |
#model.fit | |
model.fit(X, dummy_y, nb_epoch=200, batch_size=5) | |
#estimator = KerasClassifier(build_fn=baseline_model, nb_epoch=200, batch_size=5, verbose=0) | |
#kfold = KFold(n=len(X), n_folds=10, shuffle=True, random_state=seed) | |
#results = cross_val_score(estimator, X, dummy_y, cv=kfold) | |
#print("Accuracy: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100)) | |
model.save_weights('iris_model_save') | |
#model.save_model('sm2') ## does not work | |
json_string = model.to_json() | |
text_file = open("iris_model_json", "w") | |
text_file.write(json_string) | |
text_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment