【问题标题】:Tensorflow: Simple Linear Regression using CSV dataTensorflow:使用 CSV 数据的简单线性回归
【发布时间】:2017-12-01 07:56:57
【问题描述】:

我是 tensorflow 的极端初学者,我的任务是使用包含 2 列高度和充电状态 (SoC) 的 csv 数据进行简单的线性回归,其中两个值都是浮动的。 在 CSV 文件中,Height 是第一列,而 SoC 是第二列。

我假设使用高度来预测 SoC

我完全不知道我必须在代码的“适合所有训练数据”部分添加什么。我看过其他线性回归模型,它们的代码令人难以置信,比如这个:

with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
    sess.run(training_step,feed_dict={X:train_x,Y:train_y})
    cost_history = np.append(cost_history,sess.run(cost,feed_dict={X: train_x,Y: train_y}))

#calculate mean square error 
pred_y = sess.run(y_, feed_dict={X: test_x})
mse = tf.reduce_mean(tf.square(pred_y - test_y))
print("MSE: %.4f" % sess.run(mse)) 

#plot cost
plt.plot(range(len(cost_history)),cost_history)
plt.axis([0,training_epochs,0,np.max(cost_history)])
plt.show()

fig, ax = plt.subplots()
ax.scatter(test_y, pred_y)
ax.plot([test_y.min(), test_y.max()], [test_y.min(), test_y.max()], 'k--', lw=3)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()

我刚刚能够使用本指南从我的 CSV 文件中正确获取数据:

TensorFlow: Reading and using data from CSV file

完整代码:

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
rng = np.random

from numpy import genfromtxt
from sklearn.datasets import load_boston

# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50
n_samples = 221

X = tf.placeholder("float") # create symbolic variables
Y = tf.placeholder("float")

filename_queue = tf.train.string_input_producer(["battdata.csv"],shuffle=False)

reader = tf.TextLineReader(skip_header_lines=1)
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])

# 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(col1, W), b) # XW + b <- y = mx + b  where W is gradient, b is intercept

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-col2, 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)
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        _, cost_value = sess.run([optimizer,cost])
        for (x, y) in zip(col2, col1):
                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: col2, Y:col1})
            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: col2, Y: col1})
        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) * col2 + sess.run(b), label='Fitted line')
        plt.legend()
        plt.show()

    coord.request_stop()
    coord.join(threads)

错误:

INFO:tensorflow:Error 报告给 Coordinator: ,尝试使用已关闭的 Session。 -------------------------------------------------- ------------------------- TypeError Traceback(最近一次调用 最后)在() 8 用于范围内的纪元(training_epochs): 9 _, cost_value = sess.run([优化器,成本]) ---> 10 for (x, y) in zip(*col1, col2): 11 sess.run(优化器,feed_dict={X: x, Y: y}) 12

C:\Users\Shiina\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py 在 iter(自我) 514 类型错误:调用时。 第515章 --> 516 raise TypeError("'Tensor' object is not iterable.") 517 518 def bool(自我):

TypeError: 'Tensor' 对象不可迭代。

【问题讨论】:

    标签: python python-3.x csv tensorflow linear-regression


    【解决方案1】:

    错误是因为您试图迭代 for (x, y) in zip(col2, col1) 中的张量,这是不允许的。代码的另一个问题是您设置了输入管道队列,然后您还尝试通过 feed_dict{} 输入,这是错误的。您的训练部分应如下所示:

    with tf.Session() as sess:
    # Start populating the filename queue.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    sess.run(init)
    
    # Fit all training data
    for epoch in range(training_epochs):
        _, cost_value = sess.run([optimizer,cost])
    
            #Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost)
            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)
        print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
    
    #Plot data after completing training
    train_X = []
    train_Y = []
    for i in range(input_size): #Your input data size to loop through once
        X, Y = sess.run([col1, pred]) # Call pred, to get the prediction with the updated weights
        train_X.append(X)
        train_Y.append(y)
        #Graphic display
    plt.plot(train_X, train_Y, 'ro', label='Original data')
    plt.legend()
    plt.show()
    
    coord.request_stop()
    coord.join(threads)
    

    【讨论】:

    • 嗨!感谢您的回答。但是使用您的代码,我看到的图形的 x 轴是从值 0-1.0 而我正在使用的 csv 数据,X 轴应该是 0-100.0 :x 我做错了什么吗?
    • 检查得到col1值的train_X的值,看它是否和你的csv数据一样。
    • 我检查了train_X的值,它与我的csv文件中的数据不对应。
    • 我刚刚检查了一个随机生成的输入,它工作正常。您看到的数据应该来自某个地方。可以分享一下csv文件吗?
    • 这里是代码drive.google.com/open?id=0B8Kt9KpV9HnRT0xWdVdJLWJFdWc的csv文件和ipy文件的下载链接
    猜你喜欢
    • 2019-01-19
    • 2013-03-15
    • 1970-01-01
    • 2018-07-23
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多