TensorFlow(9)神经网络

加载数据

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
trainbig = np.load("trainbig.npy")
labelbig = np.load("labelbig.npy")
labelbig.shape

创建网络

两个隐藏层

# NETWORK TOPOLOGIES
n_hidden_1 = 256 
n_hidden_2 = 128 
n_input    = 784 
n_classes  = 10  

# INPUTS AND OUTPUTS
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
    
# NETWORK PARAMETERS
stddev = 0.1
weights = {
    'w1': tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=stddev)),
    'w2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev=stddev)),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes], stddev=stddev))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}
print ("NETWORK READY")

定义函数

def multilayer_perceptron(_X, _weights, _biases):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(_X, _weights['w1']), _biases['b1'])) 
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, _weights['w2']), _biases['b2']))
    return (tf.matmul(layer_2, _weights['out']) + _biases['out'])
# PREDICTION
pred = multilayer_perceptron(x, weights, biases)

# LOSS AND OPTIMIZER
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) 
optm = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost) 
corr = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))    
accr = tf.reduce_mean(tf.cast(corr, "float"))

# INITIALIZER
init = tf.global_variables_initializer()
print ("FUNCTIONS READY")

拟合数据

training_epochs = 100
batch_size      = 1000
display_step    = 5
# LAUNCH THE GRAPH
sess = tf.Session()
sess.run(init)
# OPTIMIZE
for epoch in range(training_epochs):
    avg_cost = 0.
    total_batch = int(trainbig.shape[0]/batch_size)
    # ITERATION
    for i in range(total_batch):
        batch_xs = trainbig[batch_size*i:batch_size*(i+1),:]
        batch_ys = labelbig[batch_size*i:batch_size*(i+1)]
        feeds = {x: batch_xs, y: batch_ys}
        sess.run(optm, feed_dict=feeds)
        avg_cost += sess.run(cost, feed_dict=feeds)
    avg_cost = avg_cost / total_batch
    # DISPLAY
    if (epoch+1) % display_step == 0:
        print ("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost))
        feeds = {x: batch_xs, y: batch_ys}
        train_acc = sess.run(accr, feed_dict=feeds)
        print ("TRAIN ACCURACY: %.3f" % (train_acc))
print ("OPTIMIZATION FINISHED")

输出结果

效果还不如逻辑回归模型

Epoch: 004/100 cost: 2.317420933
TRAIN ACCURACY: 0.127
Epoch: 009/100 cost: 2.288076971
TRAIN ACCURACY: 0.158
Epoch: 014/100 cost: 2.279465738
TRAIN ACCURACY: 0.182
Epoch: 019/100 cost: 2.270973765
TRAIN ACCURACY: 0.209
Epoch: 024/100 cost: 2.262400070
TRAIN ACCURACY: 0.239
Epoch: 029/100 cost: 2.253708367
TRAIN ACCURACY: 0.278
Epoch: 034/100 cost: 2.244863622
TRAIN ACCURACY: 0.306
Epoch: 039/100 cost: 2.235831651
TRAIN ACCURACY: 0.331
Epoch: 044/100 cost: 2.226578463
TRAIN ACCURACY: 0.349
Epoch: 049/100 cost: 2.217070584
TRAIN ACCURACY: 0.369
Epoch: 054/100 cost: 2.207274978
TRAIN ACCURACY: 0.400
Epoch: 059/100 cost: 2.197159137
TRAIN ACCURACY: 0.426
Epoch: 064/100 cost: 2.186690694
TRAIN ACCURACY: 0.445
Epoch: 069/100 cost: 2.175837971
TRAIN ACCURACY: 0.463
Epoch: 074/100 cost: 2.164569948
TRAIN ACCURACY: 0.476
Epoch: 079/100 cost: 2.152856080
TRAIN ACCURACY: 0.493
Epoch: 084/100 cost: 2.140666556
TRAIN ACCURACY: 0.504
Epoch: 089/100 cost: 2.127972384
TRAIN ACCURACY: 0.516
Epoch: 094/100 cost: 2.114745610
TRAIN ACCURACY: 0.527
Epoch: 099/100 cost: 2.100959375
TRAIN ACCURACY: 0.534
OPTIMIZATION FINISHED

你可能感兴趣的:(TensorFlow(9)神经网络)