Hopfield

mail@pastecode.io avatar
unknown
python
2 years ago
3.5 kB
1
Indexable
Never
import numpy

x = [
    1,1,1,1,0,0,0,0,
    0,0,0,0,1,1,1,1,
    1,1,1,1,0,0,0,0,
    0,0,0,0,1,1,1,1,
    1,1,1,1,0,0,0,0,
    0,0,0,0,1,1,1,1,
    1,1,1,1,0,0,0,0,
    0,0,0,0,1,1,1,1
]

# y = [
#     0,0,0,0,1,1,1,1,
#     1,1,1,1,0,0,0,0,
#     0,0,0,0,1,1,1,1,
#     1,1,1,1,0,0,0,0,
#     0,0,0,0,1,1,1,1,
#     1,1,1,1,0,0,0,0,
#     0,0,0,0,1,1,1,1,
#     1,1,1,1,0,0,0,0
# ]


# y = [
#     1,0,1,0,1,0,1,0,
#     1,1,0,1,0,1,0,0,
#     1,0,1,0,1,0,1,0,
#     1,1,0,1,0,1,0,0,
#     1,1,1,1,0,0,0,0,
#     1,0,0,0,1,1,1,0,
#     1,1,1,1,0,0,0,0,
#     1,0,0,0,1,1,1,0
# ]

y = [
    1,1,1,1,1,0,0,0,
    0,0,0,0,1,1,1,1,
    1,1,1,1,0,0,0,0,
    0,0,0,0,1,1,1,1,
    1,1,1,1,0,0,0,0,
    0,0,0,0,1,1,1,1,
    1,1,1,1,0,0,0,0,
    0,0,0,0,1,1,1,1
]

def vectorValidator(vector):
    vectorArray = vector.split(',')
    if (len(vectorArray) != 64):
        return False
        
    for index, element in enumerate(vectorArray):
        vectorArray[index] = int(element)
        if (vectorArray[index] != 1 and vectorArray[index] != 0):
            return False
    
    return vectorArray

def defineOwnVector():
    print('Czy chcesz podać własny wektor? (t/n): ')
    
    ownVectorDecision = ""
    while True:
        ownVectorDecision = input()
        if (ownVectorDecision != "t" or ownVectorDecision != "tak" or "n" or "nie"):
            break
        
    ownVector = ""
    if (ownVectorDecision == "t" or ownVectorDecision == "tak"):
        
        while True:
            print('Wprowadź 64 elementy wektora (tylko 0 i 1), kolejne elementy oddzielaj przecinkiem: \n')
            ownVector = input()
            
            validatedVector = vectorValidator(ownVector)
            if (validatedVector):
                return validatedVector
            if (not validatedVector):
                print('Wprowadzono błędny wektor... \n')
    
    return False

def makeMatrix(baseVector):
    matrix = {}
    for index, pixel in enumerate(baseVector):
        for index2 in range(len(baseVector)):
            if (index == index2):
                matrix[index, index2] = 0
                continue
            matrix[index, index2] = (2 * baseVector[index] - 1) * (2 * baseVector[index2] -1)
    return matrix

def prepareTestVector(matrix, y):
    testVector = []
    for index in range(64):
        element = 0
        for index2, pixelTest in enumerate(y):
            element = element + (pixelTest * matrix[index,index2])
        testVector.append(element)
    
    return testVector

def compareTestVector(testVector):
    result = []
    for index, element in enumerate(testVector):
        if (element > 0):
            result.append(1)
        if (element < 0):
            result.append(0)
        if (element == 0):
            result.append(testVector[index])
    
    return result


def printImage(image):
    row = ''
    for index, pixel in enumerate(image):
        if ((index + 1) % 8 == 0):
            row = row + str(pixel) + ', \n'
            continue
        
        row = row + str(pixel) + ','
    print(row)


def checkAndPrintResult(resultImage, baseImage):
    equal = numpy.array_equal(resultImage, baseImage)
    if (equal):
        print('Rozpoznany')
    if (not equal):
        print('Nie rozpoznany')


definedVector = defineOwnVector()
if (not definedVector):
    definedVector = y
    
testVector = prepareTestVector(makeMatrix(x), definedVector)
resultVector = compareTestVector(testVector)
printImage(resultVector)
checkAndPrintResult(resultVector, x)