- Tensorflow基础
关于Tensorflow,具体可以参考http://www.tensorfly.cn/来进行学习。下图为Tensorlfow基本结构图:
节点(operation) | 节点被称为op(也叫做操作,算子),一个节点获得0个或者多个张量数据(tensor),一个节点也可以产生0个或者多个张量数据(tensor)。 |
张量(tensor) | 计算图的一条边,就是一个tensor,一个张量数据,张量的流动是保持计算节点不变,让数据进行流动。tensor可以是一维的也可以是多维的 |
变量(variable) | 变量(tf.Variable)用于保存,更新神经网络的参数张量。 |
图(graph) | 节点和边相互连接成计算图,一个计算图描述了一次计算过程 |
会话(session) | 使用teonserflow编写的程序,通常有一个构建阶段和运行阶段。 为了得到结果,计算图必须在会话中启动,会话将图的op(节点)分发到CPU或者GPU之类的设备中,同时执行节点op的方法,处理结束后将tensor返回。
# 创建tf.Session的一个对象sess # 运行graph # 关闭sess对象 |
- Tensorflow实现DNN模型(具体算法笔记随后在机器学习模块中添加)
在我们之前的博文中有提到,我们创建神经网络的结构主要为以下结构
也就是说,我们输入的判断是那个floor的影响因素是多维的dc1-dcn,magnextcx,y,z这些。然后通过神经网络的elu激活函数激活,逐步得到递归值floor。location同理。代码中,关于X和Location和floor的model图构建代码如下:
# Declare session and placeholder
self.sess = tf.InteractiveSession()
self.x = tf.placeholder(tf.float32, [None, 10])
self.floor_y = tf.placeholder(tf.float32, [None, 1])
self.location_y = tf.placeholder(tf.float32, [None, 1])
# Define the structure of the network which can predict the building ID
floor_network = tl.layers.InputLayer(self.x, name='Input')
floor_network = tl.layers.DenseLayer(floor_network, n_units=128, act=tf.nn.elu, name='DL_fn1')
floor_network = tl.layers.DenseLayer(floor_network, n_units=32, act=tf.nn.elu, name='DL_fn2')
floor_network = tl.layers.DenseLayer(floor_network, n_units=16, act=tf.nn.elu, name='DL_fn3')
floor_network = tl.layers.DenseLayer(floor_network, n_units=8, act=tf.nn.elu, name='DL_fn4')
floor_network = tl.layers.DenseLayer(floor_network, n_units=4, act=tf.nn.elu, name='DL_fn5')
floor_network = tl.layers.DenseLayer(floor_network, n_units=2, act=tf.nn.elu, name='DL_fn6')
floor_network = tl.layers.DenseLayer(floor_network, n_units=1, act=tf.identity, name='DL_fn7')
self.floor_predict_y = floor_network.outputs
self.floor_cost = tl.cost.mean_squared_error(self.floor_y, self.floor_predict_y)
self.floor_optimize = tf.train.AdamOptimizer().minimize(self.floor_cost, var_list=floor_network.all_params)
# Define the structure of the network which can predict the building ID
location_network = tl.layers.InputLayer(self.x, name='Input')
location_network = tl.layers.DenseLayer(location_network, n_units=128, act=tf.nn.elu, name='DL_lc1')
location_network = tl.layers.DenseLayer(location_network, n_units=32, act=tf.nn.elu, name='DL_lc2')
location_network = tl.layers.DenseLayer(location_network, n_units=16, act=tf.nn.elu, name='DL_lc3')
location_network = tl.layers.DenseLayer(location_network, n_units=8, act=tf.nn.elu, name='DL_lc4')
location_network = tl.layers.DenseLayer(location_network, n_units=4, act=tf.nn.elu, name='DL_lc5')
location_network = tl.layers.DenseLayer(location_network, n_units=2, act=tf.nn.elu, name='DL_lc6')
location_network = tl.layers.DenseLayer(location_network, n_units=1, act=tf.identity, name='DL_lc7')
self.location_predict_y = location_network.outputs
self.location_cost = tl.cost.mean_squared_error(self.location_y, self.location_predict_y)
self.location_optimize = tf.train.AdamOptimizer().minimize(self.location_cost, var_list=location_network.all_params)
构建完成之后,我们对会话进行初始化并填充数据,填充数据之后,我们进行运算训练,最后我们将训练模型进行保存
self.sess.run(tf.global_variables_initializer())
for i in range(epoch):
mini_x = dp.getMiniBatch(self.normalize_x, batch_size)
mini_y = dp.getMiniBatch(np.expand_dims(self.floorNum_y, -1), batch_size)
# 填充数据为采集到的CSV训练数据
feed_dict = {
self.x: mini_x.__next__(),
self.floor_y: mini_y.__next__()
}
_cost, _, _output = self.sess.run([self.floor_cost, self.floor_optimize, self.floor_predict_y], feed_dict=feed_dict)
self.save()
样本预测值:这里得到的是根据每个输入点的RSSI和magnetic预测的Floor和location。在训练集中floor和location均为1
该DNN模型在该点的识别误差平均在
- Keras基础
Keras是一个基于 Python 编写的高级神经网络 API,可以理解为比较的高端的Tensorflow。就是API使用更加方便。具体的学习资料请参考Keras中文文档:https://keras.io/zh/
- Keras实现DNN模型(具体算法笔记随后在机器学习模块中添加)
首先创建一个具有多个输入层的全连接网络模型:
self.encode_layer = Dense(512, activation='elu')(self.input_x)
self.encode_layer = Dense(256, activation='elu')(self.encode_layer)
self.encode_layer = Dense(64, activation='elu')(self.encode_layer)
decode_layer = Dense(256, activation='elu')(self.encode_layer)
decode_layer = Dense(512, activation='elu')(decode_layer)
decode_layer = Dense(10, activation='elu')(decode_layer)
self.encoder_model = Model(inputs=self.input_x, outputs=decode_layer)
# merge_layer = Concatenate([self.longitude_predict_output, self.latitude_predict_output])
floor_net = Dense(512, activation='elu')(self.encode_layer)
floor_net = Dense(256, activation='elu')(floor_net)
floor_net = Dense(128, activation='elu')(floor_net)
floor_net = Dense(64, activation='elu')(floor_net)
# 3表示3种情况变量
self.floor_predict_output = Dense(3, activation='elu')(floor_net)
self.floor_model = Model(inputs=self.input_x, outputs=self.floor_predict_output)
location_net = Dense(512, activation='elu')(self.encode_layer)
location_net = Dense(256, activation='elu')(location_net)
location_net = Dense(128, activation='elu')(location_net)
location_net = Dense(64, activation='elu')(location_net)
self.location_predict_output = Dense(3, activation='elu')(location_net)
self.location_model = Model(inputs=self.input_x, outputs=self.location_predict_output)
之后设置损失函数,并注入数据开始训练:(激活函数在Dense()中的activationji)
self.encoder_model.compile(
loss='mse',
optimizer='adam'
)
self.encoder_model.fit(self.normalize_x, self.normalize_x, epochs=500, batch_size=100)
经过迭代训练之后,
我们得到训练预测数据(左),添加多个测试训练点(右)
预测的误差值均值为:
欢迎大家批评!