sample of neural network - python

In the neural network code that I've written, I could not get an answer since the problem of alignment.
I wrote a neural network code (based on some other). I tried to build input and output in the right way. While I defined the class and operations correctly, I could not get an answer since the problem of alignment. Error : shapes (127,3) and (1,4) not aligned: 3 (dim 1) != 1 (dim 0)
Datafile = pd.read_excel(r"C:\\Users\Hasan\Desktop\ANN\x.xlsx") is 127x3
Target = pd.read_excel(r"C:\\Users\Hasan\Desktop\ANN\y.xlsx") is 127x1
class Neural_Network(object):
def __init__(self):
self.inputlayer = 1
self.w1 = np.random.randn(self.inputlayer, self.hiddenlayer)
self.z = np.dot(Datafile, self.w1)
I think it's because of the dimension of two matrices but even, when I changed the dimensions it did not work.
All help will be appreciated

For matrix multiplication (dot product), number of columns of first matrix should be equal to number of rows of second matrix.
In your case, Datafile has 3 columns while w1 has 1 row, that's why it's giving you error because of incorrect dimensions.
For giving you example, I am assuming random matrices,
Datafile = np.random.rand(127, 3)
w1 = np.random.rand(3, 127)
z = np.dot(Datafile, w1)
print(z.shape)
Output: (127, 127)
In this example, Datafile has 3 columns and w1 has 3 rows so in this case, dot-product will be successful.

Related

python np.dot vectorization

I'm trying to get rid of some for loops when multiplying matrices. aa represents a 3x3 transformation matrix, bb represents point coords nx3, where n is the number of points.
With:
aa = np.matrix([[1,2,3],[1,2,3],[1,2,3]])
bb = np.matrix([[1,1,1],[2,2,2],[3,3,3]])
np.tile(aa,(3,1))*bb.ravel().T
I get:
ValueError: shapes (9,3) and (9,1) not aligned: 3(dim 1) != 9 (dim 0)
Expected output in this case would be long column matrix [[6],[6],[6],[12],[12],[12],[18],[18],[18]]. Which final shape should be in this case 3x3: [[6,6,6],[12,12,12],[18,18,18]].
Edit: going away from generalized description.
There is n number of points, and only one 3x3 matrix.Its a vectorization problem of something like:
points = bb
T = aa
pointsNewPos = []
for point in points:
pointNewPosition = T*point.T
pointsNewPos.append(pointNewPosition.T)
This at least gives the output that you want in this case. Hard to make sure that it's general without more information.
import numpy as np
aa = np.array([[1,2,3],[1,2,3],[1,2,3]])
bb = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4]])
result = b.dot(aa.T)
This sounds like a job for np.einsum which lets you do matrix multiplications using arbitrary dimensions of the input matrices.
Assuming you want to use each row of bb as a column vector and left multiply it (in row major fashion) by aa then give this a whirl:
cc = np.einsum('ij,kj -> ki', bb, aa)

Advanced integer indexing with l x m x n broadcast shape

Earlier today I asked this about integer array indexing and I am having trouble taking the answer and applying it to the problem that inspired the question.
In a nutshell, p_stack1 and c_stack1 contain arrays that are derived from an image processing algorithm I am working on. p_stack1 contains probability data and c_stack1 contain integer classifications. I need to find the classification that has the highest probability for every pixel in the image with dimensions 768 x 1024. From the docs for integer array indexing, it provides a way to subset data from higher dimensional arrays using their integer indexes.
The solution of my original question works for a simplified example of n x n x n shaped arrays, but does not seem to work on l x m x n shaped arrays.
#dummy data
p_stack1 = np.reshape(np.random.uniform(0,1,2359296),(3,768,1024))
c_stack1 = np.reshape(np.random.randint(0,4,2359296),(3,768,1024))
#find where max value occurs on axis 0
ind_new=p_stack1.argmax(axis=0)
#Create assending indicies
nx, ny = 768,1024
xx = np.arange(ny)
aa= np.tile(xx,(ny,1))
bb = np.column_stack(tuple(aa))[:nx,:]
aa= np.tile(xx,(ny,1))[:nx,:]
#perform the integer array indexing
print(c_stack1[ind_new, aa,bb])
The last print statement returns the error:
IndexError: index 768 is out of bounds for axis 1 with size 768
I checked the shapes of aa and bb and both are (768, 1024)
What I am I missing?
Looks like you got your dimensions mixed up:
c_stack1.shape # (3, 768, 1024)
aa.max() # 1023
bb.max() # 767
So, when you run
c_stack1[ind_new, aa, bb]
you will be trying to index axis=1 with higher values than available, hence the error
Either turn around aa and bb, or else c_stack1[ind_new, bb, aa] will also do the trick

python numpy ValueError:shapes (171,) and (784,500) not aligned: 171

I have data vector (1 coloumn) and I'm using
def propup(self, vis):
pre_sigmoid_activation = numpy.dot(vis, self.W) + self.hbias
return sigmoid(pre_sigmoid_activation)
but i getting error
ValueError: shapes (171,) and (784,500) not aligned: 171 (dim 0) !=
784 (dim 0)
A dot product between a matrix and a vector is defined as so.
This implies that the width of the matrix and the height of the vector should be identical.
In your case ves.shape = [,171] which is the height of the vector but self.W.shape = [784, 500] meaning the width of the matrix self.W is 784.
In order for numpy.dot to work properly you need to make sure ves.shape = [x, 784] where x is some integer.
Without more code i could only guess that you are trying to train a Neural Net to solve the MNIST problem (the 784 dimension is pretty specific).
So make sure that you are sending the right vector to propup().
In any case here are a few great sources on matrix multiplication:
Matrix multiplication: https://www.mathsisfun.com/algebra/matrix-multiplying.html

Python arrays dimension issues

I am struggling once again with Python, NumPy and arrays to compute some calculations between matrices.
The code part that is likely not working properly is as follows:
train, test, cv = np.array_split(data, 3, axis = 0)
train_inputs = train[:,: -1]
test_inputs = test[:,: -1]
cv_inputs = cv[:,: -1]
train_outputs = train[:, -1]
test_outputs = test[:, -1]
cv_outputs = cv[:, -1]
When printing those matrices informations (np.ndim, np.shape and dtype respectively), this is what you get:
2
1
2
1
2
1
(94936, 30)
(94936,)
(94936, 30)
(94936,)
(94935, 30)
(94935,)
float64
float64
float64
float64
float64
float64
I believe it is missing 1 dimension in all *_output arrays.
The other matrix I need is created by this command:
newMatrix = neuronLayer(30, 94936)
In which neuronLayer is a class defined as:
class neuronLayer():
def __init__(self, neurons, neuron_inputs):
self.weights = 2 * np.random.random((neuron_inputs, neurons)) - 1
Here's the final output:
outputLayer1 = self.__sigmoid(np.dot(inputs, self.layer1.weights))
ValueError: shapes (94936,30) and (94936,30) not aligned: 30 (dim 1) != 94936 (dim 0)
Python is clearly telling me the matrices are not adding up but I am not understanding where is the problem.
Any tips?
PS: The full code is pasted ħere.
layer1 = neuronLayer(30, 94936) # 29 neurons with 227908 inputs
layer2 = neuronLayer(1, 30) # 1 Neuron with the previous 29 inputs
where `nueronLayer creates
self.weights = 2 * np.random.random((neuron_inputs, neurons)) - 1
the 2 weights are (94936,30) and (30,1) in size.
This line does not make any sense. I surprised it doesn't give an error
layer1error = layer2delta.dot(self.layer2.weights.np.transpose)
I suspect you want np.transpose(self.layer2.weights) or self.layer2.weights.T.
But maybe it doesn't get there. train first calls think with a (94936,30) inputs
outputLayer1 = self.__sigmoid(np.dot(inputs, self.layer1.weights))
outputLayer2 = self.__sigmoid(np.dot(outputLayer1, self.layer2.weights))
So it tries to do a np.dot with 2 (94936,30), (94936,30) arrays. They aren't compatible for a dot. You could transpose one or the other, producing either (94936,94936) array or (30,30). One looks too big. The (30,30) is compatible with the weights for the 2nd layer.
np.dot(inputs.T, self.layer1.weights)
has a chance of working right.
np.dot(outputLayer1, self.layer2.weights)
(30,30) with (30,1) => (30,1)
But then you do
train_outputs - outputLayer2
That will have problems regardless of whether train_outputs is (94936,) or (94936,1)
You need to make sure that arrays shapes flow correctly through the calculation. Don't just check them at the start. Check then internally. And make you sure you understand what shapes they should have at each step.
It would be a whole lot easier to develop and test this code with much smaller inputs and layers, something like 10 samples and 3 features. That way you can look at the values as well as the shapes.
np.dot uses matrix multiplication when its arguments are matrices. It looks like your code is trying to multiply two non-square matrices together with the same dimensions which doesn't work. Perhaps you meant to transpose one of the matrices? Numpy matrices have a T property that returns the transpose, you could try:
self.__sigmoid(np.dot(inputs.T, self.layer1.weights))

Python- list with arrays shape issues

I'm creating a code to run a perceptron algorithm and I can't create a random matrix the way I need it:
from random import choice
from numpy import array, dot, random
unit_step = lambda x: -1 if x < 0 else 1
import numpy as np
m=3 #this will be the number of rows
allys=[]
for j in range(m):
aa=np.random.rand(1,3)
tt=np.random.rand(3)
yy=dot(aa,tt)
ally = [aa, yy]
allys.append(ally)
print "allys", allys
w = random.rand(3)
errors = []
eta = 0.2
n = 10
x=[1,3]
for i in xrange(n):
print i
x, expected = choice(allys)
print "x", x
And I get the problem here:
result = dot(x,w)
error = expected - unit_step(result)
errors.append(error)
w += eta * error * x
print x, expected, w, error, result, errors
The log says
w += eta * error * x
ValueError: non-broadcastable output operand with shape (3,) doesn't
match the broadcast shape (1,3)
The idea is to get result looping randomly over the "table" allys.
How can I solve this? What is shape(3,)?
Thanks!
The error message actually tells you what is wrong. The result of your multiplication is a (1,3) array (2D array, one row, three columns), whereas you try to add it into a 3-element vector.
Both arrays have three elements in a row, so if you do this:
w = w + eta * error * x
there will be no error on that line, but the resulting vector will actually be a (1,3) array. That is unwanted, because then your dot does not work.
There are several ways to fix the problem, but possibly the easiest to read is to reshape x for the calculation to be a 3-element vector (1D array):
w += eta * error * x.reshape(3,)
Possibly a cleaner solution would be to define w as a (1,3) 2D array, as well, and then transpose w for the dot. This is really a matter of taste.
For numpy arrays, shape attribute returns array's dimensionality. In your case, w.shape is (3,). This means that w is a one-dimensional array with 3 elements. In turn, x.shape is (1,3), which means that x is a two-dimensional array with one row and 3 columns. You are getting an error, because the interpreter is confused on how to match the shapes. I am not sure what you are trying to do, so it's hard to suggest the solution. But you might want to try reshaping one of the arrays. For example, x = x.reshape((3,)) for adapting the shape of x to w.

Categories

Resources