机器学习-回归算法




四,代码

# -*- coding: utf-8 -*-
import os
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

num_points = 100

Sample_X = []
Sample_y = []

for i in range(num_points):
    # np.random.normal(mean,divation)
    x1 = np.random.normal(0.0, 0.55)
    x2 = np.random.normal(0.0, 0.56)
    y1 = x1 * 0.1 + x2 * 0.2 + 0.3 + np.random.normal(0.0, 0.03)
    Sample_X.append([x1, x2])
    Sample_y.append([y1])

x_data = Sample_X
y_data = Sample_y


W = tf.Variable(tf.random_uniform([2, 1], -1, 1, dtype=tf.float32), name="weight")
b = tf.Variable(tf.zeros([1, 1]), name="b", dtype=tf.float32)

x_ = tf.placeholder(tf.float32, [100, 2], name="x-input")
y_ = tf.placeholder(tf.float32, [100, 1], name="y-input")

y = tf.add(tf.matmul(x_, W), b)
loss = tf.reduce_mean(tf.square(y-y_), name="loss")

train = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
init = tf.global_variables_initializer()
count = 0
with tf.Session() as sess:
    sess.run(init)
    for times in range(100000):
        _ = sess.run([train], feed_dict={x_:x_data, y_:y_data})
        loss_ = sess.run([loss], feed_dict={x_:x_data, y_:y_data})
        if count % 100 == 0:
            print(sess.run(W), sess.run(b))
        count = count + 1



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值