【问题标题】:Training a neural network to learn polynomial equation训练神经网络来学习多项式方程
【发布时间】:2022-01-09 03:47:30
【问题描述】:

我已经创建了一个 y ~ x**2 的数据集

但是,当我训练神经网络时,它无法拟合二次方程。

这是我的模型。

model2 = tf.keras.models.Sequential(
    [tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
    tf.keras.layers.Dense(1)]
)

loss = tf.keras.losses.mse
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)

model2.compile(optimizer=optimizer, loss=loss, metrics=tf.metrics.RootMeanSquaredError())
model2.fit(tf.expand_dims(X_train, -1), y_train, epochs=1000, verbose=1)

我对上述模型的思考过程是,我认为每个relu激活都会拟合一条局部线性线,并慢慢连接所有神经元形成一条二次线。

最后,我通过在输出层使用lambda x:x**2 的激活来进行拟合,但是,那是因为我知道该函数是 x**2。

所以我的问题是,在不知道真实函数的情况下,如何训练神经网络以拟合非线性曲线?

【问题讨论】:

  • 您的 NN 无法拟合二次函数并没有任何理论上的原因。具有非线性激活函数(如 ReLU)的神经网络可以逼近任何函数,参见Universal approximation theorem

标签: python tensorflow keras neural-network


【解决方案1】:

你的代码对我来说很好。

注意,我使用较大的学习率和提前停止(总共 2000 个 epoch 有 300 个耐心)。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

train_x = np.linspace(0, 80, 160)
train_y = train_x**2

test_x = np.linspace(80, 100, 40)
test_y = test_x**2


model2 = tf.keras.models.Sequential(
    [tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(100, activation='relu'),
     tf.keras.layers.Dense(1)]
)

loss = tf.keras.losses.mse
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-2)

early_stop = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=300, restore_best_weights=True)

model2.compile(optimizer=optimizer, loss=loss, metrics=tf.metrics.RootMeanSquaredError())
model2.fit(tf.expand_dims(train_x, -1), train_y, epochs=2000, verbose=1, callbacks=[early_stop])

train_pred = model2.predict(train_x)
test_pred = model2.predict(test_x)

plt.scatter(train_x, train_y, c='blue', label='train x')
plt.scatter(test_x, test_y, c='green', label='test x')
plt.scatter(train_x, train_pred, c='red', label='train pred')
plt.scatter(test_x, test_pred, c='orange', label='test pred')
plt.legend()
plt.show()

Training and test results photo here

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 2020-11-24
    • 1970-01-01
    • 2017-02-25
    • 2015-06-11
    • 2014-06-23
    • 2011-04-07
    • 1970-01-01
    相关资源
    最近更新 更多