Why this binary perceptron with 1 input and 1 output is falling?

Code:

import numpy as np
import random

peso=False
def aleatorizar():
	peso=random.choice([False, True])
def activar(input1):
	if(peso!=False and peso!=True):
		if(peso==False):
			return not input1
		else:
			return input1
def entrenar(input1,output1):
	if(activar(input1)!=output1):
		aleatorizar()
while(True):
	entrenar(False,True)
	entrenar(True,True)
	print("false => "+str(activar(False))+" true => "+str(activar(False)))

I’ve not tried your code yet, but you define peso at module scope, then don’t use the global statement in your aleatorizar function.

Edit: Also, you state that it’s failing, but don’t tell us your expected or actual output when you run it.

Edit #2: Your logic in activar is suspect. It seems peso can only be True or False, yet you expect it to be neither inside activar.

1 Like

Hello,

for starters, for your aleatorizar function, there is nothing
that is being returned to the caller.

I spect making neural networks from 0 so… i started with this a simple perceptron with 1 input, 1 output and boolean values.

Yes, i tried this global here:

import numpy as np
import random

global peso

def aleatorizar():
	peso=random.choice([False, True])

def activar(input1):
	if(peso==False):
		return not input1
	if(peso==True):
		return input1

def entrenar(input1,output1,intensity):
	for i in range(0,intensity):
		if(activar(input1)!=output1):
			aleatorizar()

while(True):
	aleatorizar()
	entrenar(False,True,10)
	entrenar(True,True,10)
	print("false => "+str(activar(False))+" true => "+str(activar(True)))

And i tried this:

import numpy as np
import random

peso

def aleatorizar():
	global peso
	peso=random.choice([False, True])

def activar(input1):
	if(peso==False):
		return not input1
	if(peso==True):
		return input1

def entrenar(input1,output1,intensity):
	for i in range(0,intensity):
		if(activar(input1)!=output1):
			aleatorizar()

while(True):
	aleatorizar()
	entrenar(False,True,10)
	entrenar(True,True,10)
	print("false => "+str(activar(False))+" true => "+str(activar(True)))

And nothing…

Note also that there is nothing in your script that monitors a condition for it to break from the infinite while loop. Did you have one in mind? If not, it will never stop and just keep printing the results.

If you intend to run this loop for a fixed amount of iterations, then the for loop is a better option.

The break while is enough good for me

It continues predicting fake results:

From the screenshot, you are forcing it to stop via a keyboard interrupt. I mean via a conditional statement.

Yes? Its bad? IDK to add a condition.

Check your activar definition. Can it ever return anything other than True?

Here are two potential links but you can perform your own search.

Activar returns the unaltered value when the weight (peso) its set to true, but when it set to false, its return the inverse value. Check my code:

def activar(input1):
	if(peso==False):
		return not input1
	if(peso==True):
		return input1

And it didnt work. Check my image:

Edit: Never mind on this: It can return not False or True.
(I shouldn’t try analyzing code on my phone.)

Also, if you haven’t already, I would pick up a static code checker, like pylint. Such tools can pick up a whole host of issues without requiring you to run the code.

Why? It only returns not False when the conditions are favorable for that, or that i think…

And i checked it. The not False value returns true:

I still don’t understand what you expect the output to be. Given that you are randomizing the value of peso, output doesn’t seem like it should be deterministic. Here’s my latest version of your script. It runs and produces output, but as I indicated, I don’t know what to expect as far as output is concerned. Note also, that I might well have not got exactly what’s in your code, since you are posting screen shots, not text.

#!/usr/bin/env python3

"perceptron"

import random

peso = False

def aleatorizar():
    "randomize"
    global peso
    peso = random.choice([False, True])

def activar(input1):
    "activate"
    return input1 if peso else not input1

def entrenar(input1, output1, intensity):
    "train"
    for _ in range(intensity):
        if activar(input1) != output1:
            aleatorizar()

for i in range(1000):
    aleatorizar()
    entrenar(False, True, 10)
    entrenar(True, True, 10)
    print(f"false => {activar(False)}, true => {activar(True)}")

This is your code?:

import random

peso = False

def aleatorizar():
    global peso
    peso = random.choice([False, True])

def activar(input1):
    return input1 if peso else not input1

def entrenar(input1, output1, intensity):
    for _ in range(intensity):
        if activar(input1) != output1:
            aleatorizar()

for i in range(1000):
    aleatorizar()
    entrenar(False, True, 10)
    entrenar(True, True, 10)
    print(f"false => {activar(False)}, true => {activar(True)}")

Because its giving me this result (IDK why):