我已尝试过Tensorflow提供的代码doc
我也尝试过Nicolas提供的解决方案,我遇到了一个错误:
ValueError:Shape()的等级必须至少为1
但我无法操纵代码,以便我可以抓取数据并将其放在train_X
和train_Y
变量中。
我目前正在使用变量train_X
和train_Y
的硬编码数据。
我的csv文件包含2列,Height&充电状态(SoC),其中height是浮点值,SoC是从0开始的整数(Int),增量为10,最大值为100。
我想从列中获取数据并在线性回归模型中使用它,其中Height是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()
编辑:
我也尝试过Nicolas提供的解决方案,我遇到了一个 错误:
ValueError:Shape()的等级必须至少为1
我通过在文件名周围添加方括号来解决这个问题,如下所示:
filename_queue = tf.train.string_input_producer(['battdata.csv'])
答案 0 :(得分:1)
您需要做的就是通过placeholder
方法获得的操作来替换decode_csv
张量。这样,无论何时运行optimiser
,TensorFlow图都会要求通过各种Tensor依赖项从文件中读取新行:
optimiser
=&gt;
cost
=&GT; pred
=&GT; X
cost
=&gt; 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)
答案 1 :(得分:0)
我遇到了同样的问题,问题解决了:
tf.train.string_input_producer(tf.train.match_filenames_once("medal.csv"))