【问题标题】:TensorFlow regression with error :"could not convert string to float: "TensorFlow 回归错误:“无法将字符串转换为浮点数:”
【发布时间】:2018-09-13 20:06:56
【问题描述】:

问题:

ValueError:无法将字符串转换为浮点数:

我已经在这里呆了几天了,有人告诉我 Stack Overflow 可以解决我的问题。这是我第一次提问,如有错误请见谅。

该代码旨在查找 15 个输入和 1 个输出之间的关系,并在 Jupyter 下运行。使用“xlrd”从“data.xls”中提取数据并存储到列表中。我计划通过计算均方误差来表示损失。

谢谢!

import xlrd
import numpy
import tensorflow as tf

book=xlrd.open_workbook('data.xls')
sheet0=book.sheet_by_index(0)
sheet_name=book.sheet_names()[0]

rows_number=sheet0.nrows

X=[]
for i in range(rows_number-1):
    temp=sheet0.row_values(i+1)
    del temp[0:4]
    X.append(temp)

Y=[]
for i in range(rows_number-1):
    temp=sheet0.row_values(i+1)
    Y.append([temp[3]])


w1= tf.Variable(tf.random_normal([15, 10],name='matrix1', stddev=1))
b1 = tf.Variable(tf.constant(0.1, shape=[10]))
w2= tf.Variable(tf.random_normal([10, 10],name='matrix2', stddev=1))
b2 = tf.Variable(tf.constant(0.1, shape=[10]))
w3= tf.Variable(tf.random_normal([10, 1],name='matrix3', stddev=1))

x = tf.placeholder(tf.float32, shape=(None, 15), name="x-input")
y_= tf.placeholder(tf.float32, shape=(None, 1), name='y-input')

a1= tf.add(tf.matmul(x, w1),b1)
a2=tf.add(tf.matmul(tf.nn.sigmoid(a1),w2),b2)
y=tf.matmul(tf.nn.sigmoid(a2),w3)
y=tf.nn.sigmoid(y)

loss = tf.losses.mean_squared_error(y_, y)
train=tf.train.AdamOptimizer(0.1).minimize(loss)

with tf.Session() as sess:

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


    STEPS = 30000
    for i in range(STEPS):
        sess.run(train, feed_dict={x: X, y_: Y})

ValueError                                Traceback (most recent call last)
<ipython-input-22-de3ef36f5080> in <module>()
      7     STEPS = 30000
      8     for i in range(STEPS):
----> 9         sess.run(train, feed_dict={x: X, y_: Y})
     10 
     11 

~\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    898     try:
    899       result = self._run(None, fetches, feed_dict, options_ptr,
--> 900                          run_metadata_ptr)
    901       if run_metadata:
    902         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1102             feed_handles[subfeed_t] = subfeed_val
   1103           else:
-> 1104             np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
   1105 
   1106           if (not is_tensor_handle_feed and

~\Anaconda3\envs\ML\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    490 
    491     """
--> 492     return array(a, dtype, copy=False, order=order)
    493 
    494 

ValueError: could not convert string to float: 

我检查了两个列表 X 和 Y 的元素的数据类型。形状是 X 的 (835,15),Y 的形状是 (835,1)。

这里是X和Y的内容 X-inputY-input

【问题讨论】:

  • 这个错误意味着XY的值不是数字。它们是字符串。尝试将它们转换为数字。
  • 非常感谢。但是如果我打印列表,每个元素都没有引号。此外, map(eval,X) 似乎无法解决它。我还发现它可以正常运行,它将原始代码替换为 sess.run(train, feed_dict={x: [[0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0]], y_: [[0]]})
  • 最后上传了截图
  • 哇,你是对的。我浏览了这些数据,有一些空单元格。我删除了空行,然后没有错误。谢谢! O(∩_∩)O.
  • 请将答案作为答案发布,而不是作为问题的更新。这是为了避免对未来的访问者造成混淆。见How to Answer

标签: python tensorflow non-linear-regression


【解决方案1】:

当输入空字符串('')时,会报错。


excel有几个空单元格,所以有些值是空的,不能转换成float。当输入空字符串('')时,它会给出错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2019-08-31
    • 2019-08-15
    • 2018-10-28
    • 1970-01-01
    相关资源
    最近更新 更多