【问题标题】:Tensorflow sigmoid regression stays linearTensorflow sigmoid 回归保持线性
【发布时间】:2021-11-18 11:44:16
【问题描述】:

我正在尝试使用张量流使一个简单的神经网络适合一个简单的函数,我知道我使用的结构和参数在 MatLab 中实现了这一点,但我需要将其移植到另一种语言(目前是 Python,但后来是 c++)。因此,我试图找到一个好的神经网络库,我认为那将是 TensorFlow,但事实证明它非常挑剔。这是代码的重要部分

import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
import matplotlib.pyplot as plt

f1 = lambda x: ((x < .5) * np.power(x, 2) + (x > .5) * x) * 2 -1
x = np.linspace(-1, 1, 180).reshape(-1,1)
y = f1(x).reshape(-1, 1)
model = keras.models.Sequential()
model.add(layers.Dense(100, activation=tf.keras.activations.sigmoid, input_shape=(1,)))
model.add(layers.Dense(1, activation=tf.keras.activations.linear))
model.compile(loss=tf.keras.losses.mean_squared_error, optimizer=tf.keras.optimizers.SGD(0.001), metrics=[tf.keras.losses.mean_squared_error])
model.fit(x, y, batch_size=1, epochs=3)
xtest = np.linspace(-1, 1, 100).reshape(-1, 1)
ytest = model.predict(xtest)

plt.scatter(x, y)
plt.plot(xtest, ytest)
plt.show()

当绘制预期图和预测时,它会导致 This Plot 其中点是预期函数,实线是预测 我不确定我做错了什么。

网络必须是一个由 100 个 sigmoid 激活神经元组成的层,然后是一个线性输出层,即使我改变了 epoch 的数量和批量大小,网络仍然可以线性训练。任何帮助将不胜感激

【问题讨论】:

  • 您能否展示您的工作 Matlab 解决方案以进行比较?

标签: python tensorflow non-linear-regression


【解决方案1】:

您可以尝试更改激活并增加训练时间,我将优化器更改为 Adam,将训练 epoch 增加到 30 并得到这个结果。

代码:

import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
import matplotlib.pyplot as plt

f1 = lambda x: ((x < .5) * np.power(x, 2) + (x > .5) * x) * 2 -1
x = np.linspace(-1, 1, 180).reshape(-1,1)
y = f1(x).reshape(-1, 1)
model = keras.models.Sequential()
model.add(layers.Dense(100, activation=tf.keras.activations.sigmoid, input_shape=(1,)))
model.add(layers.Dense(1, activation=tf.keras.activations.linear))
model.compile(loss=tf.keras.losses.mean_squared_error, 
              optimizer=tf.keras.optimizers.Adam(0.01), 
              metrics=[tf.keras.losses.mean_squared_error])
model.fit(x, y, batch_size=1, epochs=30)
xtest = np.linspace(-1, 1, 100).reshape(-1, 1)
ytest = model.predict(xtest)

plt.scatter(x, y)
plt.plot(xtest, ytest)
plt.show()

【讨论】:

  • 我需要使用 sigmoid,relu 在训练后无法满足我对模型的需要。我知道在 matlab 中使用 sigmoid 和使用 3 个 epoch(l-m 反向传播)进行训练,我只是对为什么模型保持线性感到困惑。
  • 如果我理解正确,与我发现 here 的 tensorflow 中的反向传播相比,l-m 反向传播更新权重不同。我发现使用带有 sigmoid 的 adam 效果很好。我正在更新答案中的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 2018-01-06
  • 2019-04-12
  • 1970-01-01
  • 2019-12-31
相关资源
最近更新 更多