Tensorflow 用神经网络逼近对数函数
我试图用一个神经网络在一个域上近似一个从1到100的对数函数。我使用Tensorflow 用神经网络逼近对数函数,tensorflow,neural-network,deep-learning,logarithm,approximation,Tensorflow,Neural Network,Deep Learning,Logarithm,Approximation,我试图用一个神经网络在一个域上近似一个从1到100的对数函数。我使用tensorflow作为软件。结果不如我预期的好,我想了解原因。我使用以下代码: import tensorflow as tf import numpy as np import matplotlib.pyplot as plt ## == data to be approximated == ## x_grid = np.array([np.linspace(1, 100, 100)]).T y_grid = np.log
tensorflow
作为软件。结果不如我预期的好,我想了解原因。我使用以下代码:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
## == data to be approximated == ##
x_grid = np.array([np.linspace(1, 100, 100)]).T
y_grid = np.log(x_grid)
def deepnn(x_val, prior):
"""
A neural network with input values x. Its parameters might be constraint according to a prior.
"""
## == input layer == ##
if prior:
w_in = tf.constant(1., shape=[1, 2]) #fixed to one
b_in = tf.constant([-1., -20.]) # fixed along kinks of the log function
else:
w_in = weight_variable([1, 2])
b_in = bias_variable([2])
f_in = tf.matmul(x_val, w_in) + b_in
## == first hidden layer == ##
g_1 = tf.nn.relu(f_in)
## == output layer == ##
w_out = weight_variable([2, 1])
b_out = bias_variable([1])
y_predict = tf.matmul(g_1, w_out) + b_out
return y_predict
def weight_variable(shape):
"""
generate a weight variable of a given shape
"""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
"""
generates a bias variable of a given shape
"""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
x_given = tf.placeholder(tf.float32, [None, 1])
y_out = deepnn(x_given, False)
y = tf.placeholder(tf.float32, [None, 1])
squared_deltas = tf.square(y_out - y)
loss = tf.reduce_sum(squared_deltas)
optimizer = tf.train.AdamOptimizer(1e-3)
train = optimizer.minimize(loss)
sess = tf.InteractiveSession()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(50000):
sess.run(train, {x_given: x_grid, y: y_grid})
print(sess.run(loss, {x_given: x_grid, y: y_grid}))
sess.close()
神经网络deepnn(x_val,prior)
可以有两种形式:如果prior
为真,则输入层函数tf.matmul(x_val,w_in)+b_in
的参数设置为w_in=1
和b_in=[-1,-20]
。这些b_in
的值将迫使网络在x=20处扭结。如果prior
为false,则参数值初始化为w
和b=0.1
的随机变量。(这些值以及计算机代码的灵感来源于a。)输入通过整流器激活功能和输出层传递到隐藏层。网络是否应遵循优先级在y\u out=deepnn(x\u给定,False)
行中定义
与具有先验约束的网络相比,没有先验约束的神经网络(几乎总是)产生较差的结果。网络简单地类似于一个线性函数。奇怪的是,这个不受限制的网络曾经产生了一个非常好的解决方案,但在随后的试验中我无法复制。下图显示了结果
有人能解释一下为什么我不能很好地训练网络吗 我没有彻底检查您的代码,但您似乎没有使用任何非线性网络。你的网络很浅(只有一个隐藏层),所以要深入(正如你在函数中提到的)你需要更多的层。另外,我认为层中需要更多节点。至少尝试2个隐藏层
顺便说一句,有一个函数完全按照所说的做:谢谢您的建议!你可能知道为什么我使用的网络没有很强的先验假设,却不能很好地训练吗?神经网络的估计参数并不能使损失函数最小化。