训练逻辑回归
创造数据
raw_data_X = [[1.85, 1.05],
[1.57, 2.63],
[2.28, 1.42],
[2.28, 3.64],
[1.94, 3.68],
[2.09, 2.66],
[1.49, 3.66],
[0.12, 1.12],
[0.25, 1.04],
[0.23, 0.54],
[0.83, 1.49],
[0.95, 0.09],
[0.46, 1.63],
[0.26, 1.03],
]
raw_data_Y = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1];
data = np.array(raw_data_X)
label = np.array(raw_data_Y)
data = np.hstack(data).reshape(-1,2)
label = np.hstack(label).reshape(-1,1)
label1 = label.reshape(1,-1)[0]
plt.scatter(data[label1 == 0, 0], data[label1 == 0, 1], marker="*")
plt.scatter(data[label1 == 1, 0], data[label1 == 1, 1], marker="^")
plt.show()
x = tf.placeholder(tf.float32,shape=(None,2))
y_ = tf.placeholder(tf.float32,shape=(None,1))
tf.random_normal()函数用于从服从指定正态分布的数值中取出指定个数的值
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None,
name=None)
weight = tf.Variable(tf.random_normal([2,1]), dtype=tf.float32)
bias = tf.Variable(tf.constant(0.1, shape=[1]))
tf.nn.sigmoid()是激活函数
y_hat = tf.nn.sigmoid(tf.matmul(x, weight) + bias)
不适用该损失函数
cost = tf.reducesum(tf.square(y - y_hat))
损失函数
cost = - tf.reducemean(y tf.log(tf.clip_by_value(yhat, 1e-10, 1.0)) + \
(1 - y) tf.log(tf.clip_by_value((1 - y_hat), 1e-10, 1.0)))
梯度下降
optimizer = tf.train.AdamOptimizer(0.001)
train = optimizer.minimize(cost)
开始训练
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
plt.ion()
for i in range(8000):
sess.run(train,feeddict={x:data,y:label})
#画出训练后的分割函数
#mgrid()函数产生两个300×400的数组:0~3每隔0.1取一个数,共300×400个
xx, yy = np.mgrid[0:3:.1,0:4:.1]
if (i % 20) == 0:
# np.c_用于合并两个数组
# ravel()函数将多维数组降为一维,仍返回array数组,元素以列排列
grid = np.c_[xx.ravel(), yy.ravel()]
probs = sess.run(y_hat, feed_dict={x:grid})
# print(probs)
probs = probs.reshape(xx.shape)
plt.cla() # 清除原有图像
plt.scatter(data[label1 == 0, 0], data[label1 == 0, 1], marker="*")
plt.scatter(data[label1 == 1, 0], data[label1 == 1, 1], marker="^")
plt.contour(xx, yy, probs, levels=[.5])
plt.pause(0.00001)
print("After %d steps, cost:%f" % (i, sess.run(cost, feed_dict=
{x:data,y_:label})))
plt.close()