3

I am working with Neural Nets, I want to implement it on an FPGA. I have a code working on MNIST, and I want to obtain the initial weights using float32, and then retrain the weights on the FPGA with fixed point.

I am running my simulation in python. I am looking for a way to do this conversion

from keras.datasets import mnist
from keras.layers import Dense
from keras.models import Sequential
from keras.layers import Dropout
from keras.utils import np_utils
import matplotlib.pyplot as plt
(x, y), (X, Y) = mnist.load_data()

num = x.shape[1] * x.shape[2]
x = x.reshape(x.shape[0],x.shape[1]*x.shape[2]).astype('float32')
X = X.reshape(X.shape[0],X.shape[1]*X.shape[2]).astype('float32')

x = x/255
X = X/255

y = np_utils.to_categorical(y)
Y = np_utils.to_categorical(Y)

classes = y.shape[1]

def calc():
    model = Sequential()
    model.add(Dense(num, input_dim = num, init = 'normal', activation = 'relu'))
    model.add(Dense(classes, init = 'normal', activation = 'softmax'))
    model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model

model = calc()
model.fit(x, y, validation_data=(X, Y), nb_epoch=10, batch_size=200,
    verbose=2)
scores = model.evaluate(X, Y, verbose=0)
print("Accuracy: ", scores)
6
  • I want to convert the weights of each layer into fixed point representation so it can be retrained Commented Jun 6, 2017 at 20:16
  • What variable is this list? Is it really a list? or numpy array? or some specialized kereas type? Commented Jun 6, 2017 at 20:44
  • What do you mean by "fixed point"? Commented Jun 7, 2017 at 5:07
  • @hpaulj, the output is a numpy array Commented Jun 7, 2017 at 14:00
  • @VivekKumar , I mean that I want to have say exactly 16 bits for my fractional part and 4 bits for my integer part. I don't want to work with a floating decimal point. Commented Jun 7, 2017 at 14:00

1 Answer 1

7

Pass your list to np.array with dtype=np.float32 to specify 32 bit floating point numbers as the data type:

np.array(your_list, dtype=np.float32)
Sign up to request clarification or add additional context in comments.

2 Comments

I want to convert the weights of each layer into fixed point representation so it can be retrained. I think float32 will not meet my requirements. I need a fixed point representation.
@AbhinavGoel okay, then try the above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.