import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
rng = np.random
# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50
batch_size = 10
# Training Data
train_X = np.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 = np.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(tf.float32)
Y = tf.placeholder(tf.float32)
A = tf.Variable(tf.constant(1))
# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")
e = tf.constant([0.0])
# Construct a linear model
pred = tf.add(tf.multiply(W, X), b)
# Mean squared error
cost = tf.reduce_mean(tf.maximum(0., tf.subtract(tf.abs(tf.subtract(pred, Y)), e)))
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
# Start training
with tf.Session() as sess:
sess.run(init)
# Fit all training data
for epoch in range(training_epochs):
rand_index = np.random.choice(len(train_X), size=batch_size)
rand_x = train_X[rand_index]
rand_y = train_Y[rand_index]
sess.run(optimizer, feed_dict={X: rand_x[:,None], Y: rand_y[:,None]})
#Display logs per epoch step
if (epoch+1) % display_step == 0:
c = sess.run(cost, feed_dict={X: train_X[:,None], Y:train_Y[:,None]})
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[:,None], Y: train_Y[:,None]})
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.plot(train_X, sess.run(W) * train_X + sess.run(b) - sess.run(e), "--", label='down line')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b) + sess.run(e), "--", label='up line')
plt.legend()
plt.show()
相关推荐
svr源程序,输入到matlab中进行svr回归模型建立与分析
MATLAB实现SVR(支持向量机回归)多输入单输出数据集
SVR python实现
Matlab实现SVR支持向量机回归的时间序列预测(完整源码和数据) Matlab实现SVR支持向量机回归的时间序列预测(完整源码和数据) Matlab实现SVR支持向量机回归的时间序列预测(完整源码和数据) Mean squared error =...
Matlab实现EMD-GWO-SVR、EMD-SVR、GWO-SVR、SVR时间序列预测(完整源码和数据) Matlab实现EMD-GWO-SVR、EMD-SVR、GWO-SVR、SVR时间序列预测(完整源码和数据) 经验模态分解结合灰狼算法优化支持向量机、经验模态...
Matlab实现EMD-SVR经验模态分解结合支持向量机回归的时间序列预测(完整源码和数据) Matlab实现EMD-SVR经验模态分解结合支持向量机回归的时间序列预测(完整源码和数据) Mean squared error = 6.1843e-05 ...
回归预测 | MATLAB实现SVR(支持向量机回归)多输入单输出(完整源码和数据) 本程序为SVR支持向量机回归模型,多输入单输出,运行环境MATLAB2018b。
1.版本:matlab2014/2019a,内含运行结果,不会运行可私信 2.领域:智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,更多内容可点击博主头像 ...
Matlab实现EMD-GWO-SVR经验模态分解结合灰狼算法优化支持向量机回归时间序列预测(完整源码和数据) Matlab实现EMD-GWO-SVR经验模态分解结合灰狼算法优化支持向量机回归时间序列预测(完整源码和数据) 灰狼算法优化...
在线的SVR实现,Matlab版本实现。
智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab代码模型及运行结果
支持向量机回归预测,包括模型的建立和保存
训练SVR模型做预测,可调整训练集和测试集比例及SVR参数,预测性能用MAP反映
Matlab实现基于SVR支持向量机回归的空气质量预测模型 Matlab实现基于SVR支持向量机回归的空气质量预测模型 Matlab实现基于SVR支持向量机回归的空气质量预测模型 Matlab实现基于SVR支持向量机回归的空气质量预测模型
在matlab中,借助libsvm工具箱实现SVR
该代码属于支持向量回归技术的新实现,直接调用,简单易操作,训练和测试分开
该代码基于Python3
本文件提供了支持向量回归的算法,是svm中的一个算法
本文档对svr(支持向量回归机)作了简要的介绍,内涵一些常用的svr公式。
Python实现基于SVR支持向量机回归的AQI预测模型(完整源码和数据) Python实现基于SVR支持向量机回归的AQI预测模型(完整源码和数据) Python实现基于SVR支持向量机回归的AQI预测模型(完整源码和数据) 空气质量...