2017-10-07 36 views
1

我试图让我如何使用由Keras模型产生的权重来计算Python中的结果清楚的认识(即预测值,而不使用Keras模型来预测)。当我有比下面的一个简单的案件(2个功能,没有隐藏层或1个功能隐藏层),我得到的结果,我希望(我计算出的值作为模型预测值相同)。在下面的代码中,输出值不一致。所以我要么不理解,要么看不到什么。如果病人可以用简单的英语解释我怎么能做到这一点,我会很高兴。我无法使用Keras的权重来计算模型

示例代码(具有2点的特征和1隐藏层)

from keras.models import Sequential 
from keras.layers import Dense 
import numpy 

# sigmoid function: nonlinearity. 
def nonlin(x, deriv = False): 
    if (deriv == True): 
     return x * (1 - x) 
    return 1/(1 + numpy.exp(-x)) 

# fix random seed for reproducibility 
numpy.random.seed(5) 
# load dataset 
dataset = numpy.loadtxt("values2.txt", delimiter=",") # 4 samples 2 factors 1 y 
# split into input (X) and output (Y) variables 
X = dataset[:,0:2] 
Y = dataset[:,2] 
# create model (1 hidden layer) 
model = Sequential() 
model.add(Dense(2, input_dim=2, activation='relu')) 
model.add(Dense(1, activation='sigmoid')) 
# Compile model 
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
# Fit the model 
model.fit(X, Y, epochs=200, batch_size=1) 
# calculate predictions 
predictions = model.predict(X) 
# round predictions 
rounded = [round(x[0]) for x in predictions] 
#print(rounded) 
#model.summary() 
for layer in model.layers: 
    weights = layer.get_weights() # list of numpy arrays 
    print(weights) 
M = model.get_weights() 
# Example where X = 0() 
print (X[0,0], X[0,1], "=", Y[0]) 
L1 = numpy.array([ [0.0], [0.0] ]) # to hold result of input * weights 
L1[0] = X[0,0] * M[0][0][0] + X[0,1] * M[0][1][0] + M[1][0] 
L1[1] = X[0,0] * M[0][0][1] + X[0,1] * M[0][1][1] + M[1][1] 
L2 = numpy.array([ [0.0] ]) 
L2[0] = L1[0] * M[2][0][0] + L1[1] * M[2][1][0] + M[3][0] 
print (nonlin(L2[0]), predictions[0]) # compare 

输入(Values2.txt:所述第一X确定Y)

0,0,0 
0,1,0 
1,0,1 
1,1,1 

输出 ...

Epoch 200/200 
4/4 [==============================] - 0s - loss: 0.4380 - acc: 1.0000  
[0.0, 0.0, 1.0, 1.0] 
[array([[ 1.69063699, 0.1998844 ], 
     [ 0.07858475, 0.89751321]], dtype=float32), array([-0.00052627, -0.17762977], dtype=float32)] 
[array([[ 0.83898878], 
     [-0.54218996]], dtype=float32), array([-0.05681464], dtype=float32)] 
0.0 0.0 = 0.0 
[ 0.50976198] [ 0.48580015] 

感谢的Matias Valdenegro

响应于你的意见我已经改变的代码下面并可以产生期望的输出。我在第一层将激活从relu改为sigmoid,并在第二层使用nonlin函数对第一层的结果使用nonlin函数。那是对的吗?从修改后的代码

修改后的代码

from keras.models import Sequential 
from keras.layers import Dense 
import numpy 

# sigmoid function: nonlinearity. 
def nonlin(x, deriv = False): 
    if (deriv == True): 
     return x * (1 - x) 
    return 1/(1 + numpy.exp(-x)) 

# fix random seed for reproducibility 
numpy.random.seed(5) 
# load dataset 
dataset = numpy.loadtxt("values2.txt", delimiter=",") # 4 samples 2 factors 1 y 
# split into input (X) and output (Y) variables 
X = dataset[:,0:2] 
Y = dataset[:,2] 
# create model (1 hidden layer) 
model = Sequential() 
model.add(Dense(2, input_dim=2, activation='sigmoid')) 
model.add(Dense(1, activation='sigmoid')) 
# Compile model 
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
# Fit the model 
model.fit(X, Y, epochs=200, batch_size=1) 
# calculate predictions 
predictions = model.predict(X) 
# round predictions 
rounded = [round(x[0]) for x in predictions] 
#print(rounded) 
#model.summary() 
for layer in model.layers: 
    weights = layer.get_weights() # list of numpy arrays 
    print(weights) 
M = model.get_weights() 
# Example where X = 0() 
print (X[0,0], X[0,1], "=", Y[0]) 
L1 = numpy.array([ [0.0], [0.0] ]) # to hold result of input * weights 
L1[0] = X[0,0] * M[0][0][0] + X[0,1] * M[0][1][0] + M[1][0] 
L1[1] = X[0,0] * M[0][0][1] + X[0,1] * M[0][1][1] + M[1][1] 
L2 = numpy.array([ [0.0] ]) 
L2[0] = nonlin(L1[0]) * M[2][0][0] + nonlin(L1[1]) * M[2][1][0] + M[3][0] 
print (nonlin(L2[0]), predictions[0]) # compare 

输出

Epoch 200/200 
4/4 [==============================] - 0s - loss: 0.6463 - acc: 1.0000  
[array([[ 1.70278633, 0.0848918 ], 
     [-0.0271775 , 0.92663836]], dtype=float32), array([-0.14723039, 0.00718958], dtype=float32)] 
[array([[ 0.56880862], 
     [-0.60756117]], dtype=float32), array([ 0.03559623], dtype=float32)] 
0.0 0.0 = 0.0 
[ 0.4985573] [ 0.4985573] 

我添加了一个RELU函数(X *(X> 0)) 计算是现在为遵循并产生预期的结果。

L1 = numpy.array([ [0.0], [0.0] ]) # to hold result of input * weights 
L1[0] = relu(X[0,0] * M[0][0][0] + X[0,1] * M[0][1][0] + M[1][0]) 
L1[1] = relu(X[0,0] * M[0][0][1] + X[0,1] * M[0][1][1] + M[1][1]) 
L2 = numpy.array([ [0.0] ]) 
L2[0] = L1[0] * M[2][0][0] + L1[1] * M[2][1][0] + M[3][0] 
print (nonlin(L2[0]), predictions[0]) 

谢谢。 (假设这是最基本的答案,我不知道我应该做的事情标记作为这里的答案是什么......?)

+1

我没有看到你在你的Python代码应用RELU。这可能是问题所在。 –

+0

谢谢。我已经修改了我的代码作为回应。查看修改。 –

+1

仍然在新代码中没有RELU ... –

回答

0

(我不知道为什么没有人愿意回答这个问题,我不知道我想只表达我对Matias和Daniel已经说过的内容的理解)

当在神经网络中乘以权值以找到每层的节点值时,您需要记住包括激活功能,通常是ReLU功能(整流线性单元)(又名斜坡功能)。