用于解决 c = a1-a2 问题的简单神经网络的 python 代码不起作用

发布于 01-12 00:02 字数 839 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下慵懒的猫2025-01-19 00:02:14

这里有多个问题。

  1. 您正在使用 sigmoid 输出函数,但您的目标包括负数。您的模型无法工作,因为 sigmoid 将输出限制为 [0, 1]。一般来说,您必须考虑数据范围以及合适的输出函数是什么。在这里,只需在输出层中使用no激活。
  2. 准确性对于回归任务来说毫无意义,因为它只会在完全相等的情况下才算匹配。准确度仅用于分类任务。因此,完全忽略准确性,只查看平方误差/损失。
  3. 你只是训练的时间不够长。我增加到1000步。
  4. 请注意,您的目标函数是线性的,因此您甚至不需要隐藏层(但这也没有什么坏处)。

使用此代码:

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.Dense(16, input_dim=2, activation='relu'))
model.add(layers.Dense(1))
model.compile(loss='mean_squared_error',
    optimizer='adam',
    metrics=['accuracy'])
model.fit(training_data, target_data, epochs=1000)
scores = model.evaluate(training_data, target_data)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
print (model.predict(training_data))

我得到输出 [[ 1.0008523 ], [-0.00198349], [-0.99906766]]

请注意,对您问题的评论是不正确的,您应该期望神经网络在小型训练集上表现得很好 - 如果不是,则您的模型和/或训练过程可能存在问题。

Multiple issues here.

  1. You are using a sigmoid output function, but your targets include negative numbers. Your model cannot work because sigmoid restricts the output to [0, 1]. In general, you have to think about your data range and what an appropriate output function can be. Here, just use no activation in the output layer.
  2. Accuracy is meaningless for regression tasks as it will only count a match in case of an exact equality. Accuracy is only used for classification tasks. Thus, disregard accuracy completely and only look at the squared error/loss.
  3. You are simply not training long enough. I increased to 1000 steps.
  4. Note that your target function is linear, so you don't even need a hidden layer (but it doesn't hurt here, either).

With this code:

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.Dense(16, input_dim=2, activation='relu'))
model.add(layers.Dense(1))
model.compile(loss='mean_squared_error',
    optimizer='adam',
    metrics=['accuracy'])
model.fit(training_data, target_data, epochs=1000)
scores = model.evaluate(training_data, target_data)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
print (model.predict(training_data))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文