0
votes

I need to implement a simple perceptron algorithm. There are 2 classes "cat and dogs". Each training sample is a pair of the form (x,t) where x is the vector of input values, and t is target output value. But i don't know how can i convert an image to a vector form in python for perceptron? What is meant by vector form?

Images have a size 32*32.

2
Images are 32x32 thus you have 1024 values: this is your vector! Just concat all the columns for example...xiawi

2 Answers

1
votes

If you have an image a of size 32x32:

>>> a=np.random.random((32,32))
>>> a.shape
(32, 32)

Then you can transform it to a vector to feed the perceptron by:

>>> b=a.reshape(1,1024)
>>> b.shape
(1, 1024)
1
votes

Perceptron only deal with vector not matrix. So you should flatten your image to be a vector. So if you have a batch of images of shape like (1000,32,32) you can do this:

x = x.reshape((len(x), x.shape[1]*x.shape[2]))

In the future, if you still want to deal with images, have a look at Convolutional Neural Network (CNN).