Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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
Python 简单神经网络值错误-形状未对齐_Python_Numpy_Neural Network - Fatal编程技术网

Python 简单神经网络值错误-形状未对齐

Python 简单神经网络值错误-形状未对齐,python,numpy,neural-network,Python,Numpy,Neural Network,我对此一无所知,但我开始学习神经网络。 我想用Python和Numpy制作简单的NN。我在Youtube上看了一个关于这一点的教程,我做了同样的事情,但我发现了一个错误: output = sigmoid(np.dot(input_layer, weights)) ValueError: shapes (13,3) and (13,1) not aligned: 3 (dim 1) != 13 (dim 0) 我知道我的输出数组应该看起来像1D数组,但由于某些原因,我无法得到它。 我做

我对此一无所知,但我开始学习神经网络。 我想用
Python
Numpy
制作简单的NN。我在Youtube上看了一个关于这一点的教程,我做了同样的事情,但我发现了一个错误:

    output = sigmoid(np.dot(input_layer, weights))
ValueError: shapes (13,3) and (13,1) not aligned: 3 (dim 1) != 13 (dim 0)
我知道我的输出数组应该看起来像1D数组,但由于某些原因,我无法得到它。 我做错了什么

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],
                   'input 2':[0.3, 0.5, -0.4, -0.2, 0.9, 0, 0.35, -0.4, -0.9, 0.4, 0.3, -0.1, 0.1],
                   'input 3':[0, 0.4, 0, -0.1, 0.4, -0.2, 0.4, -0.3, -0.1, 0.1, 0.3, 0, 0.5],
                   'result':[1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1]})

print(df)

def sigmoid(x):

    return 1 / (1 + np.exp(-x))

features = np.array(df.iloc[:,:-1])
results =  np.array(df.iloc[:,-1:]).T

np.random.seed(10)

weights = 2 * np.random.random((13,1)) - 1

print('These are my random weights:\n')
print(weights)


for iteration in range(1):

    input_layer = features

    output = sigmoid(np.dot(input_layer, weights))

print('\nOutput result:\n', output)

我设法找到了结果:

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)