Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.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
Keras提出的python神经网络的舍入误差_Python_Neural Network_Precision_Keras - Fatal编程技术网

Keras提出的python神经网络的舍入误差

Keras提出的python神经网络的舍入误差,python,neural-network,precision,keras,Python,Neural Network,Precision,Keras,我使用Keras高级神经网络库学习python。 我制作了一个简单的神经网络,只有一个神经元,用于2个输入和1个输出的线性分类问题。 我的网络运行良好,但如果我想使用网络的权重计算预测自我(以证明神经网络的工作),那么神经网络触发的自身“实现”与Keras的“实现”之间会有一点差异。例如: 在经过训练的网络上使用predict()方法: testset = np.array([[5],[1]]) prediction = model.predict(testset.transpose()) pr

我使用Keras高级神经网络库学习python。 我制作了一个简单的神经网络,只有一个神经元,用于2个输入和1个输出的线性分类问题。 我的网络运行良好,但如果我想使用网络的权重计算预测自我(以证明神经网络的工作),那么神经网络触发的自身“实现”与Keras的“实现”之间会有一点差异。例如:
在经过训练的网络上使用predict()方法:

testset = np.array([[5],[1]])
prediction = model.predict(testset.transpose())
print prediction
结果是:

[[ 0.22708023]]
自行计算结果:

# get the weights of the neural network
for layer in model.layers:
    weights = layer.get_weights()
# the math of the prediction
prediction_calc = testset[0]*weights[0][0]+testset[1]*weights[0][1]+1*weights[1]
print prediction_calc
结果是:

[ 0.22708024]
为什么这两个值之间有这么小的差异?正如我在计算预测计算变量时所做的那样,神经网络应该做得更多

我认为这应该是变量之间的转换。如果我打印权重变量,我明白了,它是一个由32个值组成的矩阵

[array([[-0.07256483],
   [ 0.02924729]], dtype=float32), array([ 0.56065708], dtype=float32)]
我不知道为什么会有这种差异,这应该是一个简单的舍入误差,但我不知道如何避免它

以下是完整的代码以获取帮助:

import numpy as np

# fix random seed for reproducibility
seed = 7
np.random.seed(seed)

# load and transpose input data set
inputset = np.loadtxt("learningsets/hyperplane2d_INPUTS.csv", delimiter=",")
inputset = inputset.transpose()

# load output data set
outputset = np.loadtxt("learningsets/hyperplane2d_OUTPUTS.csv", delimiter=",")
outputset = outputset    

# build the simple NN
from keras.models import Sequential
model = Sequential() 

from keras.layers import Dense
# The neuron
# Dense: 2 inputs, 1 outputs . Linear activation
model.add(Dense(output_dim=1, input_dim=2, activation="linear"))
model.compile(loss='mean_squared_error', metrics=['accuracy'], optimizer='sgd')
model.fit(inputset, outputset, nb_epoch=20, batch_size=10)

# calculate prediction for one input
testset = np.array([[5],[1]])
predictions = model.predict(testset.transpose())
print predictions

# get the weights of the neural network
for layer in model.layers:
    weights = layer.get_weights()
# the math of the prediction
prediction_calc = testset[0]*weights[0][0]+testset[1]*weights[0][1]+1*weights[1]
print prediction_calc