Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用NumPy用Python制作Tunning神经网络_Python_Numpy_Neural Network - Fatal编程技术网

用NumPy用Python制作Tunning神经网络

用NumPy用Python制作Tunning神经网络,python,numpy,neural-network,Python,Numpy,Neural Network,我已经写了一个神经网络的代码,它使用了sigmoid函数。我用NumPy和Python制作的。 代码运行良好,但现在我想对其进行调优,以提高准确性。我如何调整我的NN,我需要添加一些参数,还是向它添加隐藏层? 有可能吗 这是我的代码: 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,

我已经写了一个神经网络的代码,它使用了sigmoid函数。我用NumPy和Python制作的。 代码运行良好,但现在我想对其进行调优,以提高准确性。我如何调整我的NN,我需要添加一些参数,还是向它添加隐藏层? 有可能吗

这是我的代码:

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)

outputs = outputs.round(0).tolist()
outputs  = list(itertools.chain(*outputs))

outputs.insert(0,'None')

df['output prediction'] = outputs
print(df)

df1 = df.tail(len(df)-1)
#print(df1)

acc = 0
for i, j in zip(df1['result'] ,df1['output prediction']):

    if i == j:

        acc += 1

accuracy = round(acc * 100 /len(df1), 2)
print(accuracy)
我想我应该把它加在我定义权重的部分下面,但我不确定

谢谢你的帮助

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)

alpha=0.1#define alpha
features = df.iloc[:,:-1]
results =  df.iloc[:,-1:]
features=np.array(features)
results=np.array(results)

np.random.seed(1)


weight0  = 2*np.random.random((3,10)) - 1 #3 - number of features; 10 - number of nodes in hidden layer
weight1  = 2*np.random.random((10,4)) - 1 #10 - number of nodes in hidden layer; 4 - number of nodes in output layer
weight2  = 2*np.random.random((4,1)) - 1 #4 - number of nodes in output layer; 1 - number of labels
# you can change layer's nodes, but they must be able to make dot product. For example (320,160) and (160,40)
for iteration in range(1000):

    l0 = features
    l1 = sigmoid(np.dot(l0,weight0)) 
    l2 = sigmoid(np.dot(l1,weight1))
    l3 = sigmoid(np.dot(l2,weight2))

    l3_error = results - l3
    print ("Error after "+str(iteration)+" iterations:" + str(np.mean(np.abs(l3_error))))
    l3_delta = l3_error*sigmoid_derivate(l3)
    l2_error = l3_delta.dot(weight2.T)
    l2_delta = l2_error * sigmoid_derivate(l2)
    l1_error = l2_delta.dot(weight1.T)
    l1_delta = l1_error * sigmoid_derivate(l1)
    weight2 += alpha*l2.T.dot(l3_delta)
    weight1 += alpha*l1.T.dot(l2_delta)
    weight0 += alpha*l0.T.dot(l1_delta)

这是您的代码,包含1个输入层、1个隐藏层和1个输出层。

谢谢我的朋友,还有一个问题吗?有没有办法确定节点的数量((3,10)、(10,4)、(4,1))和层的数量?当然。要更改层数,必须添加权重(权重3、权重4等)、层(l4、l5等)、增量(l4_增量、l5_增量等)、错误(l4_错误、l5_错误等),最后更新权重(权重3+=alpha*l3.T.dot(l4_增量)等)。要更改节点数,请查看代码,我已对其进行了编辑。