神经网络_多层感知机MLP_以mnist为例(tensorflow和keras实现)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_, labels=y))

optimizer1= tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost) 

 

# Adam Optimizer优化器

from tensorflow.examples.tutorials.mnist import input_data   
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)  
  
import tensorflow as tf  
  
x = tf.placeholder("float", [None, 784])  
y = tf.placeholder("float", [None, 10])  
  
h1_W = tf.Variable(tf.random_normal([784, 256]))  
h1_b = tf.Variable(tf.random_normal([256]))  
  
out_W = tf.Variable(tf.random_normal([256, 10]))  
out_b = tf.Variable(tf.random_normal([10])) 
  
h1 = tf.nn.sigmoid(tf.add(tf.matmul(x, h1_W), h1_b)) 
#不要softmax #不要softmax #不要softmax #不要softmax #不要softmax 
y_ = tf.matmul(h1, out_W) + out_b
#y_是预测值(未经过softmax的)  
#y_是预测值(未经过softmax的)  
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_, labels=y))  
optimizer1= tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost) # Adam Optimizer  
  
batch_size = 100  
training_epochs = 15  
  
with tf.Session() as sess:  
    sess.run(tf.global_variables_initializer())  
  
    for epoch in range(training_epochs):  
        avg_cost = 0.  
        total_batch = int(mnist.train.num_examples/batch_size)  
                #num_examples=55000训练例子,验证例子是5000。55000/100=550  
        for i in range(total_batch):  
  
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)  
  
            sess.run(optimizer1, feed_dict={x: batch_xs, y: batch_ys})  
            avg_cost = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})  
  
        print('epoch: %d, cost: %.9f' % (epoch+1, avg_cost))  
  
    correct_prediction = tf.equal(tf.argmax(y_, 1), tf.argmax(y, 1))  
    # Calculate accuracy  
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))  
    print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))  

四层感知机例子

# -*- coding: utf-8 -*-

from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf

# Parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100
display_step = 1

# Network Parameters
n_hidden_1 = 256 # 1st layer num features
n_hidden_2 = 256 # 2nd layer num features
n_input = 784 # MNIST data input (img shape: 28*28)
n_classes = 10 # MNIST total classes (0-9 digits)

# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])

# Create model
def multilayer_perceptron(_X, _weights, _biases):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(_X, _weights['h1']), _biases['b1'])) #Hidden layer with sigmoid activation
    layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, _weights['h2']), _biases['b2'])) #Hidden layer with RELU activation
    return tf.matmul(layer_2, _weights['out']) + _biases['out']  
                            #并未softmax化
# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
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]))
}

# Construct model
pred = multilayer_perceptron(x, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=pred, logits=y))           # Softmax loss
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)                  # Adam Optimizer

# Initializing the variables
init = tf.initialize_all_variables()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            # Fit training using batch data
            sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
            # Compute average loss
            avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))

    print("Optimization Finished!")

    # Test model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    # Calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

keras例子

 

# -*- coding: utf-8 -*-

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop


batch_size = 128
num_classes = 10
epochs = 4

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# if urlopen fails, please download https://s3.amazonaws.com/img-datasets/mnist.npz
# and move mnist.npz to C:/user/<your username>/.keras/datasets

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])

history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值