简体   繁体   English

Tensorflow多层感知器图不会收敛

[英]Tensorflow Multi-layer perceptron graph won't converge

I am new to python and tensorflow. 我是python和tensorflow的新手。 After better(maybe) understanding DNN and its math. 更好(也许)了解DNN及其数学之后。 I start to learn to use tensorflow by exercises. 我开始学习通过练习使用张量流。

One of my exercises is to predict x^2. 我的一项练习是预测x ^ 2。 Which means after fine training. 这意味着经过良好的训练。 When I give 5.0, it will predict 25.0. 当我给出5.0时,它将预测为25.0。

Parameters and settings: 参数和设置:

Cost function = E((y-y')^2) 成本函数= E((y-y')^ 2)

Two hidden Layers and they are fully connected. 两个隐藏层,它们完全连接。

learning_rate = 0.001 learning_rate = 0.001

n_hidden_1 = 3 n_hidden_​​1 = 3

n_hidden_2 = 2 n_hidden_​​2 = 2

n_input = 1 n_input = 1

n_output = 1 n_output = 1

def multilayer_perceptron(x, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)
    # Hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)
    # Output layer with linear activation
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

def generate_input():
    import random

    val = random.uniform(-10000, 10000)
    return np.array([val]).reshape(1, -1), np.array([val*val]).reshape(1, -1)


# tf Graph input
# given one value and output one value
x = tf.placeholder("float", [None, 1])
y = tf.placeholder("float", [None, 1])
pred = multilayer_perceptron(x, weights, biases)

# Define loss and optimizer
distance = tf.sub(pred, y)
cost = tf.reduce_mean(tf.pow(distance, 2))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

init = tf.initialize_all_variables()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    avg_cost = 0.0

    for iter in range(10000):
        inp, ans = generate_input()
        _, c = sess.run([optimizer, cost], feed_dict={x: inp, y: ans})
        print('iter: '+str(iter)+' cost='+str(c))

However, it turns out that c sometimes gets larger,and sometimes lower. 然而,事实证明,c有时会变大,有时会变低。 (but it is big) (但是很大)

It seems that your training data have big range because of the statement val = random.uniform(-10000, 10000) , try to do some data preprocessing before you train. 由于语句val = random.uniform(-10000, 10000) ,看来您的训练数据范围很大,请在训练之前尝试进行一些数据预处理。 for example, 例如,

val = random.uniform(-10000, 10000)
val = np.asarray(val).reshape(1, -1)
val -= np.mean(val, axis=0)
val /= np.std(val, axis=0)

As for loss value, it's ok that sometimes it gets larger,and sometimes lower, just make sure the loss is decreasing when training epoch increase in general. 至于损失值,可以确定它有时会变大,有时会变低,只要确保训练时长一般增加,损失就减少了。 PS: we are often using SGD optimizer. PS:我们经常使用SGD优化器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM