作者:阿尼陀佛1314 | 来源:互联网 | 2023-05-18 12:15
TensorFlow-线性回归(2)flyfishPaddlePaddle的线性回归教程已经做了很详细的代码这里将PaddlePaddle的代码更改为TensorFlow运行的代码
TensorFlow - 线性回归(2)
flyfish
PaddlePaddle的线性回归教程已经做了很详细的代码
这里将PaddlePaddle的代码更改为TensorFlow运行的代码
import tensorflow as tf
import numpy as np
UCI_TRAIN_DATA = None
UCI_TEST_DATA = None
UCI_LABEL_DATA=None
feature_names = [
'CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX',
'PTRATIO', 'B', 'LSTAT', 'convert'
]
def load_data(filename, feature_num=14, ratio=0.8):
data = np.fromfile(filename, sep=' ')
print(data.shape)
print(data.shape[0])
print(data.shape[0] / feature_num)
data = data.reshape( int(data.shape[0] / feature_num), feature_num)
print(data.shape)
maximums, minimums, avgs = data.max(axis=0), data.min(axis=0), data.sum(
axis=0) / data.shape[0]
for i in range(feature_num - 1):
data[:, i] = (data[:, i] - avgs[i]) / (maximums[i] - minimums[i])
offset = int(data.shape[0] * ratio)
global UCI_TRAIN_DATA
global UCI_TEST_DATA
global UCI_LABEL_DATA
UCI_TRAIN_DATA = data[:offset]
UCI_TEST_DATA = data[offset:]
i=feature_num-1;
data[:, i] = (data[:, i] - avgs[i]) / (maximums[i] - minimums[i])
UCI_LABEL_DATA = data[:offset, 13:14]
load_data(r"H:\1.data")
x_data = tf.cast(UCI_TRAIN_DATA,dtype=tf.float32)
y_data =tf.cast(UCI_LABEL_DATA,dtype=tf.float32)
print(x_data.shape)
W= tf.Variable(tf.zeros([14,1]), name="weights",dtype=tf.float32)
b = tf.Variable(0.0, name="biases",dtype=tf.float32)
y=tf.add(tf.matmul( x_data,W) ,b)
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.0000001)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(1000):
sess.run(train)
print( sess.run(W), sess.run(b))