24小时热门版块排行榜    

查看: 764  |  回复: 1

qcl龍

铜虫 (初入文坛)

[求助] tensorflow神经网络问题请教 已有1人参与

请教下,我这段tensorflow代码误差一直收敛不了是为什么

import tensorflow as tf
import numpy as np

def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

# Make up some real data
x_data = np.linspace(0.2, 13, 33)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = 2 * x_data + 2.2 + noise

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediciton and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square( ys -prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# important step
init = tf.global_variables_initializer()
sess= tf.Session()
sess.run(init)

for i in range(10000):
    # training
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        # to see the step improvement
        print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

懒青蛙吐泡泡

铁杆木虫 (正式写手)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
jjdg: 金币+1, 感谢参与 2018-11-04 18:13:27
qcl龍: 金币+10, ★★★很有帮助 2018-11-05 14:58:15
学习率设置太大了,把GradientDescentOptimizer的学习率改成0.001就可以了
2楼2018-11-04 10:56:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 qcl龍 的主题更新
信息提示
请填处理意见