使用tensorflow优化函数:
import numpy as np
import tensorflow as tf
#初始化变量
w = tf.Variable(0, dtype=tf.float32)
#初始化cost function
#type 1
#cost = tf.add(tf.add(w**2, tf.multiply(-10.0, w)), 25)
#type 2
cost = w**2 - 10*w + 25
#初始化学习算法
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
#初始化全局变量
init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
print(session.run(w)) #评估w的值
#运行一次学习算法迭代
session.run(train)
print(session.run(w))
#运行1000次学习算法迭代
for i in range(1000):
session.run(train)
print(session.run(w))
运行结果:
0.0
0.099999994
4.9999886
参数在训练的时候加入
coefficients = np.array([[1], [-10], [25]])
w = tf.Variable([0], dtype=tf.float32)
x = tf.placeholder(tf.float32, [3, 1])
cost = x[0][0]*w**2 + x[1][0]*w + x[2][0]
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
#初始化全局变量
init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
print(session.run(w)) #评估w的值
for i in range(1000):
session.run(train, feed_dict={x:coefficients})
print(session.run(w))
运行结果
[0.]
[4.9999886]
logistic regression
import tensorflow as tf
import numpy as np
from numpy.linalg import cholesky
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sampleNo = 100
mu = np.array([[2, 3]])
Sigma = np.array([[1, 0.5], [1.5, 3]])
R = cholesky(Sigma)
s = np.dot(np.random.randn(sampleNo, 2), R) + mu
mu2 = np.array([[7, 9]])
t = np.dot(np.random.randn(sampleNo, 2), R) + mu2
plt.plot(s[:,0],s[:,1],'+')
plt.plot(t[:,0],t[:,1],'*')
plt.show()
image.png
#构造数据
x_data = np.concatenate((s, t)).T
x_data = x_data.astype(np.float32)
y1 = np.zeros(100).reshape(1,100)
y2 = np.ones(100).reshape(1,100)
y_data = np.concatenate((y1, y2), axis=1)
y_data = y_data.astype(np.float32)
#初始化参数
w_0 = np.random.randn(2) / 2
w_0 = w_0.reshape(2, 1)
w_0 = w_0.astype(np.float32)
b_0 = np.random.randn(1)
b_0 = b_0.astype(np.float32)
#tf变量
x = tf.placeholder(tf.float32, [2, 200])
y = tf.placeholder(tf.float32, [1, 200])
w = tf.Variable(w_0)
b = tf.Variable(b_0)
m = tf.constant(200, dtype=tf.float32)
#cost function
z = tf.matmul(tf.transpose(w), x) + b
a = 1 / (1 + tf.exp(-z))
cost = -tf.reduce_sum(y * tf.log(a) + (1-y) * tf.log(1-a)) / m
# tf全局变量
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(cost, feed_dict={x:x_data,y:y_data}))
#训练
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
for i in range(5000):
sess.run(train, feed_dict={x:x_data, y:y_data})
print(sess.run(cost, feed_dict={x:x_data, y:y_data}))
def logistic(z):
return 1 / (1 + np.exp(-z))
#显示结果
w_des = sess.run(w)
b_des = sess.run(b)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x_data[0], x_data[1], y_data)
x1_tmp = x2_tmp = np.linspace(-2, 15, 100)
x1_tmp, x2_tmp = np.meshgrid(x1_tmp, x2_tmp)
x_tmp = np.concatenate((x1_tmp.reshape(1, 10000), x2_tmp.reshape(1, 10000)))
z_tmp = np.dot(w_des.T, x_tmp) + b_des
a_tmp = logistic(z_tmp)
y_prected = a_tmp.reshape(100, 100)
ax.plot_surface(x1_tmp, x2_tmp, y_prected)
ax.view_init(elev=10,azim=-5)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
image.png