问题:
ValueError:无法将字符串转换为float:
我已经在这里呆了几天,有人告诉我Stack Overflow解决了我的问题。这是我第一次问问题,因此请原谅任何可能的错误。
该代码旨在查找Jupyter下15个输入和1个输出和运行之间的关系。使用“ 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)。
答案 0 :(得分:1)
当输入空字符串('')时,将显示错误。
excel有几个空单元格,因此某些值是空的,不能转换为float。送入空字符串('')时,会出现错误。