Python 有人能帮我解决运行这个tensorflow程序时遇到的错误吗
---------------------------错误----------------------------------------Python 有人能帮我解决运行这个tensorflow程序时遇到的错误吗,python,tensorflow,Python,Tensorflow,---------------------------错误---------------------------------------- program for applying linear regression on dataset using tensorflow # About housing_data: # dataset characterisitics:multivariate #Associate task: regression #Number of in
program for applying linear regression on dataset using tensorflow
# About housing_data:
# dataset characterisitics:multivariate
#Associate task: regression
#Number of instance: 506
#number of attribut: 14
#Attribute Information:
#1. CRIM: per capita crime rate by town
#2. ZN: proportion of residential land zoned for lots over 25,000 sq.ft.
#3. INDUS: proportion of non-retail business acres per town
#4. CHAS: Charles River dummy variable
#5. NOX: nitric oxides concentration (parts per 10 million)
#6. RM: average number of rooms per dwelling
#7. AGE: proportion of owner-occupied units built prior to 1940
#8. DIS: weighted distances to five Boston employment centres
#9. RAD: index of accessibility to radial highways
#10. TAX: full-value property-tax rate per $10,000
#11. PTRATIO: pupil-teacher ratio by town
#12. B: 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town
#13. LSTAT: % lower status of the population
#14. MEDV: Median value of owner-occupied homes in $1000's
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
batch_size=50
#make queue of file
filename_queue = tf.train.string_input_producer(
tf.train.match_filenames_once("housing_data.csv"),
shuffle=True)
line_reader = tf.TextLineReader(skip_header_lines=1)
_, csv_row = line_reader.read(filename_queue)
record_defaults = [[0.0], [0.0], [0.0], [0.0], [0.0],[0.0],[0.0],[0.0],[0.0],
[0.0], [0.0], [0.0], [0.0], [0.0]]
CRIM,ZN,INDUS,CHAS,NOX,RM,AGE,DIS,RAD,TAX,PTRATIO,B,LSTAT,MEDV = tf.decode_csv(
csv_row, record_defaults=record_defaults)
# Turn the features back into a tensor.
features = tf.pack([CRIM,ZN,INDUS,CHAS,NOX,RM,AGE,DIS,RAD,TAX,PTRATIO,B,LSTAT,MEDV])
#variable which we need to fill when we are ready to comput the graph
x=tf.placeholder(tf.float32)
y=tf.placeholder(tf.float32)
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
y_pred = tf.add(tf.mul(x, W), b)
error=tf.reduce_sum((y-y_pred)**2/506)
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(error)
#create the session to use the graph
with tf.Session() as sess:
# Here we tell tensorflow that we want to initialize all
# the variables in the graph so we can use them
#tf.initialize_all_variables().run()
sess.run(tf.initialize_all_variables())
sess.run([features,MEDV])
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
sess.run([features,MEDV])
#gradient descent loop for 500 iteration
for _ in range(500):
#select random mini batch
indices=np.random.choice(506,batch_size)
x_batch,y_batch=features[indices],MEDV[indices]
#do gradient descent
_,loss_val=sess.run([optimizer,error],feed_dict={x:x_batch,y:y_batch})
print _,loss_val
coord.request_stop()
coord.join(threads)
当我运行此代码时,我遇到以下错误。
错误:
回溯(最近一次呼叫最后一次):
文件“linear_reg_tf.py”,第92行,在
x_批次,y_批次=特征[索引],MEDV[索引]
文件“/usr/local/lib/python2.7/dist packages/tensorflow/pytho/ops/array_ops.py”,第161行,在
raise TypeError(“类型%s的错误切片索引%s”%(s,类型)))
类型错误:错误的切片索引[468 105 218 124 492 428 464 194 110 76 165 127 480 414 88 332 54 68
195 162 223 65 340 279 390 390 1 127 290 82 430 154 202 389 478 42
410 25 161 42 143 256 481 417 263 92 335 101 430 334]类型可能代替
x\u批次,y\u批次=特征[索引],MEDV[索引]
你可以试试
When I am running this code I am getting following error.
error:
Traceback (most recent call last):
File "linear_reg_tf.py", line 92, in <module>
x_batch,y_batch=features[indices],MEDV[indices]
File "/usr/local/lib/python2.7/dist-packages/tensorflow/pytho/ops/array_ops.py", line 161, in _SliceHelper
raise TypeError("Bad slice index %s of type %s" % (s, type(s)))
TypeError: Bad slice index [468 105 218 124 492 428 464 194 110 76 165 127 480 414 88 332 54 68
可能不是
x\u batch,而是y\u batch=features[index],而是MEDV[index]
你可以试试
When I am running this code I am getting following error.
error:
Traceback (most recent call last):
File "linear_reg_tf.py", line 92, in <module>
x_batch,y_batch=features[indices],MEDV[indices]
File "/usr/local/lib/python2.7/dist-packages/tensorflow/pytho/ops/array_ops.py", line 161, in _SliceHelper
raise TypeError("Bad slice index %s of type %s" % (s, type(s)))
TypeError: Bad slice index [468 105 218 124 492 428 464 194 110 76 165 127 480 414 88 332 54 68
这段代码正在运行,但我想知道我们是否要测试一些新数据,然后我们将如何测试,或者如何将数据拆分为训练集和测试集
x_batch,y_batch = [], []
for index in indices:
x_batch.append(features[index])
y_batch.append(MEDV[index])
#创建会话以使用图表
使用tf.Session()作为sess:
这段代码正在运行,但我想知道我们是否要测试一些新数据,然后我们将如何测试,或者如何将数据拆分为训练集和测试集
x_batch,y_batch = [], []
for index in indices:
x_batch.append(features[index])
y_batch.append(MEDV[index])
#创建会话以使用图表
使用tf.Session()作为sess:
好的,我在代码中做了一些修改,现在它可以正常工作了,我没有批量生成数据集。但是你能给我解释一下创建批处理的概念吗?好的,我在代码中做了一些修改,现在它工作了,我没有对数据集进行批处理。但是你能给我解释一下创建批处理的概念吗。