tensorflow实验-线性回归

实验结果:
这里写图片描述

Epoch: 0050 cost= 1779611648.000 W= 21.1876 b= -1.94716
Epoch: 0100 cost= 1779628928.000 W= 21.1871 b= -28.8248
Epoch: 0150 cost= 1779607424.000 W= 21.1888 b= -56.0113
Epoch: 0200 cost= 1779576064.000 W= 21.1912 b= -83.3023
Epoch: 0250 cost= 1779540736.000 W= 21.1937 b= -110.634
Epoch: 0300 cost= 1779503872.000 W= 21.1963 b= -137.979
Epoch: 0350 cost= 1779466112.000 W= 21.199 b= -165.326
Epoch: 0400 cost= 1779427712.000 W= 21.2017 b= -192.666
Epoch: 0450 cost= 1779389184.000 W= 21.2044 b= -219.997
Epoch: 0500 cost= 1779350784.000 W= 21.2071 b= -247.317
Epoch: 0550 cost= 1779312128.000 W= 21.2099 b= -274.623
Epoch: 0600 cost= 1779273600.000 W= 21.2126 b= -301.916
Epoch: 0650 cost= 1779234944.000 W= 21.2153 b= -329.197
Epoch: 0700 cost= 1779196288.000 W= 21.2181 b= -356.462
Epoch: 0750 cost= 1779157888.000 W= 21.2208 b= -383.714
Epoch: 0800 cost= 1779119360.000 W= 21.2235 b= -410.952
Epoch: 0850 cost= 1779080832.000 W= 21.2262 b= -438.175
Epoch: 0900 cost= 1779042432.000 W= 21.229 b= -465.386
Epoch: 0950 cost= 1779004160.000 W= 21.2317 b= -492.58
Epoch: 1000 cost= 1778965760.000 W= 21.2344 b= -519.762
Optimization Finished!
Training cost= 1.77897e+09 W= 21.2344 b= -519.762 
#coding:utf-8
'''
    线性回归
                y=Wx+b
    其中W为权重,b为偏置,x 即为LotArea,y 即为SalePrice。

'''
from __future__ import print_function, division
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 读入数据
train = pd.read_csv("dataset/linear_regression.csv")
# 选取房屋面积小于12000的数据
train = train[train['LotArea'] < 12000]
train_X = train['LotArea'].values.reshape(-1, 1)
train_Y = train['SalePrice'].values.reshape(-1, 1)

n_samples = train_X.shape[0]
# 学习率
learning_rate = 2
# 迭代次数
training_epochs = 1000
# 每多少次输出一次迭代结果
display_step = 50

# 这个X和Y和上面的train_X,train_Y是不一样的,这里只是个占位符,
# 训练开始的时候需要“喂”(feed)数据给它
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# 定义模型参数
W = tf.Variable(np.random.randn(), name="weight", dtype=tf.float32)
b = tf.Variable(np.random.randn(), name="bias", dtype=tf.float32)

# 定义模型
pred = tf.add(tf.multiply(W, X), b)
# 定义损失函数
cost = tf.reduce_sum(tf.pow(pred-Y, 2)) / (2 * n_samples)
# 使用Adam算法,至于为什么不使用一般的梯度下降算法,一会说
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

# 初始化所有变量
init = tf.initialize_all_variables()

# 训练开始
with tf.Session() as sess:
    sess.run(init)

    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        if (epoch + 1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
            print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.3f}".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')

    # 画图
    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()

dataset/linear_regression.csv

,LotArea,SalePrice
0,8450,208500
1,9600,181500
2,11250,223500
3,9550,140000
4,10084,307000
5,10382,200000
6,6120,129900
7,7420,118000
8,11200,129500
9,11924,345000
10,10652,279500

测试1
生成测试数据

for x in range(0,100):
    y = 3+3*x
    print ",".join([str(x),str(x),str(y)])

测试结果

这里写图片描述

Epoch: 0050 cost= 0.000 W= 3.0 b= 2.99642
Epoch: 0100 cost= 1087.274 W= 3.14082 b= -50.4254
Epoch: 0150 cost= 36.720 W= 3.15869 b= -12.0979
Epoch: 0200 cost= 376.034 W= 2.29296 b= 19.6809
Epoch: 0250 cost= 0.008 W= 3.00197 b= 2.79233
Epoch: 0300 cost= 1718.088 W= 4.19537 b= -103.558
Epoch: 0350 cost= 0.246 W= 2.98618 b= 4.2617
Epoch: 0400 cost= 32.799 W= 3.14644 b= -11.1574
Epoch: 0450 cost= 0.123 W= 2.99225 b= 3.82624
Epoch: 0500 cost= 0.000 W= 3.00001 b= 2.99951
Epoch: 0550 cost= 81.916 W= 3.11827 b= 9.48149
Epoch: 0600 cost= 0.000 W= 2.99996 b= 2.99695
Epoch: 0650 cost= 47.679 W= 3.16947 b= -13.8402
Epoch: 0700 cost= 0.070 W= 3.00629 b= 2.36031
Epoch: 0750 cost= 0.000 W= 3.00002 b= 2.99861
Epoch: 0800 cost= 1.976 W= 3.03423 b= -0.419249
Epoch: 0850 cost= 0.003 W= 2.99807 b= 3.15099
Epoch: 0900 cost= 0.001 W= 3.00133 b= 2.90672
Epoch: 0950 cost= 0.002 W= 2.9985 b= 3.1233
Epoch: 1000 cost= 0.000 W= 3.00005 b= 2.99974
Optimization Finished!
Training cost= 3.12927e-06 W= 3.00005 b= 2.99974 

参考资料
http://blog.csdn.net/u010099080/article/details/52894773

好的,我可以为您提供逻辑回归模型的 TensorFlow 实现实验报告。在这份实验报告中,我们将讨论逻辑回归模型的基本概念和 TensorFlow 实现过程。 1. 逻辑回归模型概述 逻辑回归模型是一种分类算法,它通常用于解决二分类问题。它基于线性回归模型,但其输出被限制在0和1之间。逻辑回归模型的输出可以被解释为某个事件发生的概率。在训练过程中,我们使用一组特征来预测一个二元目标变量的值。 2. TensorFlow 实现过程 为了实现逻辑回归模型,我们需要使用 TensorFlow 库。以下是 TensorFlow 实现逻辑回归模型的步骤: (1) 导入所需的库: ```python import tensorflow as tf import numpy as np import matplotlib.pyplot as plt ``` (2) 准备数据 我们需要准备训练数据和测试数据。在这里,我们将使用一个简单的数据集。 ```python train_X = np.array([[1., 2.], [2., 3.], [3., 4.], [4., 5.], [5., 6.], [6., 7.], [7., 8.], [8., 9.]]) train_Y = np.array([[0.], [0.], [0.], [0.], [1.], [1.], [1.], [1.]]) ``` (3) 定义模型 我们使用 TensorFlow 创建一个简单的逻辑回归模型。 ```python # 定义输入和输出 X = tf.placeholder(tf.float32, shape=[None, 2]) Y = tf.placeholder(tf.float32, shape=[None, 1]) # 定义权重和偏置项 W = tf.Variable(tf.zeros([2, 1])) b = tf.Variable(tf.zeros([1])) # 定义输出 logits = tf.matmul(X, W) + b pred = tf.nn.sigmoid(logits) ``` (4) 定义损失函数和优化器 我们使用交叉熵作为损失函数,并使用梯度下降优化器来最小化损失函数。 ```python # 定义损失函数和优化器 loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=Y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss) ``` (5) 训练模型 我们使用 TensorFlow 训练模型。 ```python # 训练模型 sess = tf.Session() sess.run(tf.global_variables_initializer()) for i in range(1000): _, l = sess.run([optimizer, loss], feed_dict={X: train_X, Y: train_Y}) if (i + 1) % 100 == 0: print("Epoch:", i + 1, "Loss:", l) ``` (6) 测试模型 我们使用 TensorFlow 测试模型。 ```python # 测试模型 test_X = np.array([[9., 10.], [10., 11.]]) test_Y = np.array([[1.], [1.]]) print("Accuracy:", sess.run(tf.reduce_mean(tf.cast(tf.equal(tf.round(pred), Y), tf.float32)), feed_dict={X: test_X, Y: test_Y})) ``` 3. 总结 逻辑回归模型是一种简单而强大的分类算法。在 TensorFlow 中,我们可以很容易地实现逻辑回归模型。在本实验中,我们讨论了逻辑回归模型的基本概念和 TensorFlow 实现过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值