Skip to content

Instantly share code, notes, and snippets.

@bstriner
Last active October 13, 2021 01:23
Show Gist options
  • Save bstriner/e1e011652b297d13b3ac3f99fd11b2bc to your computer and use it in GitHub Desktop.
Save bstriner/e1e011652b297d13b3ac3f99fd11b2bc to your computer and use it in GitHub Desktop.
How to use Keras backend and optimizers directly outside of a Keras model
from keras.optimizers import Adam
from keras import backend as K
from keras.datasets import mnist
from keras.utils.np_utils import to_categorical
from keras.metrics import categorical_accuracy
from keras.initializations import glorot_uniform, zero
import numpy as np
# inputs and targets are placeholders
input_dim = 28*28
output_dim = 10
x = K.placeholder(name="x", shape=(None, input_dim))
ytrue = K.placeholder(name="y", shape=(None, output_dim))
# model parameters are variables
hidden_dim = 128
W1 = glorot_uniform((input_dim,hidden_dim))
b1 = zero((hidden_dim,))
W2 = glorot_uniform((hidden_dim,output_dim))
b2 = zero((output_dim,))
params = [W1, b1, W2, b2]
# two-layer model
hidden = K.sigmoid(K.dot(x, W1)+b1)
ypred = K.softmax(K.dot(hidden,W2)+b2)
# categorical cross entropy loss
loss = K.mean(K.categorical_crossentropy(ytrue, ypred),axis=None)
# categorical accuracy
accuracy = categorical_accuracy(ytrue, ypred)
# Train function
opt = Adam()
updates = opt.get_updates(params, [], loss)
train = K.function([x, ytrue],[loss, accuracy],updates=updates)
# Test function
test = K.function([x, ytrue], [loss, accuracy])
# Train the network
((xtrain, ytrain),(xtest, ytest)) = mnist.load_data()
(xtrain, xtest) = [x.reshape((-1, input_dim))/255.0 for x in (xtrain, xtest)]
(ytrain, ytest) = [to_categorical(y, output_dim) for y in (ytrain, ytest)]
for epoch in range(1000):
loss, accuracy = train([xtrain, ytrain])
test_loss, test_accuracy = test([xtest, ytest])
print("Epoch: {}, Train Loss: {}, Train Accuracy: {}, Test Loss: {}, Test Accuracy: {}".format(
epoch, loss, accuracy, test_loss, test_accuracy))
@BenAsaf
Copy link

BenAsaf commented Jan 3, 2018

Thank you @bstriner! Just managed to modify the code to run it on the current Keras version:

from keras.optimizers import Adam
from keras import backend as K
from keras.datasets import mnist
from keras.utils.np_utils import to_categorical
from keras.metrics import categorical_accuracy
from keras.initializers import glorot_uniform, zero


input_dim = 28*28
output_dim = 10
x = K.placeholder(name="x", shape=(None, input_dim))
ytrue = K.placeholder(name="y", shape=(None, output_dim))

hidden_dim = 128
W1 = K.variable(glorot_uniform()([input_dim, hidden_dim]))
b1 = K.variable(zero()((hidden_dim,)))
W2 = K.variable(glorot_uniform()([hidden_dim, output_dim]))
b2 = K.variable(zero()((output_dim,)))
params = [W1, b1, W2, b2]


hidden = K.sigmoid(K.dot(x, W1)+b1)
ypred = K.softmax(K.dot(hidden, W2)+b2)


loss = K.mean(K.categorical_crossentropy(ytrue, ypred),axis=None)

accuracy = categorical_accuracy(ytrue, ypred)

opt = Adam()
updates = opt.get_updates(params, [], loss, )
train = K.function([x, ytrue],[loss, accuracy],updates=updates)

test = K.function([x, ytrue], [loss, accuracy])

((xtrain, ytrain),(xtest, ytest)) = mnist.load_data()
(xtrain, xtest) = [x.reshape((-1, input_dim))/255.0 for x in (xtrain, xtest)]
(ytrain, ytest) = [to_categorical(y, output_dim) for y in (ytrain, ytest)]
for epoch in range(1000):
	loss, accuracy = train([xtrain, ytrain])
	test_loss, test_accuracy = test([xtest, ytest])
	print("Epoch: {}, Train Loss: {}, Train Accuracy: {}, Test Loss: {}, Test Accuracy: {}".format(
		epoch, loss, accuracy, test_loss, test_accuracy))

@shoutashi
Copy link

Thank you @bstriner! That is very helpful. However, if I want to save test model from K.function to reuse it later, what should the addition code?

@inspirepassion
Copy link

I know this post is from 2016, now it's 2021. I saw ppl using Keras few different ways, one is like yours, which is:

opt = Adam()
updates = opt.get_updates(params, [], loss, )
train = K.function([x, ytrue],[loss, accuracy],updates=updates)
loss, accuracy = train([xtrain, ytrain])

Note that most nowadays Keras model is used by compiling using the API, such as:

model.compile(optimizer, loss, metrics)
model.fit(x, y, epochs, batch_size)

My question is, back then in 2016, why did ppl not use the second method? Isn't the second one more succinct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment