Python TensorFlow:从CSV文件读取和使用数据
我试过Tensorflow提供的代码 我也尝试过Nicolas提供的解决方案,我遇到了一个错误: ValueError:形状()的秩必须至少为1 但是我无法操纵代码,这样我就可以获取数据并将其放入Python TensorFlow:从CSV文件读取和使用数据,python,csv,tensorflow,neural-network,linear-regression,Python,Csv,Tensorflow,Neural Network,Linear Regression,我试过Tensorflow提供的代码 我也尝试过Nicolas提供的解决方案,我遇到了一个错误: ValueError:形状()的秩必须至少为1 但是我无法操纵代码,这样我就可以获取数据并将其放入train_X和train_Y变量中 我目前正在为变量train\u X和train\u Y使用硬编码数据 我的csv文件包含两列,高度和充电状态(SoC),其中高度是一个浮点值,SoC是一个整数(Int),从0开始,增量为10,最大为100 我想从列中获取数据,并将其用于线性回归模型,其中高度是Y值,
train_X
和train_Y
变量中
我目前正在为变量train\u X
和train\u Y
使用硬编码数据
我的csv文件包含两列,高度和充电状态(SoC),其中高度是一个浮点值,SoC是一个整数(Int),从0开始,增量为10,最大为100
我想从列中获取数据,并将其用于线性回归模型,其中高度是Y值,SoC是x值
这是我的密码:
filename_queue = tf.train.string_input_producer("battdata.csv")
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1], [1]]
col1, col2= tf.decode_csv(
value, record_defaults=record_defaults)
features = tf.stack([col1, col2])
with tf.Session() as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1200):
# Retrieve a single instance:
example, label = sess.run([features, col2])
coord.request_stop()
coord.join(threads)
我想更改此模型中csv数据的使用:
# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50
# Training Data
train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = numpy.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]
# tf Graph Input
X = tf.placeholder("float")#Charge
Y = tf.placeholder("float")#Height
# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")
# Construct a linear model
pred = tf.add(tf.multiply(X, W), b) # XW + b <- y = mx + b where W is gradient, b is intercept
# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initializing the variables
init = tf.global_variables_initializer()
# Launch the graph
with tf.Session() as sess:
sess.run(init)
# Fit all training data
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={X: x, Y: y})
#Display logs per epoch step
if (epoch+1) % display_step == 0:
c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
print( "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
"W=", sess.run(W), "b=", sess.run(b))
print("Optimization Finished!")
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
#Graphic display
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
plt.legend()
plt.show()
你所需要做的就是用你从
decode\u csv
方法得到的op替换你的占位符
张量。这样,无论何时运行Optimizer
,TensorFlow图都会要求通过各种Tensor依赖项从文件中读取新行:
optimizer
=>成本
=>pred
=>X
成本
=>Y
它会给出这样的结果:
filename_queue = tf.train.string_input_producer("battdata.csv")
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1.], [1]]
X, Y = tf.decode_csv(
value, record_defaults=record_defaults)
# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")
# Construct a linear model
pred = tf.add(tf.multiply(X, W), b) # XW + b <- y = mx + b where W is gradient, b is intercept
# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initializing the variables
init = tf.global_variables_initializer()
with tf.Session() as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# Fit all training data
for epoch in range(training_epochs):
_, cost_value = sess.run([optimizer, cost])
[...] # The rest of your code
coord.request_stop()
coord.join(threads)
filename\u queue=tf.train.string\u input\u producer(“battdata.csv”)
reader=tf.TextLineReader()
key,value=reader.read(文件名\队列)
#如果列为空,则为默认值。还指定了
#解码结果。
记录_默认值=[[1.],[1]]
十、 Y=tf.decode\u csv(
值,记录默认值=记录默认值)
#设置模型权重
W=tf.Variable(rng.randn(),name=“weight”)
b=tf.Variable(rng.randn(),name=“bias”)
#构建一个线性模型
pred=tf.add(tf.multiply(X,W),b)#XW+b我遇到了同样的问题,问题的解决方式如下:
tf.train.string_input_producer(tf.train.match_filenames_once("medal.csv"))
在这里找到这个:
tf.train.string_input_producer(tf.train.match_filenames_once("medal.csv"))