用张量流求解函数方程

时间:2018-09-02 09:26:51

标签: python tensorflow

让我们考虑函数方程

enter image description here

在数学上,我们知道如何以封闭形式求解该方程式,但是如果我们以具有relu激活功能的完全连接的1层或2层神经网络的形式寻找近似解f怎么办?

在Tensorflow中进行梯度下降的最佳方法是什么

enter image description here

对于在[-10,10]中随机绘制的x's的迷你批处理? 我的问题来自这样一个事实,即方程中同时存在f(x)f(x+1),这与经典的监督学习不同。

1 个答案:

答案 0 :(得分:2)

一种方法是仅通过x+1在网络中运行。也就是说,对于两层网络,您可以拥有一个如下所示的模型:

num_units_layer_1 = 200
num_units_layer_2 = 200

x = tf.placeholder(tf.float32, [None, 1])

seed = 42

weights = {
    'hidden1': tf.Variable(tf.random_normal([1, num_units_layer_1], seed=seed)),
    'hidden2': tf.Variable(tf.random_normal([num_units_layer_1, num_units_layer_2], seed=seed)),
    'output': tf.Variable(tf.random_normal([num_units_layer_2, 1], seed=seed))
}

biases = {
    'hidden1': tf.Variable(tf.random_normal([num_units_layer_1], seed=seed)),
    'hidden2': tf.Variable(tf.random_normal([num_units_layer_2], seed=seed)),
    'output': tf.Variable(tf.random_normal([1], seed=seed))
}

def model_f(x):
    hidden_layer_1 = tf.add(tf.matmul(x, weights['hidden1']), biases['hidden1'])
    hidden_layer_1 = tf.nn.relu(hidden_layer_1)
    hidden_layer_2 = tf.add(tf.matmul(hidden_layer_1, weights['hidden2']), biases['hidden2'])
    hidden_layer_2 = tf.nn.relu(hidden_layer_2)
    return tf.matmul(hidden_layer_2, weights['output']) + biases['output']

output_layer = model_f(x)
output_layerp = model_f(x+1)

in_range = tf.logical_and(x >= 0, x <= 1)
target_x = tf.where(in_range, output_layer, x)

cost = tf.reduce_mean((output_layerp - output_layer - x**2)**2) + tf.reduce_mean((target_x - x)**2)

optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)

init = tf.initialize_all_variables()

然后,在估算参数时,您可以随需生成简单的批处理:

with tf.Session() as sess:
    sess.run(init)

    # Estimate
    for epoch in range(5000):
        sample = np.random.uniform(-10, 10, (400, 1))
        _, c = sess.run([optimizer, cost], feed_dict = {x: sample})
        if epoch % 1000 == 999:
            print(f'Epoch {epoch}, cost: {c}')

    # Make predictions and plot result
    xs = np.linspace(-10, 10, 500).reshape(500, 1)
    predictions = sess.run(output_layer, feed_dict={x: xs})
    plt.plot(xs, predictions)

这将产生以下输出:

enter image description here

我们可以通过简单地使用函数方程递归定义 f 来将其与您获得的结果进行比较:

def f(x):
    if x >= 0 and x <= 1:
        return x
    if x > 1:
        return f(x-1) + (x-1)**2
    if x < 0:
        return f(x+1) - x**2

plt.plot(xs, [f(x[0]) for x in xs])
plt.plot(xs, predictions)

enter image description here

相当多的地方。但是,它不能推广到其他范围:

enter image description here