# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('mnist/',one_hot=True)

import numpy as np
import tensorflow as tf

X_train = mnist.train.images
Y_train = mnist.train.labels
X_test = mnist.test.images
Y_test = mnist.test.labels

import matplotlib.pyplot as plt
plt.imshow(X_train[0].reshape((28,28)))
plt.title('Numero '+str(np.argmax(Y_train[0])))

X_batch,Y_batch = mnist.train.next_batch(64)
X_batch.shape

neuronios_entrada = X_train.shape[1]
neuronios_entrada

neuronios_oculta1 = int((X_train.shape[1] + Y_train.shape[1])/2)
neuronios_oculta1

neuronios_oculta2=neuronios_oculta1
neuronios_oculta3=neuronios_oculta1
neuronios_saida = Y_train.shape[1]
neuronios_saida

# +
#784->397->397->10
# -

W = {
    'oculta1':tf.Variable(tf.random_normal([neuronios_entrada,neuronios_oculta1])),
    'oculta2':tf.Variable(tf.random_normal([neuronios_oculta1,neuronios_oculta2])),
    'oculta3':tf.Variable(tf.random_normal([neuronios_oculta2,neuronios_oculta3])),
    'saida':tf.Variable(tf.random_normal([neuronios_oculta3,neuronios_saida]))
}

b = {
    'oculta1':tf.Variable(tf.random_normal([neuronios_oculta1])),
    'oculta2':tf.Variable(tf.random_normal([neuronios_oculta2])),
    'oculta3':tf.Variable(tf.random_normal([neuronios_oculta3])),
    'saida':tf.Variable(tf.random_normal([neuronios_saida]))
}

Xph = tf.placeholder('float',[None,neuronios_entrada])
Yph = tf.placeholder('float',[None,neuronios_saida])


def mlp(x,w,bias):
    camada_oculta1 = tf.nn.relu(tf.add(tf.matmul(x, W['oculta1']),bias['oculta1']))
    camada_oculta2 = tf.nn.relu(tf.add(tf.matmul(camada_oculta1, W['oculta2']),bias['oculta2']))
    camada_oculta3 = tf.nn.relu(tf.add(tf.matmul(camada_oculta2, W['oculta3']),bias['oculta3']))
    camada_saida = tf.add(tf.matmul(camada_oculta3, W['saida']),bias['saida'])
    return camada_saida


modelo = mlp(Xph,W,b)
erro = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits = modelo, labels = Yph))
otimizador = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(erro)
init = tf.global_variables_initializer()

previsoes = tf.nn.softmax(modelo)
previsoes_corretas = tf.equal(tf.argmax(previsoes,1),tf.argmax(Yph,1))
taxa = tf.reduce_mean(tf.cast(previsoes_corretas,tf.float32))

with tf.Session() as sess:
    sess.run(init)
    for epoca in range(5000):
        X_batch,Y_batch = mnist.train.next_batch(128)
        _ ,custo = sess.run([otimizador,erro],feed_dict = {Xph:X_batch, Yph:Y_batch})
        if epoca %100==0:
            error = sess.run(taxa,feed_dict = {Xph:X_test, Yph:Y_test})
            print('época:'+str(epoca+1)+' erro:'+str(custo)+' acc:'+str(error))
    print('Treinamento Concluído!')   
    print(sess.run(taxa,feed_dict = {Xph:X_test, Yph:Y_test}))

Y_test

StarcoderdataJupyterScriptsDedupFiltered