<jupyter_start><jupyter_text>Desafio CNN >  Instalando o Keras e o URLLib<jupyter_code>!pip install keras
!pip install urllib<jupyter_output>Requirement already satisfied: keras in /usr/local/lib/python3.6/dist-packages
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.6/dist-packages (from keras)
Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python3.6/dist-packages (from keras)
Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.6/dist-packages (from keras)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from keras)
Collecting urllib
[31m  Could not find a version that satisfies the requirement urllib (from versions: )[0m
[31mNo matching distribution found for urllib[0m<jupyter_text>>  Importando as bibliotecas da CNN<jupyter_code>import keras
from keras.models import Model
from keras.layers import *
from keras.optimizers import *
from google.colab import files

import urllib.request

import h5py

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>>  Fazendo o download do dataset para o colaboratory<jupyter_code>urllib.request.urlretrieve("http://infra.heurys.com.br:8080/files/dataset.zip", "dataset.h5")<jupyter_output><empty_output><jupyter_text>>  Carregando o dataset para as variáveis (20% para teste e 80% para treinamento)<jupyter_code>with h5py.File('dataset.h5','r') as hf:
    X = hf['X'][:]
    y = hf['y'][:]
    X_test = hf['X_test'][:]
    
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.20, random_state=42)<jupyter_output><empty_output><jupyter_text>>  Normalizando os dados<jupyter_code>X_train = X_train.astype('float32')
X_val = X_val.astype('float32')
X_test = X_test.astype('float32')

X_train /= 255
X_val  /= 255
X_test /= 255

X_train = X_train.reshape(-1,256,256,1)
X_val = X_val.reshape(-1,256,256,1)
X_test = X_test.reshape(-1,256,256,1)<jupyter_output><empty_output><jupyter_text>>  Definição da CNN<jupyter_code>inputs = Input((256, 256,1))

Conv = Conv2D(1, kernel_size=(3,3), strides=(5, 5), padding='same', activation='relu')(inputs)
Conv = Conv2D(1, kernel_size=(3,3), strides=(5, 5), padding='same', activation='relu')(Conv)

flat = Flatten()(Conv)

dense = Dense(8,activation = 'sigmoid')(flat)
dense = Dense(1, activation = 'sigmoid')(dense)

model = Model(inputs, dense)
model.compile(loss='mean_squared_error',optimizer=SGD(lr=4.0), metrics=['acc'])
model.summary()<jupyter_output><empty_output><jupyter_text>>  Treinamento da CNN<jupyter_code>history = model.fit(X_train, y_train,
          batch_size=256,
          epochs=10,
          verbose=1,
          validation_data=(X_val, y_val))<jupyter_output>Train on 120 samples, validate on 30 samples
Epoch 1/10<jupyter_text>>  Exibe o gráfico de erro e acurácia<jupyter_code>plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.ylabel('acurácia')
plt.xlabel('época')
plt.legend(['treino', 'teste'], loc='upper left')
plt.show()

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.ylabel('loss')
plt.xlabel('época')
plt.legend(['treino', 'teste'], loc='upper left')
plt.show()

train_score = model.evaluate(X_train, y_train, verbose=0)
val_score = model.evaluate(X_val, y_val, verbose=0)
print('---- Summary ----')
print('train loss.....:', train_score[0].round(10), 'train accuracy.....:', train_score[1].round(10))
print('validation loss:', val_score[0].round(10),   'validation accuracy:', val_score[1].round(10))<jupyter_output><empty_output><jupyter_text>>  Resultado da rede (Salvar o conteúdo em um arquivo csv e enviar para avaliação) - Serão apenas 3 tentativas consideradas<jupyter_code>#pred = model.predict(X_test)
#np.savetxt('result.csv', pred)
#files.download('result.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15121698601760747, 'fraction_numerical': 0.03443811496633868, 'mean_word_length': 6.344106463878327, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '42973', 'license': 'MIT', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1453, 'n_words': 283, 'path': 'HandsOn_Hiper_Parametros/challange_datah_HyperParameters.ipynb', 'repo_name': 'WesGtoX/Simple-Projects-I2A2', 'size': '3862'}