张量流的非线性回归

时间:2016-09-01 23:00:03

标签: python machine-learning neural-network tensorflow non-linear-regression

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

1 个答案:

答案 0 :(得分:1)

经常使用的COST只是平方差异:

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,它实际上归结为几乎为零的权重,但由于各种原因不完全为零。 对于激活,您可以使用tanh,sigmoid或ReLU或其他任何东西。