Numpy Matrixen
Verfasst: Freitag 12. April 2019, 15:37
Hallo,
mein Ziel ist es in dieser Zeile
main:
matrix:
nn:
mein Ziel ist es in dieser Zeile
eine neue Matrix zu erzeugen in dem ich 2 andere Multipliziere. Allerdings bekomme ich dabei diesen Fehler hier:hidden = matrix.Matrix.statscale(self.weights_ih, inputs)
Weiß hier jemand was ich falsch mache?AttributeError: 'Matrix' object has no attribute 'shape'
main:
Code: Alles auswählen
import nn
import matrix
def main():
neuralnetwork = nn.Nn(2, 2, 1)
inputs = [2, 2]
output = neuralnetwork.feedforward(inputs);
print(output)
main()
Code: Alles auswählen
import numpy as np
class Matrix:
def __init__(self, values):
self.matrix = np.zeros(shape=(values))
self.values = values
def __repr__(self):
return f'{self.__class__.__name__}({self.matrix!r})'
def get_values(values):
m = np.array(values)
m.transpose()
return m
@property
def rows(self):
return self.values.shape[0]
@property
def cols(self):
return self.values.shape[1]
def randomize(self):
self.matrix = np.random.uniform(-1, 1, self.matrix.shape)
return self.matrix
def add(self, other):
self.matrix += other
return self.matrix
def scale(self, factor):
if isinstance(factor, (Matrix, np.ndarray)):
if (self.matrix.shape[1] == factor.shape[0]):
self.matrix = self.matrix.dot(factor)
elif (self.matrix.shape[1] == factor.shape[1] and self.matrix.shape[0] == factor.shape[0]):
self.matrix *= factor
else:
print('Warning: Columms of A must match rows of B')
else:
self.matrix *= factor
return self.matrix
def statscale(m, factor):
if isinstance(factor, np.ndarray):
if (m.shape[1] == factor.shape[0]):
m = self.matrix.dot(factor)
elif (m.shape[1] == factor.shape[1] and m.shape[0] == factor.shape[0]):
m *= factor
else:
print('Warning: Columms of A must match rows of B')
else:
m *= factor
return m
def transpose(self) :
self.matrix = np.transpose(self.matrix)
return self.matrix
@classmethod
def from_shape(cls, rows, cols):
return cls(np.zeros((rows, cols), int))
Code: Alles auswählen
import matrix
import math
import numpy as np
def sigmoid(x):
return 1 / (1 + math.exp(-x))
class Nn:
def __init__(self, input_nodes, hidden_nodes, output_nodes):
self.input_nodes = input_nodes
self.hidden_nodes = hidden_nodes
self.output_nodes = output_nodes
self.weights_ih = matrix.Matrix([self.hidden_nodes, self.input_nodes])
self.weights_ho = matrix.Matrix([self.output_nodes, self.hidden_nodes])
self.weights_ih.randomize()
self.weights_ho.randomize()
bias_h_val = []
for x in range(self.hidden_nodes):
bias_h_val.append(np.random.uniform(-1, 1))
bias_o_val = []
for x in range(self.output_nodes):
bias_o_val.append(np.random.uniform(-1, 1))
self.bias_h = matrix.Matrix.get_values(bias_h_val)
self.bias_o = matrix.Matrix.get_values(bias_o_val)
def feedforward(self, input_array):
# Generate Hidden Outputs
inputs = matrix.Matrix.get_values(input_array)
hidden = matrix.Matrix.statscale(self.weights_ih, inputs)
hidden.add(self.bias_h)
#activation function
hidden.map(sigmoid)
output = matrix.Matrix.statscale(self.weights_ho, hidden)
output.add(self.bias_o)
output.map(sigmoid)
output.toArray()
return output