• 使用一层神经网络训练mnist数据集


    import numpy as np
    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    def add_layer(inputs,in_size,out_size,activation_function=None):
        W=tf.Variable(tf.random_normal([in_size,out_size]))
        b=tf.Variable(tf.zeros([1,out_size])+0.01)
        Z=tf.matmul(inputs,W)+b
        if activation_function is None:
            out_puts=Z
        else:
            out_puts=activation_function(Z)
        return out_puts
    if __name__=="__main__":
        MINST=input_data.read_data_sets("./",one_hot=True)
        learning_rate=0.05
        batch_size=128
        n_epochs=10
        X=tf.placeholder(tf.float32,[batch_size,784])
        Y=tf.placeholder(tf.float32,[batch_size,10])
        L1=add_layer(X,784,1000,tf.nn.relu)
        prediction=add_layer(L1,1000,10)
        entropy=tf.nn.softmax_cross_entropy_with_logits(labels=Y,logits=prediction)
        loss=tf.reduce_mean(entropy)
        optimizer=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
        init=tf.global_variables_initializer()
        with tf.Session() as sess:
            sess.run(init)
            n_batches=int(MINST.train.num_examples/batch_size)
            for i in range(n_epochs):
                for j in range(n_batches):
                    X_batch,Y_batch=MINST.train.next_batch(batch_size=batch_size)
                    _,loss_=sess.run([optimizer,loss],feed_dict={
                        X:X_batch,
                        Y:Y_batch
                    })
                    if j == 0:
                        print("Loss of epochs[{0}] batch[{1}]: {2}".format(i, j, loss_))
    
            # test the model
            n_batches = int(MINST.test.num_examples / batch_size)
            total_correct_preds = 0
            for i in range(n_batches):
                X_batch, Y_batch = MINST.test.next_batch(batch_size)
                preds = sess.run(prediction, feed_dict={X: X_batch, Y: Y_batch})
                correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y_batch, 1))
                accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
    
                total_correct_preds += sess.run(accuracy)
    
            print("Accuracy {0}".format(total_correct_preds / MINST.test.num_examples))

    我们不做卷积。直接将x输入到网络中去。最后用softmax作为激活函数

    大概结构,我这里没法上传,等我回去在传。

  • 相关阅读:
    dN/dS与分子进化常用软件
    samtools和bcftools使用说明
    变异检测VarScan软件使用说明
    线程可以共享进程里的哪些资源
    函数调用与系统调用的区别
    海量数据统计出现次数
    海量数据查找问题
    建立高并发模型需要考虑的点
    言简意赅的TIME_WAIT
    常用的TCP选项
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/9531875.html
Copyright © 2020-2023  润新知