2

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)
cs95
  • 379,657
  • 97
  • 704
  • 746
Abhinav Goel
  • 401
  • 1
  • 6
  • 13

1 Answers1

6

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)
cs95
  • 379,657
  • 97
  • 704
  • 746
  • 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. – Abhinav Goel Jun 06 '17 at 20:17
  • @AbhinavGoel okay, then try the above. – cs95 Jun 06 '17 at 20:18