<jupyter_start><jupyter_text>1. Prepare dataset<jupyter_code>%run create_dataset_hologram.py
# Load the dataset
X_train = np.load('X_train.npy')
Y_train = np.load('Y_train.npy')

X_test = np.load('X_test.npy')
Y_test = np.load('Y_test.npy')

print('Train: ', X_train.shape, Y_train.shape)
print('Test : ', X_test.shape, Y_test.shape)<jupyter_output>Train:  (1200, 40000) (1200,)
Test :  (300, 40000) (300,)<jupyter_text>2. Neural network<jupyter_code># Create the model
model = Sequential() # build a model layer by layer

# Add model layers

# First layer (input layer)
nb_nodes1 = 12
input_dim1 = 8 
activation1 = 'relu' # Rectified linear unit (ReLU)

model.add(Dense(nb_nodes1, input_dim=input_dim1, activation=activation1))

# Second layer (hidden layer)
nb_nodes2 = 8
activation2 = 'relu' # Rectified linear unit (ReLU)

model.add(Dense(nb_nodes2, activation=activation2))

# Third layer (output layer)
nb_nodes3 = 1
activation3 = 'sigmoid' # Sigmoid, ensures the newtowork output between 0 and 1

model.add(Dense(nb_nodes3, activation=activation3))
# loss_fct = 'categorical_crossentropy' # Used for classification problem
loss_fct = 'binary_crossentropy'
opt_fct = 'adam' # Popular version of Gradient Descent
metrics_fct = ['accuracy'] # Classification problem

model.compile(loss=loss_fct, optimizer=opt_fct, metrics=metrics_fct)
nb_epochs = 150
nb_batchs = 10
history = model.fit(X, y, epochs=nb_epochs, batch_size=nb_batchs)
# Summarize model.
model.summary()
plt.plot(history.history['loss'], label = 'train')
plt.xlabel('Number of epochs')
plt.ylabel('Loss')
plt.show()
plt.plot(history.history['accuracy'], label = 'train')
plt.xlabel('Number of epochs')
plt.ylabel('Accuracy')
plt.show()
# load the dataset
dataset = np.loadtxt('pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:8]
y = dataset[:,8]
print(X.shape)
print(y.shape)<jupyter_output>(768, 8)
(768,)<jupyter_text>Reshaped dataset input<jupyter_code># Reshape the dataset input to the shape that our model expects
# The first number is the number of images, then comes the shape of each image
# The last number is 1, which signifies that the images are greyscale

X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)

print('Train: ', X_train.shape)
print('Test : ', X_test.shape)<jupyter_output><empty_output><jupyter_text>'One-hot-encode' the target variable<jupyter_code># One-hot encode target column: create a column for each category and a binary 
# variable is inputted for each category
Y_train = to_categorical(Y_train)
Y_test = to_categorical(Y_test)

print('Train: ', Y_train.shape)
print(Y_train)
print('Test : ', Y_test.shape)<jupyter_output><empty_output><jupyter_text>**Building the model**<jupyter_code># Create model
model = Sequential() # build a model layer by layer
# Number of nodes in the first and second layer
nb_nodes_1 = 64
nb_nodes_2 = 32

# Kernel size
k_size = 3
# Add model layers

# Conv2D layer: 
# 64 nodes, 3x3 filter matrix, Rectified Linear Activation as activation function,
# shape of each input (200, 200, 1,) with 1 signifying images are greyscale
model.add(Conv2D(nb_nodes_1, kernel_size = k_size, activation = 'relu', input_shape = (40000, 1)))

# 32 nodes
model.add(Conv2D(nb_nodes_2, kernel_size = k_size, activation = 'relu'))

# Flatten layer: connection between the convolution and dense layers
model.add(Flatten())

# Dense layer: used for the output layer
# 5 nodes for the output layer, one for each possible outcome (1-5)
# 'softmax' as activation function, it makes the output sump up to 1 so the output
# can be interpreted as probalities
model.add(Dense(nb_class, activation = 'softmax'))<jupyter_output><empty_output><jupyter_text>**Compiling the model**<jupyter_code># Three parameters:
# optmizer: 'adam'
# loss function: 'categorical_crossentropy', the most common choice for classification
# metrics: 'accuracy', to see the accuracy score
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
# Number of times that the learning algorihm will work through the entire training set
nb_epochs = 10

# Number of samples to work through before updating the interval model parameters
nb_batch = 1<jupyter_output><empty_output><jupyter_text>**Training the model**<jupyter_code>%%time

# Number of epochs: number of tmes the model wil cycle trough the data
history = model.fit(X_train, Y_train, validation_data = (X_test, Y_test), epochs = nb_epochs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11562841530054645, 'fraction_numerical': 0.02360655737704918, 'mean_word_length': 6.2405063291139244, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '389873', 'license': 'Unlicense', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1569, 'n_words': 455, 'path': 'neural_network/old_jupyter_notebooks/jupyter_neural_network_classification_problem_holograms.ipynb', 'repo_name': 'fernandolucasaa/computerGeneratedHolography', 'size': '4575'}