吴裕雄--天生自然TensorFlow高层封装:使用TensorFlow-Slim处理MNIST数据集实现LeNet-5模型

# 1. 通过TensorFlow-Slim定义卷机神经网络
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim

from tensorflow.examples.tutorials.mnist import input_data

# 通过TensorFlow-Slim来定义LeNet-5的网络结构。
def lenet5(inputs):
    inputs = tf.reshape(inputs, [-1, 28, 28, 1])
    net = slim.conv2d(inputs, 32, [5, 5], padding=‘SAME‘, scope=‘layer1-conv‘)
    net = slim.max_pool2d(net, 2, stride=2, scope=‘layer2-max-pool‘)
    net = slim.conv2d(net, 64, [5, 5], padding=‘SAME‘, scope=‘layer3-conv‘)
    net = slim.max_pool2d(net, 2, stride=2, scope=‘layer4-max-pool‘)
    net = slim.flatten(net, scope=‘flatten‘)
    net = slim.fully_connected(net, 500, scope=‘layer5‘)
    net = slim.fully_connected(net, 10, scope=‘output‘)
    return net

# 训练模型。
def train(mnist):
    x = tf.placeholder(tf.float32, [None, 784], name=‘x-input‘)
    y_ = tf.placeholder(tf.float32, [None, 10], name=‘y-input‘)
    y = lenet5(x)

    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    loss = tf.reduce_mean(cross_entropy)

    train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        for i in range(3000):
            xs, ys = mnist.train.next_batch(100)
            _, loss_value = sess.run([train_op, loss], feed_dict={x: xs, y_: ys})
            if i % 1000 == 0:
                print("After %d training step(s), loss on training batch is %g." % (i, loss_value))
#  主程序
def main(argv=None):
    mnist = input_data.read_data_sets("F:\\TensorFlowGoogle\\201806-github\\datasets\\MNIST_data\\", one_hot=True)
    train(mnist)

if __name__ == ‘__main__‘:
    main()

原文地址:https://www.cnblogs.com/tszr/p/12069987.html

时间: 2024-10-07 21:31:52

吴裕雄--天生自然TensorFlow高层封装:使用TensorFlow-Slim处理MNIST数据集实现LeNet-5模型的相关文章

吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习

import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platform import gfile import tensorflow.contrib.slim as slim # 加载通过TensorFlow-Slim定义好的inception_v3模型. import tensorflow.contrib.slim.python.slim.nets.incepti

吴裕雄--天生自然python Google深度学习框架:经典卷积神经网络模型

import tensorflow as tf INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE_SIZE = 28 NUM_CHANNELS = 1 NUM_LABELS = 10 CONV1_DEEP = 32 CONV1_SIZE = 5 CONV2_DEEP = 64 CONV2_SIZE = 5 FC_SIZE = 512 def inference(input_tensor, train, regularizer): with tf.variable_s

吴裕雄--天生自然 pythonTensorFlow图形数据处理:读取MNIST手写图片数据写入的TFRecord文件

import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 读取文件. filename_queue = tf.train.string_input_producer(["F:\\output.tfrecords"]) reader = tf.TFRecordReader() _,serialized_example = reader.re

吴裕雄--天生自然深度学习TensorBoard可视化:projector_MNIST

import os import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow.contrib.tensorboard.plugins import projector INPUT_NODE = 784 OUTPUT_NODE = 10 LAYER1_NODE = 500 def get_weight_variable(shape, regularizer):

吴裕雄--天生自然TensorFlow2教程:数据加载

import tensorflow as tf from tensorflow import keras # train: 60k | test: 10k (x, y), (x_test, y_test) = keras.datasets.mnist.load_data() x.shape y.shape # 0纯黑.255纯白 x.min(), x.max(), x.mean() x_test.shape, y_test.shape # 0-9有10种分类结果 y_onehot = tf.on

吴裕雄--天生自然TensorFlow2教程:测试(张量)- 实战

import tensorflow as tf from tensorflow import keras from tensorflow.keras import datasets import os # do not print irrelevant information # os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # x: [60k,28,28], [10,28,28] # y: [60k], [10k] (x, y), (x_test, y_te

吴裕雄--天生自然TensorFlow高层封装:解决ValueError: Invalid backend. Missing required entry : placeholder

找到对应的keras配置文件keras.json 将里面的内容修改为以下就可以了 原文地址:https://www.cnblogs.com/tszr/p/12095994.html

吴裕雄--天生自然 神经网络人工智能项目:基于深度学习TensorFlow框架的图像分类与目标跟踪报告(续一)

1.3 项目计划 第一周:深入学习和了解神经网络的工作原理,学习卷积的相关理论. 第二周:使用python的TensorFlow库,编写神经网络深度学习代码,搭建神经网络层,并且了解其工作原理和相关的计算.相关参数的传递等,到htttps://www.kaggle.com/moltean/fruits下载fruits压缩包,对数据进行初步的处理. 第三周:使用TensorFlow搭建卷积神经网络,采用训练集数据对测试集数据进行预测:完成数据可视化,显示每个文件夹中第5张图片.使用Tensorbo

吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集高层操作

import tempfile import tensorflow as tf # 1. 列举输入文件. # 输入数据生成的训练和测试数据. train_files = tf.train.match_filenames_once("F:\\output.tfrecords") test_files = tf.train.match_filenames_once("F:\\output_test.tfrecords") # 定义解析TFRecord文件的parser方