用于解决 c = a1-a2 问题的简单神经网络的 python 代码不起作用
我是 python 和神经网络的新手,因此我将不胜感激。试图弄清楚如何制作这个计算 c = a1 - a2 的简单神经网络,但不确定从哪里开始,因为不需要偏差项,如果 a=[(1,0),(0,0),(0 ,1)] 那么我如何计算每个元组的 c ?或者找到张量的权重?
training_data = np.array([[1,0],[0,0],[0,1]], "float32")
target_data = np.array([[1],[0],[-1]], "float32")
print("input : " + str(training_data))
print("output : " + str(target_data))
model = models.Sequential()
model.add(layers.core.Dense(16, input_dim=2, activation='relu'))
model.add(layers.core.Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['accuracy'])
model.fit(training_data, target_data, epochs=100)
scores = model.evaluate(training_data, target_data)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
print (model.predict(training_data).round())
这就是我所拥有的,但其准确性(~33%)非常低,不确定我错过了什么
I'm new to python and neural networks so I'd appreciate any assistance. trying to figure out how to make this simple NN that computes c = a1 - a2 but not sure where to start as there is no need for bias terms, if a=[(1,0),(0,0),(0,1)] then how can I compute c for each tuple? or find weights for the tensor?
training_data = np.array([[1,0],[0,0],[0,1]], "float32")
target_data = np.array([[1],[0],[-1]], "float32")
print("input : " + str(training_data))
print("output : " + str(target_data))
model = models.Sequential()
model.add(layers.core.Dense(16, input_dim=2, activation='relu'))
model.add(layers.core.Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['accuracy'])
model.fit(training_data, target_data, epochs=100)
scores = model.evaluate(training_data, target_data)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
print (model.predict(training_data).round())
this is what I have but its accuracy (~33%) is very low and not sure what I am missing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

这里有多个问题。
使用此代码:
我得到输出
[[ 1.0008523 ], [-0.00198349], [-0.99906766]]
。请注意,对您问题的评论是不正确的,您应该期望神经网络在小型训练集上表现得很好 - 如果不是,则您的模型和/或训练过程可能存在问题。
Multiple issues here.
With this code:
I get outputs
[[ 1.0008523 ], [-0.00198349], [-0.99906766]]
.NB the comments on your question are incorrect, you should expect a neural network to do very well on a small training set -- if it's not, something is likely wrong with your model and/or training process.