我在neural network
中为简单的Python
写了一个代码。神经网络使用Sigmoid
函数来预测结果(0或1)。
我的问题是,如何根据自己的输入来预测结果?
例如,我要对这些输入值进行预测:
input 1: 0.3
input 2: -0.1
input 3: 0.1
my_input = [0.3, -0.1, 0.1]
我应该在哪里传递此参数/输入? 这是我的代码:
import numpy as np
import pandas as pd
df = pd.DataFrame({'input 1':[0.5, 0.3, 0, 0.1, 0.4, -0.4, 0.4, 0.1, -0.6, 0.2, 0.6, 0, 0.2, 0.2, -0.1, -0.1, 0, 0.4, -0.2, -0.4],
'input 2':[0.3, 0.6, -0.4, -0.2, 0.9, 0, 0.35, -0.4, -0.9, 0.4, 0.3, -0.1, 0.1, 0.3, 0.1, 0.1, 0.3, 0.1, 0.3, 0.3],
'input 3':[0, 0.4, 0, -0.1, 0.4, -0.2, 0.7, -0.3, -0.1, 0.1, 0.3, 0, 0.5, 0.4, -0.31, 0.1, 0.3, 0.1, 0.1, 0.2],
'result':[1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0]})
print(df)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivate(x):
return x * (1 - x)
features = df.iloc[:,:-1].to_numpy()
results = df.iloc[:,-1:].to_numpy()
np.random.seed(1)
weights = 2 * np.random.random((3,1)) - 1
print('These are my random weights:\n')
print(weights)
for iteration in range(100000):
input_layer = features
outputs = sigmoid(np.dot(input_layer, weights))
error = results - outputs
adjustments = error * sigmoid_derivate(outputs)
weights += np.dot(input_layer.T, adjustments)
df['output prediction'] = outputs.round(0)
print(df)
因此,输出应仅为一个值,零或一。
感谢您的帮助
答案 0 :(得分:1)
您的预测与训练中的预测方法相同:
my_output = sigmoid(np.dot(my_input, weights))
如果您尝试将培训的前三个示例用作输入,则会找到正确的输出:
my_input = [0.3,-0.1,0.1]
prediction: [1.]
my_input = [0.5,.3,0]
prediction: [1.]
my_input = [0.0,-.4,0.0]
prediction: [2.25648121e-13]
祝贺您实施了自己的培训!
答案 1 :(得分:0)
您可以通过以下方式实现:
# in addition to your previous code
In [14]: while True:
...: user_input = input("Enter the input: ")
...: user_input = [float(number) for number in user_input.split(',')]
...: outputs = sigmoid(np.dot(user_input, weights))
...: print("Outputs:",outputs)
...:
Enter the input: 0.6,0.1,0.9
Outputs: [1.]
Enter the input:
这将: