所有代码数据可在百度云下载:
链接: https://pan.baidu.com/s/1c31hKLM 密码: 4tpm
所有涉及tensorflow API用法的,均可查看https://tensorflow.google.cn/api_docs/
下面的代码实现了一个单层感知机(Single Layer Perceptron) y=softmax(wx+b),来处理MNIST手写数字识别问题。
# mnist_slp.py
# -*- coding: utf-8 -*-
import tensorflow as tf
from input_data import read_data_sets
import os
# don't show INFO
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
# read mnist
mnist = read_data_sets('MNIST_data', one_hot=True)
# single layer perceptron: y = wx + b
# input
x = tf.placeholder(tf.float32, [None, 784])
# weights
W = tf.Variable(tf.random_normal([784,10], stddev=0.1))
# bias
b = tf.Variable(tf.zeros([10]))
# softmax
y = tf.nn.softmax(tf.matmul(x,W) + b)