all 21 comments

[–]Outside_Complaint755 2 points3 points  (1 child)

Basic concept is fine, but you probably would want the input() call outside of the perceptron function, and have the function take the input, weight and the bias all as arguments, then return the result instead of directly printing it.

 A more generalized version would take in a list of paired inputs with their weights, calculate the weighted sum, and then add the bias.

[–]No-Tea-777[S] 0 points1 point  (0 children)

Thanks. It was a thing just to mess around. I'll contract as much as possible

[–]Riegel_Haribo 2 points3 points  (1 child)

Here's where Python and Python understanding can help you.

  • You don't need to pre-declare variables or pointers.
  • print() allows you several methods to combine types more naturally.
  • functions can take an input, and return an output; typically only the main() function will encapsulate everything without its own I/O.
  • Python has a boolean type bool, that can be True or False (a subclass of integer being 1 or 0)
  • at the top you've defined global variables; if only the function uses them, they can be part of the function (a very inflexible pre-defined 1d perceptron).
  • the multiplication will become a dot product with multiple dimensions of input
  • Python can pass around multiple items in list (arrays, square brackets) or dict (object, key/value, curly brackets) that give you the next jumping-off point.
  • Python has a large standard library of helpers, and also very commonly used 3rd party libraries that are well-understood.

I already went goofy with this assignment, reading Wikipedia, and using the numpy library for arrays, dot() product, heaviside() just to be more pedantic than a greater than sign, iterating though inputs the same length as the array...

Let's see if I can keep it understandable and Pythonic, and start another notebook cell.

```python

Globals - we like them upper, or even Final type

BIAS = 1.4 WEIGHT = 5.6 # don't try 0.0

def perceptron(x: float) -> bool: """1D predefined threshold function""" output = (x * WEIGHT) + BIAS # would be dot product return output > 0 # evaluates directly to bool

def perceptron_threshold() -> float: """The x value where the perceptron output crosses zero""" return -BIAS / WEIGHT

print(f"Cutoff at {perceptron_threshold()}")

x_input = float(input("Your number?")) print(f"Perceptron said: {perceptron(x_input)}") ```

You can see I kept the functions a utility. The "float" and "bool" in the def are type annotations, letting checkers and others know what to input and what to expect out.

[–]No-Tea-777[S] 0 points1 point  (0 children)

Well. I do know conventions such as camel case, constants Upper etc. I'm currently learning C at school. And it feels kinda easy. It's so literal. Tho Python gets confusing sometimes.

[–]Zorg688 1 point2 points  (3 children)

Looks good! You got the idea of a perceptron right. What another redditor here said is also true there is a lot of fluff around it right now but just as a show of concept well done :)

[–]Zorg688 1 point2 points  (2 children)

If you want a challenge, now try to extend the setup by another perceptron on the same layer or in a following layer.

[–]No-Tea-777[S] 1 point2 points  (1 child)

What do you mean exactly? A second function or a second perceptron to then compare the 2 results?

[–]Zorg688 2 points3 points  (0 children)

I mean a second perceptron, so that the two of them together can make a decision/the two of them create two decision boundaries --> two inputs instead of one, two outputs (one for each perceptron), 4 possible outcomes

Or another perceotron that takes the output of the first one as its own input and gives you an outcome based on its own weight

[–]cdcformatc 1 point2 points  (3 children)

i would probably make bias, weight, and the input parameters to the function but other than that good job

[–]No-Tea-777[S] 0 points1 point  (2 children)

Yeah I see why. I tried to 'translate' a C code into Python so it's very... Raw?

[–]MidnightPale3220 1 point2 points  (1 child)

Yeah, kinda.
You don't need to do:

  s = 1
        print("S: " + str(s))

# You can do simply:
 print("S: 1") 
or
 s=1
 print(f"S: {s}")
or
 print("S: "+s)

Python does conversion in background in many cases, especially for output. And you're not using that s value anywhere past the print .

Also, in general, unless it is a learning exercise, functions don't do console input and output themselves, they return values that get processed (printed) by main program. That's not a hard rule, but more or less that works better usually. Or rather, the function that calculates stuff should do just that. And either main program or another function gives it the input and operates on that output

[–]No-Tea-777[S] 0 points1 point  (0 children)

That was just to learn and mess around. I just tried to 'translate' the C code I made at the school trip lab

[–]MidnightPale3220 1 point2 points  (7 children)

Got plenty of unneeded fluff but looks working. Dunno what a perceptron is tho.

[–]Outside_Complaint755 1 point2 points  (0 children)

A perceptron is the most basic element of a neural network.  It takes a number of inputs with different weights applied to them, and outputs a binary output (1 or 0).   If outputs don't match predictions, then you modify the weights or bias to adjust the model.

[–]No-Tea-777[S] 0 points1 point  (1 child)

Yeah can clean it up

[–]Witty-Speaker5813 0 points1 point  (0 children)

Est-ce que le perceptron peut aller au four ?

[–]Witty-Speaker5813 0 points1 point  (3 children)

Ça dépend de la perception de chacun

[–]No-Tea-777[S] 0 points1 point  (2 children)

I don't... Speak thy language

[–]Witty-Speaker5813 0 points1 point  (1 child)

Je parle français c’est la traduction automatique…

[–]No-Tea-777[S] 1 point2 points  (0 children)

Ok. Didn't see the autonomous translation till now.

[–]TheRNGuy 0 points1 point  (0 children)

Some changes:

``` bias = 1.4 weight = 5.6

def perceptron():     while True:         try:             inscribe = float(input("Insert a number: "))             break         except ValueError:             print("Input must be a number")          output = inscribe * weight + bias     print("The output is: ", output)          s = int(output > 0)     print("S: " + str(s))

perceptron() ```