2

My print statements, within many invocations, are not displaying to console.

The purpose of my program is to display "working out" of the math behind forward pass in a perceptron. However, the math isn't important to know here.

Let's crudely assume any math referenced is sound.

My problem occurs at # -- OUTPUT -- in Perceptron.py.

Excuse the size of the program.

main.py

import os
os.system('clear')
import Perceptron

Perceptron.py

import ActivationFunctions as af
import numpy as np
from math import e

X = [[0.5, 0.3], [-0.5, 0.9], [0, -0.1], [1, 0]]
target = 0.7
LR = 0.01
dp = 5

# ----- Forward Pass -----
print('Forward Pass')

# -- INPUT --
in_str = 'in = '
for input in X:
  substr = '('+str(input[0])+' x '+str(input[1])+') + '
  in_str += substr
in_str = in_str[:-3]
print(in_str)

calcs = [x * y for x, y in X]
in_str = '   = '
for c in calcs:
  substr = '('+str(c)+') + '
  in_str += substr
in_str = in_str[:-3]
print(in_str)

ans = round(sum([x * y for x, y in X]), dp)
print('   = ' + str(ans))
print()

# PROBLEM OCCURS HERE
# -- OUTPUT --
# SIGMOID
out = af.invoker('softmax', LR, ans, dp)
print()

ActivationFunctions.py

import numpy as np
from math import e

def binary_step(ans, dp):
  if ans >= 0: return 1
  else: return 0

def identity(ans, dp):
  return round(ans, dp)

def logistic(ans, dp):
  return round((1)/(1+(e**-ans)), dp)

def tanh(ans, dp):
  return round(((e**ans) - (e**-ans))/((e**ans) + (e**-ans)), dp)

def relu(ans, dp):
  if ans < 0: return 0
  else: return round(ans, dp)

def leaky_relu(LR, ans, dp):
  if ans < 0: return round(LR*ans, dp)
  else: return round(ans, dp)

def softmax(ans, dp):
  print('out = 1 / (1 + e^-'+str(+ans)+')')
  out = round(1 / (1 + e**-ans), dp)
  print('    = '+str(out))
  return out

def invoker(name, LR, ans, dp):
  name = name.lower()
  if 'binary' or 'step' in name: return binary_step(ans, dp)
  elif name == 'identity': return identity(ans, dp)
  elif name == 'logistic': return logistic(ans, dp)
  elif name == 'tanh': return tanh(ans, dp)
  elif name == 'relu': return relu(ans, dp)
  elif name == 'lrelu' or 'leaky' in name: return leaky_relu(LR, ans, dp)
  elif name == 'softmax': return softmax(ans, dp)
  else: print("ENTER VALID ACTIVATION FUNCTION")

Output should appear right after:

Forward Pass
in = (0.5 x 0.3) + (-0.5 x 0.9) + (0 x -0.1) + (1 x 0)
   = (0.15) + (-0.45) + (-0.0) + (0)
   = -0.3
  • Interesting, ok. Maybe it's my environment. I am using repl.it. Will report back with reasoning for not printing if when solved. – ThePewCDestroyer May 12 '21 at 10:43
  • Sod's Law. I have used this minimal code solution and it works :/. I will append entire ActivationFunctions.py as well as main.py – ThePewCDestroyer May 12 '21 at 10:48
  • @Reti43 Could you try again with the updated code in the questions, please? – ThePewCDestroyer May 12 '21 at 10:50
  • Cheers man. Going afk. Will be back in 1 hr – ThePewCDestroyer May 12 '21 at 10:58
  • Does this answer your question? [How to test multiple variables against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-single-value) – Reti43 May 12 '21 at 11:01
  • Your issue is in `if 'binary' or 'step' in name`, which doesn't behave as you expect. The flagged duplicate goes into more detail about this. [This one](https://stackoverflow.com/questions/21344842/if-a-or-b-in-l-where-l-is-a-list-python) is also more direct about your case but still the same thing. – Reti43 May 12 '21 at 11:04
  • Welcome to Stack Overflow! Please read about [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). You can also use [Python-Tutor](http://www.pythontutor.com/visualize.html#mode=edit) which helps to visualize the execution of the code step-by-step. If this doesn't help to solve the problem, use it to create a [mre] of your problem after narrowing it down – Tomerikoo May 12 '21 at 11:55

1 Answers1

0

As pointed out by kind commenters; in ActivationFunctions.py:

The initial if statement in method invoker() needs to be explicit in its condition.

if 'binary' or 'step' in name: return binary_step(ans, dp)

should be:

if 'binary' in name or 'step' in name: return binary_step(ans, dp)

where: in name is stated twice. Otherwise, we are mentioning a possible substring binary but for what reason?

desertnaut
  • 57,590
  • 26
  • 140
  • 166