I want to build and train a neural network using the keras framework. I configured keras that it will use Tensorflow as a backend. After I trained the keram model, I tried to use only Tensorflow. I can access the session and get the tensorflow graph. But I do not know how to use the tensor flow graph, for example, to make a prediction.
I am creating a network with the following tutorial
http://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
in the train () method, I create and equip the model using only keras and save the keras and tensorflow model
in the eval () method
Here is my code:
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
import keras.backend.tensorflow_backend as K
import tensorflow as tf
import numpy
sess = tf.Session()
K.set_session(sess)
seed = 7
numpy.random.seed(seed)
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
X = dataset[:, 0:8]
Y = dataset[:, 8]
def train():
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics['accuracy'])
model.fit(X, Y, nb_epoch=10, batch_size=10)
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1] * 100))
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
model.save_weights("model.h5")
saver = tf.train.Saver()
save_path = saver.save(sess, "model")
def eval():
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model.h5")
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
score = loaded_model.evaluate(X, Y, verbose=0)
loaded_model.predict(X)
print ("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))
sess = tf.Session()
saver = tf.train.import_meta_graph('model.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
(sess.graph), keras, , tensorflow. , generell, keras build .