This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Updating chart | |
| def update(i): | |
| frame_end = (i + 1) * STEPS_PER_FRAME | |
| for plot, dot, data in zip(plots, dots, plots_data): | |
| xs, ys, zs = data | |
| # Updating the trajectory | |
| plot.set_data(xs[:frame_end], ys[:frame_end]) | |
| plot.set_3d_properties(zs[:frame_end]) | |
| # Updating the position of dots | |
| dot._offsets3d = ([xs[frame_end]], [ys[frame_end]], [zs[frame_end]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Animation creation | |
| anim = FuncAnimation(fig, update, | |
| frames=np.arange(0, int(STEPS/STEPS_PER_FRAME)), interval=40) | |
| # Saving animation | |
| anim.save('lorenz_attractor.gif', dpi=80, writer='imagemagick') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| convert -delay 10 -loop 0 *.png keras_class_boundaries.gif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Auxiliary function creating graph of classification boundaries | |
| def save_model_prediction_graph(epoch, logs): | |
| prediction_probs = model.predict_proba(grid_2d, batch_size=32, verbose=0) | |
| plt.figure(figsize=(10,10)) | |
| sns.set_style("whitegrid") | |
| plt.title('Binary classification with KERAS - epoch: ' + makeIndexOfLength(epoch, 3), fontsize=20) | |
| plt.xlabel('X', fontsize=15) | |
| plt.ylabel('Y', fontsize=15) | |
| plt.contourf(X, Y, prediction_probs.reshape(100, 100), alpha = 0.7, cmap=cm.Spectral) | |
| plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train.ravel(), s=50, cmap=plt.cm.Spectral, edgecolors='black') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Adding callback functions that will run on each epoch | |
| testmodelcb = keras.callbacks.LambdaCallback(on_epoch_end=save_model_prediction_graph) | |
| # Compilation of the model | |
| model.compile(loss='binary_crossentropy', optimizer='adamax', metrics=['accuracy']) | |
| # Model training | |
| model.fit(X_train, y_train, epochs=N_EPOCHS, verbose=0, callbacks=[testmodelcb]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def init_layers(nn_architecture, seed = 99): | |
| np.random.seed(seed) | |
| number_of_layers = len(nn_architecture) | |
| params_values = {} | |
| for idx, layer in enumerate(nn_architecture): | |
| layer_idx = idx + 1 | |
| layer_input_size = layer["input_dim"] | |
| layer_output_size = layer["output_dim"] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| nn_architecture = [ | |
| {"input_dim": 2, "output_dim": 4, "activation": "relu"}, | |
| {"input_dim": 4, "output_dim": 6, "activation": "relu"}, | |
| {"input_dim": 6, "output_dim": 6, "activation": "relu"}, | |
| {"input_dim": 6, "output_dim": 4, "activation": "relu"}, | |
| {"input_dim": 4, "output_dim": 1, "activation": "sigmoid"}, | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sigmoid(Z): | |
| return 1/(1+np.exp(-Z)) | |
| def relu(Z): | |
| return np.maximum(0,Z) | |
| def sigmoid_backward(dA, Z): | |
| sig = sigmoid(Z) | |
| return dA * sig * (1 - sig) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def single_layer_forward_propagation(A_prev, W_curr, b_curr, activation="relu"): | |
| Z_curr = np.dot(W_curr, A_prev) + b_curr | |
| if activation is "relu": | |
| activation_func = relu | |
| elif activation is "sigmoid": | |
| activation_func = sigmoid | |
| else: | |
| raise Exception('Non-supported activation function') | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def full_forward_propagation(X, params_values, nn_architecture): | |
| memory = {} | |
| A_curr = X | |
| for idx, layer in enumerate(nn_architecture): | |
| layer_idx = idx + 1 | |
| A_prev = A_curr | |
| activ_function_curr = layer["activation"] | |
| W_curr = params_values["W" + str(layer_idx)] |