【问题标题】:Tensorflow Adagrad optimizer isn't workingTensorflow Adagrad 优化器不工作
【发布时间】:2018-01-20 15:41:06
【问题描述】:

当我运行以下脚本时,我注意到以下几个错误:

import tensorflow as tf
import numpy as np
import seaborn as sns
import random

#set random seed:
random.seed(42)

def potential(N):

    points  = np.random.rand(N,2)*10

    values = np.array([np.exp((points[i][0]-5.0)**2 + (points[i][1]-5.0)**2) for i in range(N)])

    return points, values



def init_weights(shape,var_name):
    """
        Xavier initialisation of neural networks
    """

    init = tf.contrib.layers.xavier_initializer()
    return tf.get_variable(initializer=init,name = var_name,shape=shape)

def neural_net(X):

    with tf.variable_scope("model",reuse=tf.AUTO_REUSE):

        w_h = init_weights([2,10],"w_h")
        w_h2 = init_weights([10,10],"w_h2")
        w_o = init_weights([10,1],"w_o")

        ### bias terms:
        bias_1 = init_weights([10],"bias_1")
        bias_2 = init_weights([10],"bias_2")
        bias_3 = init_weights([1],"bias_3")

        h = tf.nn.relu(tf.add(tf.matmul(X, w_h),bias_1))
        h2 = tf.nn.relu(tf.add(tf.matmul(h, w_h2),bias_2))

    return tf.nn.relu(tf.add(tf.matmul(h2, w_o),bias_3))

X = tf.placeholder(tf.float32, [None, 2])

with tf.Session() as sess:

    model = neural_net(X)

    ## define optimizer:
    opt = tf.train.AdagradOptimizer(0.0001)

    values =tf.placeholder(tf.float32, [None, 1]) 

    squared_loss = tf.reduce_mean(tf.square(model-values))   

    ## define model variables:
    model_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,"model")
    train_model = opt.minimize(squared_loss,var_list=model_vars)

    sess.run(tf.global_variables_initializer())

    for i in range(10):

        points, val = potential(100)

        train_feed = {X : points,values: val.reshape((100,1))}

        sess.run(train_model,feed_dict = train_feed)
        print(sess.run(model,feed_dict = {X:points}))

    ### plot the approximating model:
    res = 0.1
    xy = np.mgrid[0:10:res, 0:10:res].reshape(2,-1).T

    values = sess.run(model, feed_dict={X: xy})    
    sns.heatmap(values.reshape((int(10/res),int(10/res))),xticklabels=False,yticklabels=False)
  1. 在第一次运行时,我得到:

[nan] [nan] [nan] [nan] [nan] [nan] [nan]] Traceback(最 最近通话最后一次):

...

文件 "/Users/aidanrockea/anaconda/lib/python3.6/site-packages/seaborn/matrix.py", 第 485 行,在热图中 yticklabels,掩码)

文件 "/Users/aidanrockea/anaconda/lib/python3.6/site-packages/seaborn/matrix.py", 第 167 行,在 init 中 cmap, 中心, 健壮)

文件 "/Users/aidanrockea/anaconda/lib/python3.6/site-packages/seaborn/matrix.py", 第 206 行,在 _determine_cmap_params vmin = np.percentile(calc_data, 2) if 健壮 else calc_data.min()

文件 “/Users/aidanrockea/anaconda/lib/python3.6/site-packages/numpy/core/_methods.py”, 第 29 行,在 _amin return umr_minimum(a, axis, None, out, keepdims)

ValueError: 零大小数组到归约操作最小值 没有身份

  1. 在第二次运行时,我有:

ValueError: 变量 model/w_h/Adagrad/ 已经存在,不允许。 你的意思是在 VarScope 中设置reuse=True 还是reuse=tf.AUTO_REUSE?

我不清楚为什么会出现这些错误。此外,当我使用:

for i in range(10):

    points, val = potential(10)

    train_feed = {X : points,values: val.reshape((10,1))}
    sess.run(train_model,feed_dict = train_feed)
    print(sess.run(model,feed_dict = {X:points}))

我发现在第一次运行时,我有时会得到一个网络,它已经崩溃到输出为 0 的常量函数。现在我的直觉是,这可能只是一个数字问题,但我可能错了。

如果是这样,这是一个严重的问题,因为我在这里使用的模型非常简单。

【问题讨论】:

  • 添加完整的控制台消息而不仅仅是错误行可能会有所帮助。

标签: python-3.x tensorflow neural-network


【解决方案1】:

现在我的直觉是这可能只是一个数字问题

确实,在运行potential(100) 时,我有时会得到与1E21 一样大的值。最大的点将支配您的损失函数,并将驱动网络参数。

即使在标准化目标值时,例如对于单位方差,最大值主导损失的问题仍然存在(例如,参见plt.hist(np.log(potential(100)[1]), bins = 100))。

如果可以,请尝试学习 val 的日志,而不是 val 本身。但是请注意,您正在将损失函数的假设从“预测遵循围绕目标值的正态分布”更改为“对数预测遵循围绕目标值的对数的正态分布”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多