创建AdamOptimizer时的Tensorflow签名问题

时间:2018-12-24 17:16:15

标签: python tensorflow

这是应用签名的简短图形,其中出现了一个错误,使我感到困惑。

tf.version = 1.12

import tensorflow as tf
from tensorflow.python import autograph


@autograph.convert()
def prepare_for_cost(ys, ps, limit=tf.constant(.9)):
    l = len(ys)
    nys = []
    nps = []
    for i in range(l):
        y = ys[i]
        p = ps[i]
        if p[1] > limit:
            nys.append(y)
            nps.append(p)
        else:
            nys.append([0.0, 1.0])
            nps.append([1.0, 0.0])
    nys_list = autograph.stack(nys, tf.float32)
    nps_list = autograph.stack(nps, tf.float32)
    return nys_list, nps_list


with tf.Graph().as_default() as g, tf.Session(graph=g) as sess:
    xs = tf.placeholder(tf.float32, [None, 2])
    w = tf.Variable(tf.random_normal([2, 2]))
    ps = tf.matmul(xs, w)
    ys = tf.placeholder(tf.float32, [None, 2])

    nys, nps = prepare_for_cost(ys, ps)
    nys.set_shape(tf.TensorShape((None, 2)))
    nps.set_shape(tf.TensorShape((None, 2)))

    cost = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(labels=nys, logits=nps))
    train_op = tf.train.AdamOptimizer(0.01).minimize(cost)
    sess.run(tf.global_variables_initializer())
    sess.run([train_op], feed_dict={ys: [[0.0, 1.0], [1.0, 0.0], [0.0, 1.0]], xs: [[.2, .8], [.7, .3], [.1, .92]]})

下面的错误堆栈,有人可以找出解决方法吗?

autograph似乎仍在构建中,例如,应在此处使用autograph.tensor_list代替autograph.stack,但是有一个小错误阻止了我这样做。至于下面的错误,我不知道这是另一个错误还是滥用。

File "D:/project/myshpy/test/myshtest/common/CommonPy.py", line 36, in <module>
        train_op = tf.train.AdamOptimizer(0.01).minimize(cost)
      File "D:\soft\python36\lib\site-packages\tensorflow\python\training\optimizer.py", line 400, in minimize
        grad_loss=grad_loss)
      File "D:\soft\python36\lib\site-packages\tensorflow\python\training\optimizer.py", line 519, in compute_gradients
        colocate_gradients_with_ops=colocate_gradients_with_ops)
      File "D:\soft\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 630, in gradients
        gate_gradients, aggregation_method, stop_gradients)
      File "D:\soft\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 858, in _GradientsHelper
        loop_state.PostProcessing()
      File "D:\soft\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 1404, in PostProcessing
        grad_val = constant_op.constant(0, dtype=dtype, shape=shape)
      File "D:\soft\python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 208, in constant
        value, dtype=dtype, shape=shape, verify_shape=verify_shape))
      File "D:\soft\python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 477, in make_tensor_proto
        (dtype, nparray.dtype, values))
    TypeError: Incompatible types: <dtype: 'variant'> vs. int32. Value is 0

0 个答案:

没有答案