[英]Keras simple neural network for NOT logic generating wrong output
I am new to deep learning and I am trying to implement NOT
logic in keras
. 我是深度学习的新手,我正在尝试在
keras
实现NOT
逻辑。 But the results are not correct. 但是结果不正确。 Below is the code.
下面是代码。
from keras.layers import Input, Dense
from keras.models import Model
import numpy as np
inputs = Input(shape=(1,))
x = Dense(1024, activation='relu')(inputs)
x = Dense(2048, activation='relu')(x)
predictions = Dense(1, activation='softmax')(x)
model = Model(input=inputs, output=predictions)
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
X = np.array([[0.], [1.]], dtype=np.float32)
y = np.array([[1.], [0.]], dtype=np.float32)
print "learning....."
model.fit(X, y, nb_epoch=100)
print model.predict(X)
Output: 输出:
On every epoch the output is same: 在每个纪元上,输出都是相同的:
Epoch 100/100
2/2 [==============================] - 0s - loss: 0.5000 - acc: 0.5000
and the predictions are: 和预测是:
[[ 1.]
[ 1.]]
I am not sure, what is wrong with this network. 我不确定,该网络出了什么问题。
Your usage of the loss looks wrong. 您对损失的使用方式看起来不对。 Softmax is typically used for multi-class predictions and you already setup the output to be consisting of 2 values using
Dense(2)
. Softmax通常用于多类别预测,并且您已经使用
Dense(2)
将输出设置为由2个值组成。 Therefore make your target a multi-class target of dim=2
. 因此,使您的目标成为
dim=2
的多目标。
from keras.layers import Input, Dense
from keras.models import Model
import numpy as np
inputs = Input(shape=(1,))
x = Dense(1024, activation='relu')(inputs)
x = Dense(2048, activation='relu')(x)
predictions = Dense(2, activation='softmax')(x)
model = Model(input=inputs, output=predictions)
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
X = np.array([[0.], [1.]], dtype=np.float32)
y = np.array([[1., 0], [0., 1]], dtype=np.float32)
print "learning....."
model.fit(X, y, nb_epoch=100)
print model.predict(X)
Output 输出量
Epoch 100/100
2/2 [==============================] - 0s - loss: 1.5137e-07 - acc: 1.0000
[[ 9.99880254e-01 1.19736877e-04]
[ 5.35955711e-04 9.99464035e-01]]
Edit: One can argue if the above setup of final-layer activation and loss-function is a good one for binary-classification (probably not). 编辑:有人可以争辩说,上述最后一层激活和损失函数的设置是否适合二进制分类(可能不是)。 Link
链接
Alternative using sigmoid and only one target: 使用S形和仅一个目标的替代方法:
from keras.layers import Input, Dense
from keras.models import Model
import numpy as np
inputs = Input(shape=(1,))
x = Dense(1024, activation='relu')(inputs)
x = Dense(2048, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(input=inputs, output=predictions)
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
X = np.array([[0.], [1.]], dtype=np.float32)
y = np.array([1., 0.], dtype=np.float32)
print "learning....."
model.fit(X, y, nb_epoch=100)
print model.predict(X)
Output 输出量
Epoch 100/100
2/2 [==============================] - 0s - loss: 9.9477e-07 - acc: 1.0000
[[ 0.99945992]
[ 0.00129277]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.