Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 无法在tensorflow中创建自定义分类器_Python 3.x_Numpy_Tensorflow - Fatal编程技术网

Python 3.x 无法在tensorflow中创建自定义分类器

Python 3.x 无法在tensorflow中创建自定义分类器,python-3.x,numpy,tensorflow,Python 3.x,Numpy,Tensorflow,到目前为止,我所做的是: import tensorflow as tf dists_next_error = tf.placeholder(tf.float32) dists_center_error = tf.placeholder(tf.float32) pts_count = tf.placeholder(tf.float32) ideal_polygon = tf.Variable(0.) cost = tf.square(dists_next_error) \

到目前为止,我所做的是:

import tensorflow as tf

dists_next_error = tf.placeholder(tf.float32)
dists_center_error = tf.placeholder(tf.float32)
pts_count = tf.placeholder(tf.float32)
ideal_polygon = tf.Variable(0.)

cost = tf.square(dists_next_error)      \
        + tf.square(dists_center_error) \
        + tf.square(pts_count - ideal_polygon)

optimizer = tf.train.GradientDescentOptimizer(.05).minimize(cost)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

hund_zeros = tf.zeros([100])
hund_ones = tf.ones([100])

for i in range(1000):
    sess.run(optimizer, feed_dict={
        dists_next_error: hund_zeros,
        dists_center_error: hund_zeros,
        pts_count: hund_ones})

print(cost.eval(feed_dict={
        dists_next_error: 0.,
        dists_center_error: 0.,
        pts_count: 6.}))         #it should output 0 or close to it.

问题在于

sess.run(optimizer, feed_dict={
        dists_next_error: hund_zeros,
        dists_center_error: hund_zeros,
        pts_count: hund_ones})
pts\u count
行中更精确地显示:

TypeError:提要的值不能是tf.Tensor对象。可接受的提要值包括Python标量、字符串、列表或numpy ndarray


但是我在
pts\u count
中没有看到张量,所以我不知道发生了什么。

张量不能用作提要值(您知道这一点);tf.one(),tf.zero创建
张量
。所以

hund_zeros = tf.zeros([100])
hund_ones = tf.ones([100])
它们只是张量

您要做的是发送一些实际的数字(这似乎是您的意图)。您可以使用:

  • numpy:
    np.one([100])
    np.zero([100])
  • 或者使用
    hund\u zero,hund\u one=sess.run([hund\u zero,hund\u one])
  • 使用
    Tensor.eval()方法
请参阅此处的答案以了解更多信息

以下是您提供的代码,已使用运行的
上下文管理器进行了更改:

import tensorflow as tf

dists_next_error = tf.placeholder(tf.float32)
dists_center_error = tf.placeholder(tf.float32)
pts_count = tf.placeholder(tf.float32)
ideal_polygon = tf.Variable(0.)

cost = tf.square(dists_next_error)      \
        + tf.square(dists_center_error) \
        + tf.square(pts_count - ideal_polygon)

optimizer = tf.train.GradientDescentOptimizer(.05).minimize(cost)


hund_zeros = tf.zeros([100])
hund_ones = tf.ones([100])

sess = tf.Session()
init = tf.global_variables_initializer()

with tf.Session()  as sess:
    sess.run(init)

    # run graphs to get some constants
    hund_zeros, hund_ones = sess.run([hund_zeros, hund_ones])

    for i in range(1000):
        sess.run(optimizer, feed_dict={
            dists_next_error: hund_zeros,
            dists_center_error: hund_zeros,
            pts_count: hund_ones})

    print(cost.eval(feed_dict={
            dists_next_error: 0.,
            dists_center_error: 0.,
            pts_count: 6.}))

从这个开始。

张量不能用作馈送值(您知道这一点);tf.one(),tf.zero创建
张量
。所以

hund_zeros = tf.zeros([100])
hund_ones = tf.ones([100])
它们只是张量

您要做的是发送一些实际的数字(这似乎是您的意图)。您可以使用:

  • numpy:
    np.one([100])
    np.zero([100])
  • 或者使用
    hund\u zero,hund\u one=sess.run([hund\u zero,hund\u one])
  • 使用
    Tensor.eval()方法
请参阅此处的答案以了解更多信息

以下是您提供的代码,已使用运行的
上下文管理器进行了更改:

import tensorflow as tf

dists_next_error = tf.placeholder(tf.float32)
dists_center_error = tf.placeholder(tf.float32)
pts_count = tf.placeholder(tf.float32)
ideal_polygon = tf.Variable(0.)

cost = tf.square(dists_next_error)      \
        + tf.square(dists_center_error) \
        + tf.square(pts_count - ideal_polygon)

optimizer = tf.train.GradientDescentOptimizer(.05).minimize(cost)


hund_zeros = tf.zeros([100])
hund_ones = tf.ones([100])

sess = tf.Session()
init = tf.global_variables_initializer()

with tf.Session()  as sess:
    sess.run(init)

    # run graphs to get some constants
    hund_zeros, hund_ones = sess.run([hund_zeros, hund_ones])

    for i in range(1000):
        sess.run(optimizer, feed_dict={
            dists_next_error: hund_zeros,
            dists_center_error: hund_zeros,
            pts_count: hund_ones})

    print(cost.eval(feed_dict={
            dists_next_error: 0.,
            dists_center_error: 0.,
            pts_count: 6.}))
从这个开始