辅助阅读:TensorFlow中文社区教程 - 英文官方教程
代码见:full_connect.py
def reformat(dataset, labels):
dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
# Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...]
labels = (np.arange(num_labels) == labels[:, None]).astype(np.float32)
return dataset, labels
使用梯度计算train_loss,用tf.Graph()创建一个计算单元
it is also good practice to initialize them with a slightly positive initial bias to avoid "dead neurons."
所以init时value设置为0.1会比较好(恩,这也是黑魔法)
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
上面这些变量都是一种Tensor的概念,它们是一个个的计算单元,我们在Graph中设置了这些计算单元,规定了它们的组合方式,就好像把一个个门电路串起来那样
Session用来执行Graph里规定的计算,就好像给一个个门电路通上电,我们在Session里,给计算单元冲上数据,That’s Flow.
重复计算单元反复训练800次,提高其准确度
with tf.Session(graph=graph) as session:
tf.global_variables_initializer().run()
for step in range(num_steps):
_, l, predictions = session.run([optimizer, loss, train_prediction])
valid_prediction.eval()
这样训练的准确度为83.2%
每次只取一小部分数据做训练,计算loss时,也只取一小部分数据计算loss
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :]
batch_labels = train_labels[offset:(offset + batch_size), :]
tf_train_dataset = tf.placeholder(tf.float32,
shape=(batch_size, image_size * image_size))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
准确率提高到86.5%,而且准确率随训练次数增加而提高的速度变快了
Y = W2 * RELU(W1*X + b1) + b2
[n * 10] = RELU([n * 784] · [784 * N] + [n * N]) · [N * 10] + [n * 10]
weights1 = tf.Variable(
tf.truncated_normal([image_size * image_size, hidden_node_count]))
biases1 = tf.Variable(tf.zeros([hidden_node_count]))
weights2 = tf.Variable(
tf.truncated_normal([hidden_node_count, num_labels]))
biases2 = tf.Variable(tf.zeros([num_labels]))
ys = tf.matmul(tf_train_dataset, weights1) + biases1
hidden = tf.nn.relu(ys)
logits = tf.matmul(hidden, weights2) + biases2