我想建立一个只有一层的简单神经网络。我认为代码本身没有问题,但是存储了训练数据和其他内容的数组。我得到了这个奇怪的ValueError,我不知道为什么。 这是我的代码:
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
training_inputs = np.array([[0,0,1],
[1,1,1],
[1,0,1],
[0,1,1]])
training_output = np.array([[0,
1,
1,
0]])
synaptic_weights = 2 * np.random.random((3,1)) - 1
for iterantion in range(2000):
input_layer = training_inputs
outputs = sigmoid(np.dot(input_layer, synaptic_weights))
synaptic_weights += np.dot(input_layer,((training_output - outputs)
* (outputs * (1 - outputs))))
print(outputs)
print("Session closed")
我收到此错误:
(base) c:\Users\juliu\AppData\Local\Programs\Python\Python37-32\Lib\curses>cd c:\Users\juliu\AppData\Local\Programs\Python\Python37-32\Lib\curses && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && C:\Users\juliu\Anaconda3\python.exe c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd_launcher.py --default --nodebug --client --host localhost --port 50932 c:\Users\juliu\AppData\Local\Programs\Python\Python37-32\Lib\curses\AI.py "
Traceback (most recent call last):
File "c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd_launcher.py", line 45, in <module>
main(ptvsdArgs)
File "c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\__main__.py", line 265, in main
wait=args.wait)
File "c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\__main__.py", line 256, in handle_args
run_main(addr, name, kind, *extra, **kwargs)
File "c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 52, in run_main
runner(addr, name, kind == 'module', *extra, **kwargs)
File "c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\runner.py", line 32, in run
set_trace=False)
File "c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\Users\juliu\.vscode\extensions\ms-python.python-2018.11.0\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "c:\Users\juliu\AppData\Local\Programs\Python\Python37-32\Lib\curses\AI.py", line 25, in <module>
synaptic_weights += np.dot(input_layer,((training_output - outputs) * (outputs * (1 - outputs))))
ValueError: shapes (4,3) and (4,4) not aligned: 3 (dim 1) != 4 (dim 0)
如果有人对我可以进行更改有任何想法,请写下来!
答案 0 :(得分:0)
您的training_inputs
和training_output
矩阵维不适合相乘。 training_inputs
应该换位。