tensorflow
上有哪些激活函数和成本函数?下面可能适合tf.nn学习简单的单变量非线性关系 f(x) = x * x
这是先验未知吗?
当然,这个不切实际的模型仅用于理解tf.nn mechanics 101
.
import numpy as np
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 1])
W = tf.Variable(tf.zeros([1,1]))
b = tf.Variable(tf.zeros([1]))
y = some_nonlinear_activation_function_HERE(tf.matmul(x,W) + b)
y_ = tf.placeholder(tf.float32, [None, 1])
cost = tf.reduce_mean(some_related_cost_function_HERE(y, y_))
learning_rate = 0.001
optimize = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
steps = 1000
for i in range(steps):
sess.run(optimize,
feed_dict={x: np.array([[i]])), y_: np.array([[i*i]])})
print("prediction: %f" % sess.run(y,
feed_dict={x: np.array([[1100]])}))
# expected output near 1210000
最佳答案
经常使用的成本就是平方差:
def squared_error(y1,y2):
return tf.square(y1-y2)
如果您愿意的话,还可以加上 L1 或 L2 惩罚。
但是在我看来,如果你想要一些远程有趣的东西,你就需要在神经网络中添加一个隐藏层。另外,如果你压缩你的输出并且你的目标是平方函数,你可能无法做太多事情。 我会这样做:
x = tf.placeholder(tf.float32, [None, 1])
#Hidden layer with ten neurons
W1 = tf.Variable(tf.zeros([1,10]))
b1 = tf.Variable(tf.zeros([10]))
h1 = some_nonlinear_activation_function(tf.matmul(x,W) + b)
W2 = tf.Variable(tf.zeros([10,1]))
b2 = tf.Variable(tf.zeros([1]))
#I am not squashing the output
y=tf.matmul(h1,W2)+b
cost = tf.reduce_mean(squared_error(y, y_))
此外,我不会使用 0 权重,而是使用更聪明的初始化方案,例如 Xavier 或 He's,这实际上可以归结为从几乎为零的权重开始,但由于各种原因而不是完全为零。 对于激活,您可以使用 tanh、sigmoid 或 ReLU 或其他任何东西。
关于python - tensorflow 的非线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39282060/