????????參數(shù)設(shè)計好之后耿导,需要理解tensorflow存儲數(shù)據(jù)的方式:使用占位符(參考tensorflow的英文文檔)
# x y placeholder
x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_classes])
? ? ? ? 設(shè)置初始權(quán)重和偏置:
# 對 weights biases 初始值的定義
weights = {
# shape (28, 128)
? ? 'in': tf.Variable(tf.random_normal([n_inputs, n_hidden_units])),
# shape (128, 10)
? ? 'out': tf.Variable(tf.random_normal([n_hidden_units, n_classes]))
}
biases = {
# shape (128, )
? ? 'in': tf.Variable(tf.constant(0.1,shape=[n_hidden_units, ])),
# shape (10, )
? ? 'out': tf.Variable(tf.constant(0.1,shape=[n_classes, ]))
}
隨后晦攒,導(dǎo)入前兩周設(shè)計后的數(shù)據(jù):
def init_Vec():
? ? data_vec = open('vectors.txt')
? ? line_vec = data_vec.readlines()
? ? vec_list = []
? ? for s in line_vec:
? ? ? ? num = s.split(" ")
? ? ? ? #print(num)
? ? ? ? #num.remove("\n")
? ? ? ? num = list(map(float, num))
? ? ? ? vec_list.append(num)
? ? return vec_list
def init_Tag():
? ? data_tag = open('tag.txt')
? ? line_tag = data_tag.readlines()
? ? tag_list = []
? ? for s in line_tag:
? ? ? ? num = int(s)
? ? ? ? if num == 0:
? ? ? ? ? ? tag_list.append([0, 0, 1])
? ? ? ? if num == 1:
? ? ? ? ? ? tag_list.append([0, 1, 0])
? ? ? ? if num == 2:
? ? ? ? ? ? tag_list.append([1, 0, 0])
? ? return tag_list
RNN定義:
????????首先,為什么需要矩陣轉(zhuǎn)換呢端辱,我們可以得知n_steps*n_inputs是向量的長度梁剔,我們每次輸入僅僅是1/n_steps的數(shù)據(jù),而我們需要一整塊向量來計算最終的結(jié)果舞蔽,需要用上一次訓(xùn)練好的權(quán)重荣病,偏執(zhí)來計算,然后在這個基礎(chǔ)上在進(jìn)行擬合計算渗柿,可以根據(jù)lstm的結(jié)構(gòu)看出个盆。
def RNN(X, weights, biases):
? ? # 原始的 X 是 3 維數(shù)據(jù), 我們需要把它變成 2 維數(shù)據(jù)才能使用 weights 的矩陣乘法
? ? X = tf.reshape(X, [-1, n_inputs])
? ? # X_in = W*X + b
? ? X_in = tf.matmul(X, weights['in']) + biases['in']
? ? # X_in ==> (128 batches, 28 steps, 128 hidden) 換回3維
? ? X_in = tf.reshape(X_in, [-1, n_steps, n_hidden_units])
? ? # 使用 basic LSTM Cell.
? ? lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units, forget_bias=1.0, state_is_tuple=True)
? ? init_state = lstm_cell.zero_state(batch_size, dtype=tf.float32)? # 初始化全零 state
? ? outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, X_in, initial_state=init_state, time_major=False)
? ? results = tf.matmul(final_state[1], weights['out']) + biases['out']
? ? return results
最后,定義main函數(shù)朵栖,即可開始訓(xùn)練:
with tf.Session() as sess:
? ? sess.run(init)
? ? step = 1
? ? while step * batch_size < max_samples:
? ? ? ? batch_x, batch_y = mnist.train.next_batch(batch_size)
? ? ? ? batch_x = batch_x.reshape((batch_size, n_steps, n_input))
? ? ? ? sess.run(optimizer, feed_dict = {x: batch_x, y: batch_y})
? ? ? ? if step % display_step == 0:
? ? ? ? ? ? acc = sess.run(accuracy, feed_dict = {x: batch_x, y: batch_y})
? ? ? ? ? ? loss = sess.run(cost, feed_dict = {x: batch_x, y: batch_y})
? ? ? ? ? ? print("Iter" + str(step * batch_size) + ", Minibatch Loss = " + \
? ? ? ? ? ? ? ? "{:.6f}".format(loss) + ", Training Accuracy = " + \
? ? ? ? ? ? ? ? "{:.5f}".format(acc))
? ? ? ? step += 1
? ? print("Optimization Finished!")
? ? test_len = 10000
? ? test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
? ? test_label = mnist.test.labels[:test_len]
? ? print("Testing Accuracy:", sess.run(accuracy, feed_dict = {x: test_data, y: test_label}))
現(xiàn)在颊亮,我們可以看一下訓(xùn)練的結(jié)果:
可以看出訓(xùn)練的效果還是不錯,在自己的訓(xùn)練集上做測試出來結(jié)果還算滿意陨溅。