简体   繁体   English

Keras简单神经网络,用于非逻辑生成错误的输出

[英]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.

相关问题 theano - 评估神经网络的输出 - theano - evaluate the output of a neural network 神经网络收敛到零输出 - Neural Network converging to zero output 神经网络 output 的两个错误 - two errors with neural network output 在Raspberry Pi上加载经过训练的Keras神经网络模型 - Load a trained Keras neural network model on Raspberry Pi 卷积神经网络input_shape尺寸误差(KERAS,PYTHON) - Convolution Neural Network input_shape dimension error (KERAS ,PYTHON) Keras神经网络错误:使用序列设置数组元素 - Keras Neural Network Error: Setting an Array Element with a Sequence 卷积神经网络中密集层和输出层的尺寸如何计算? - How to calculate dimensions of the dense and output layer in convolutional neural network? 我可以在使用Keras的线性回归中使用神经网络吗? 如果是,如何? - Can I use a neural network on a linear regression using Keras? If yes , How? 如何使用 scikit BaggingClassifier 和 keras 卷积神经网络作为基本估计器通过 keras-scikit 包装器进行装袋? - How to do bagging using scikit BaggingClassifier with keras Convolutional Neural Network as base estimator via keras-scikit wrapper? 如何创建简单的3层神经网络并使用有监督的学习教它? - How to create simple 3-layer neural network and teach it using supervised learning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM