【问题标题】:many invocations w/out print() to console | Python许多调用不带 print() 到控制台 | Python
【发布时间】:2021-05-12 10:28:02
【问题描述】:

我的打印语句,在许多调用中,没有显示到控制台。

我的程序的目的是在perceptron 中显示forward pass 背后的数学“计算”。但是,在这里了解数学并不重要。

让我们粗略地假设引用的任何数学都是正确的。

我的问题出现在# -- OUTPUT -- in Perceptron.py

请原谅程序的大小。

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")

输出应该出现在以下:

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

【问题讨论】:

  • 有趣,好的。可能是我的环境。我正在使用 repl.it。如果解决后将报告不打印的原因。
  • 索德定律。我使用了这个最小的代码解决方案,它可以工作:/。我将附加整个 ActivationFunctions.py 以及 main.py
  • @Reti43 你能用问题中的更新代码再试一次吗?
  • 干杯人。去afk。 1 小时后回来

标签: python printing deep-learning invoke


【解决方案1】:

正如好心的评论者所指出的那样;在 ActivationFunctions.py 中:

方法invoker() 中的初始if statement 在其条件中需要显式

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

应该是:

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

其中:in name 被声明两次。否则,我们提到了一个可能的子字符串binary,但出于什么原因?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-28
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多