# -*- coding: utf-8 -*-
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt # 可视化模块
# 创建数据
# create some data
X = np.linspace(-1, 1, 200)
np.random.shuffle(X) # randomize the data
Y = 0.5 * X + 2 + np.random.normal(0, 0.05, (200, ))
# plot data 绘制散点图
plt.scatter(X, Y)
X_train, Y_train = X[:160], Y[:160] # train 前 160 data points
X_test, Y_test = X[160:], Y[160:] # test 后 40 data points
# 建立模型
model = Sequential()
model.add(Dense(1, input_dim=1)) # 输入是一维,输出也是一维
# 激活模型
# choose loss function and optimizing method
model.compile(loss='mse', optimizer='sgd')
# 训练模型
for step in range(301):
cost = model.train_on_batch(X_train, Y_train)
if step % 100 == 0:
print('train cost: ', cost)
# 检验模型
print('\nTesting ------------')
cost = model.evaluate(X_test, Y_test, batch_size=40)
print('test cost:', cost)
W, b = model.layers[0].get_weights()
print('Weights=', W, '\nbiases=', b)
# 将训练的结果绘制出来
plt.scatter(X, X * W + b, c = 'r', s= 5) # draw the line after training
plt.show()
显示的结果
train cost: 4.173795223236084
train cost: 0.10054540634155273
train cost: 0.010608138516545296
train cost: 0.004354666918516159
Testing ------------
test cost: 0.005134253762662411
Weights= [[0.5617714]]
biases= [2.0009663]
以下是Tensorflow的写法
# -*- coding: utf-8 -*-
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np
import matplotlib.pyplot as plt
#添加网络层
def add_layer(inputs, in_size, out_size, activation_function=None): #添加一个层
w = tf.Variable(tf.random_normal([in_size, out_size])) #从正态分布中生成随机值
b = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.add(tf.matmul(inputs, w), b, name = "result")
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
#保存
def save(tf,sess):
constant_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, ["result"])
with tf.gfile.FastGFile("model.pb", mode='wb') as f:
f.write(constant_graph.SerializeToString())
#定义一个(300,1)的列x_data, 同时生成一个y_data, 然后进行预测, 输入-隐藏-输出 三层,训练1000次, 每50次看loss变化, 并把原始数据和预测的数据可视化
x_data = np.linspace(-1, 1, 200)[:, np.newaxis]
print('x_data.shape =',x_data.shape)
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = 0.5 * x_data + 2 + noise
print('y_data.shape =',y_data.shape)
xs = tf.placeholder(tf.float32, [None,1])
ys = tf.placeholder(tf.float32, [None,1])
#构造模型
prediction = add_layer(xs, 1, 1, activation_function=tf.nn.relu)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
#开始训练
for i in range(1000):
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
print('loss = ',sess.run(loss, feed_dict={xs:x_data, ys:y_data}))
#保存训练好的模型
save(tf,sess)
#画图,把原始数据可视化
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
ax.plot(x_data, prediction_value,color='red') # 画线
ax.scatter(x_data, y_data) # 画散点
plt.show()
显示控制台结果
x_data.shape = (200, 1)
y_data.shape = (200, 1)
loss = 3.1638856
loss = 0.0031950492
loss = 0.0028336623
loss = 0.0028333224
loss = 0.0028333217
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226
loss = 0.0028333226