Tensorflow:TypeError:float()参数必须是字符串或数字,而不是' generator'

时间:2017-12-13 07:25:57

标签: python machine-learning tensorflow

我只是使用Tensorflow的ML学习者的初学者,在教程中我想解决" Iris"不使用批量解决方案来判断程序。 这是我的代码:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets

sess = tf.Session()

iris = datasets.load_iris()
binary_target = np.array([1. if x==0 else 0. for x in iris.target])
iris_2d = np.array([[x[2], x[3]] for x in iris.data])

x1_data = tf.placeholder(dtype=tf.float32, shape=[1])
x2_data = tf.placeholder(dtype=tf.float32, shape=[1])
y_ = tf.placeholder(dtype=tf.float32, shape=[1])
w1 = tf.Variable(tf.random_normal(shape=[1]))
w2 = tf.Variable(tf.random_normal(shape=[1]))
b  = tf.Variable(tf.random_normal(shape=[1]))

x1w1 = tf.multiply(x1_data, w1)
x2w2 = tf.multiply(x2_data, w2)
xsum = tf.add(x1w1, x2w2)
output = tf.add(xsum, b)
loss   = tf.square(tf.subtract(y_, output))

my_opt = tf.train.GradientDescentOptimizer(0.02)
train_step = my_opt.minimize(loss)

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

for i in range(1000):
    rand_index = np.random.choice(len(iris_2d),1)
    rand_x  = iris_2d[rand_index]
    rand_x1 = np.array([[x][0][0] for x in rand_x])
    rand_x2 = np.array([[x][0][1] for x in rand_x])
    rand_y  = np.array([[y]] for y in binary_target[rand_index])
    sess.run(train_step, feed_dict={x1_data: rand_x1, x2_data: rand_x2, y_: rand_y})
    if (i+1)%100 == 0:
        print('Step #' + str(i+1) + ' w1 = ' + str(sess.run(w1)) + ' w2 = ' + str(sess.run(w2)) + \
              ' b = ' + str(sess.run(b)))

我总是收到这条消息:

"C:\Users\Naoki Nakamura\PycharmProjects\tensorflow\venv\Scripts\python.exe" "C:/Users/Naoki Nakamura/PycharmProjects/tensorflow/venv/naoki/iris.py"
2017-12-13 16:07:00.955740: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Traceback (most recent call last):
  File "C:/Users/Naoki Nakamura/PycharmProjects/tensorflow/venv/naoki/iris.py", line 37, in <module>
    sess.run(train_step, feed_dict={x1_data: rand_x1, x2_data: rand_x2, y_: rand_y})
  File "C:\Users\Naoki Nakamura\PycharmProjects\tensorflow\venv\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
    run_metadata_ptr)
  File "C:\Users\Naoki Nakamura\PycharmProjects\tensorflow\venv\lib\site-packages\tensorflow\python\client\session.py", line 1089, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "C:\Users\Naoki Nakamura\PycharmProjects\tensorflow\venv\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number, not 'generator'

Process finished with exit code 1

我觉得我的代码错了,但我无法弄清楚错误是什么...... 任何帮助都非常感谢。 (我很抱歉我的英语和Python能力差。)

1 个答案:

答案 0 :(得分:0)

在rand_y without square brackets you've got generator

将您的rand_y更改为:

#linknav:active {
    color:white;
} 

输出:

rand_y  = np.array([y for y in binary_target[rand_index]])