Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list of floats to NumPy array

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)
like image 588
Abhinav Goel Avatar asked Jan 26 '26 10:01

Abhinav Goel


1 Answers

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)
like image 190
cs95 Avatar answered Jan 28 '26 22:01

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!